Reputation:
I want to calculate the response time taken by each request. Currently I am sending 50 requests to a service and I am getting responses after all the requests are sent. So I'm unable to calculate the time taken for each response as there are 49 requests after the first call.I am using http module and not http client module. I feel the chrome developer tools is giving a correct response time but how do I capture it in angular 4 ?
Upvotes: 1
Views: 7207
Reputation: 8295
Here's a simple interceptor that shows the time for each request from the angular perspective. You can actually modify it to include the time along with the response... I'm not sure how you want to use the time.
import { HttpInterceptor, HttpRequest, HttpHandler } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { defer } from 'rxjs';
import { finalize } from 'rxjs/operators';
@Injectable()
export class PerformanceInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler) {
return defer(() => {
const key = req.urlWithParams;
console.time(key);
return next.handle(req).pipe(finalize(() => {
console.timeEnd(key);
}));
});
}
}
Upvotes: 2