diff --git a/packages/http/src/interfaces/http-response.interface.ts b/packages/http/src/interfaces/http-response.interface.ts index c51f96e99..067ba3e2e 100644 --- a/packages/http/src/interfaces/http-response.interface.ts +++ b/packages/http/src/interfaces/http-response.interface.ts @@ -25,4 +25,14 @@ export interface HttpResponseInterface { * The request that was sent for getting this response. */ request: HttpRequestInterface; + + /** + * The time it took to get the response (in ms). + */ + responseTime?: number; + + /** + * The time it took to get the first byte of the response (in ms). + */ + timeToFirstByte?: number; } diff --git a/packages/http/src/wrappers/http.wrapper.ts b/packages/http/src/wrappers/http.wrapper.ts index e78e9fc91..4f05135d6 100644 --- a/packages/http/src/wrappers/http.wrapper.ts +++ b/packages/http/src/wrappers/http.wrapper.ts @@ -24,6 +24,8 @@ export class HttpWrapper implements HttpWrapperInterface { executeRequest(request: HttpRequestInterface): Promise { return new Promise((resolve, reject) => { // Define the options required by the http and https modules. + let now: number = -1; + const url = new URL(request.url); const options: RequestOptions = { host: url.hostname, @@ -48,20 +50,25 @@ export class HttpWrapper implements HttpWrapperInterface { let body = ''; res.on('data', chunk => { + response.timeToFirstByte = performance.now() - now; body = body + "" + chunk; }); res.on('error', error => { + response.responseTime = performance.now() - now; return reject(error); }) res.on('end', async () => { response.body = body; + response.responseTime = performance.now() - now; return resolve(response); }); } + now = performance.now(); + // Make the http or https call depending on the url. if (url.protocol === "http://" || url.protocol === "http:" || url.protocol === "http") { const req = httpRequest(options, callback);