Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 5, 2024
1 parent 85e08d7 commit 0513d6c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 35 deletions.
5 changes: 1 addition & 4 deletions src/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ export class HttpClient extends EventEmitter {
const internalStore = {
requestId,
requestStartTime: performance.now(),
enableRequestTiming: !!options?.timing,
enableRequestTiming: !!(options?.timing ?? true),
} as InternalStore;
return await asyncLocalStorage.run(internalStore, async () => {
return await this.#requestInternal<T>(url, options);
Expand All @@ -275,8 +275,6 @@ export class HttpClient extends EventEmitter {
debug('Request#%d dns lookup %sms, servername: %s, origin: %s',
store.requestId, dnslookup, options.servername, options.origin);
}
console.log(options);
handler.opaque = options.opaque;
return dispatch(options, handler);
};
},
Expand Down Expand Up @@ -344,7 +342,6 @@ export class HttpClient extends EventEmitter {
contentDownload: 0,
};
internalStore.requestTiming = timing;
internalStore.enableRequestTiming = !!args.timing;
const originalOpaque = args.opaque;
const reqMeta = {
requestId,
Expand Down
2 changes: 1 addition & 1 deletion src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export class FetchFactory {
const internalStore = {
requestId,
requestStartTime: performance.now(),
enableRequestTiming: !!init.timing,
enableRequestTiming: !!(init.timing ?? true),
requestTiming: timing,
} as InternalStore;
const reqMeta: RequestMeta = {
Expand Down
73 changes: 43 additions & 30 deletions test/options.timing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,55 +17,68 @@ describe('options.timing.test.ts', () => {
await close();
});

it.only('should timing = true work', async () => {
// _url = _url.replace('localhost', '127.0.0.1');
it('should timing = true work', async () => {
_url = _url.replace('localhost', '127.0.0.1');
let response = await urllib.request(`${_url}?content-encoding=gzip`, {
dataType: 'json',
timing: true,
});
assert.equal(response.status, 200);
let res = response.res as RawResponseWithMeta;
console.log(res.timing);
// assert(res.timing.waiting > 0);
// assert(res.timing.dnslookup > 0);
// assert(res.timing.queuing > 0);
// assert(res.timing.connected > 0);
// assert(res.timing.requestHeadersSent > 0);
// assert(res.timing.requestSent > 0);
// assert(res.timing.contentDownload > 0);
// assert(res.timing.contentDownload > res.timing.waiting);
// assert(res.timing.contentDownload <= res.rt);
assert(res.socket.handledResponses === 1);
// console.log(res.timing);
assert(res.timing.waiting > 0);
assert(res.timing.dnslookup > 0);
assert(res.timing.queuing > 0);
assert(res.timing.connected > 0);
assert(res.timing.requestHeadersSent > 0);
assert(res.timing.requestSent > 0);
assert(res.timing.contentDownload > 0);
assert(res.timing.contentDownload > res.timing.waiting);
assert(res.timing.contentDownload <= res.rt);
assert.equal(res.socket.handledRequests, 1);
assert.equal(res.socket.handledResponses, 1);

// again connected should be zero
await sleep(1000);
await sleep(1);

response = await urllib.request(`${_url}?content-encoding=gzip`, {
dataType: 'json',
timing: true,
});
await sleep(1000);
console.log(response);
assert.equal(response.status, 200);
res = response.res as RawResponseWithMeta;
console.log(res.timing);
// assert.equal(res.timing.waiting, 0);
// assert(res.timing.dnslookup > 0);
// assert(res.timing.queuing > 0);
// assert.equal(res.timing.connected, 0);
// assert(res.timing.requestHeadersSent > 0);
// assert(res.timing.requestSent > 0);
// assert(res.timing.contentDownload > 0);
// assert(res.timing.contentDownload > res.timing.waiting);
// assert(res.timing.contentDownload <= res.rt);
// assert(res.socket.handledResponses === 2);
// console.log(res.timing);
assert(res.timing.waiting > 0);
assert(res.timing.dnslookup > 0);
assert(res.timing.queuing > 0);
assert.equal(res.timing.connected, 0);
assert(res.timing.requestHeadersSent > 0);
assert(res.timing.requestSent > 0);
assert(res.timing.contentDownload > 0);
assert(res.timing.contentDownload > res.timing.waiting);
assert(res.timing.contentDownload <= res.rt);
assert.equal(res.socket.handledRequests, 2);
assert.equal(res.socket.handledResponses, 2);

await sleep(1);

response = await urllib.request(`${_url}?content-encoding=gzip`, {
dataType: 'json',
timing: true,
});
await sleep(1000);
console.log(response);
res = response.res as RawResponseWithMeta;
// console.log(res.timing);
assert(res.timing.waiting > 0);
assert(res.timing.dnslookup > 0);
assert(res.timing.queuing > 0);
assert.equal(res.timing.connected, 0);
assert(res.timing.requestHeadersSent > 0);
assert(res.timing.requestSent > 0);
assert(res.timing.contentDownload > 0);
assert(res.timing.contentDownload > res.timing.waiting);
assert(res.timing.contentDownload <= res.rt);
assert.equal(res.socket.handledRequests, 3);
assert.equal(res.socket.handledResponses, 3);
});

it('should timing = false work', async () => {
Expand All @@ -88,7 +101,7 @@ describe('options.timing.test.ts', () => {
assert.equal(response.status, 200);
const res = response.res as RawResponseWithMeta;
// console.log(res.timing);
assert.equal(res.timing.waiting, 0);
assert(res.timing.waiting > 0);
assert(res.timing.dnslookup > 0);
assert(res.timing.contentDownload > 0);
assert(res.rt > 0);
Expand Down

0 comments on commit 0513d6c

Please sign in to comment.