Skip to content

Commit

Permalink
Fix setting phases.request before the connect event
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak committed Jan 4, 2019
1 parent 5006a4f commit 3eb9568
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
20 changes: 17 additions & 3 deletions source/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';

const deferToConnect = require('defer-to-connect');

module.exports = request => {
Expand Down Expand Up @@ -30,13 +29,21 @@ module.exports = request => {
if (event === 'error') {
timings.error = Date.now();
timings.phases.total = timings.error - timings.start;

origin.emit = emit;
}

// Saves the original behavior
return emit(event, ...args);
};
};

let uploadFinished = false;
const onUpload = () => {
timings.upload = Date.now();
timings.phases.request = timings.upload - timings.connect;
};

handleError(request);

request.once('socket', socket => {
Expand All @@ -60,12 +67,19 @@ module.exports = request => {
}

timings.phases.tcp = timings.connect - timings.lookup;

if (uploadFinished && !timings.upload) {
onUpload();
}
});
});

request.once('finish', () => {
timings.upload = Date.now();
timings.phases.request = timings.upload - timings.connect;
uploadFinished = true;

if (timings.connect) {
onUpload();
}
});

request.once('response', response => {
Expand Down
36 changes: 30 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,18 @@ test.after('cleanup', async () => {

const error = 'Simple error';

const makeRequest = () => {
const request = https.get('https://httpbin.org/anything');
const makeRequest = (url = 'https://httpbin.org/anything') => {
const {protocol} = new URL(url);
const fn = protocol === 'http:' ? http : https;

const request = fn.get(url);
const timings = timer(request);

return {request, timings};
};

test('by default everything is set to null', t => {
const request = https.get('https://httpbin.org/anything');
const timings = timer(request);
const {timings} = makeRequest();

t.is(typeof timings, 'object');
t.is(typeof timings.start, 'number');
Expand Down Expand Up @@ -93,8 +95,7 @@ test('phases', async t => {
});

test('no memory leak (`lookup` event)', async t => {
const request = http.get(s.url);
timer(request);
const {request} = makeRequest();

await pEvent(request, 'finish');

Expand Down Expand Up @@ -142,3 +143,26 @@ test('doesn\'t throw when someone used `.prependOnceListener()`', async t => {

await t.notThrows(() => emitter.emit('error', new Error(error)));
});

test('sensible timings', async t => {
const {timings, request} = makeRequest('http://google.com');
const now = Date.now();

const response = await pEvent(request, 'response');
response.resume();
await pEvent(response, 'end');

t.true(timings.socket >= now);
t.true(timings.lookup >= now);
t.true(timings.connect >= now);
t.true(timings.response >= now);
t.true(timings.end >= now);
t.is(timings.error, null);
t.true(timings.phases.wait < 1000);
t.true(timings.phases.dns < 1000);
t.true(timings.phases.tcp < 1000);
t.true(timings.phases.request < 1000);
t.true(timings.phases.firstByte < 1000);
t.true(timings.phases.download < 1000);
t.true(timings.phases.total < 1000);
});

0 comments on commit 3eb9568

Please sign in to comment.