Skip to content

Commit

Permalink
fix: use timestamp instead of Date object (#511)
Browse files Browse the repository at this point in the history
  • Loading branch information
realyuyanan authored Dec 15, 2021
1 parent 9a86eaf commit 8e6bf33
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
14 changes: 7 additions & 7 deletions lib/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,31 @@ export interface RequestLog {
/**
* Request begin.
*/
requestStart: Date;
requestStart: number;
/**
* request.on("socket")
*/
onSocket: Date;
onSocket: number;
/**
* Exact time that dns look up done.
*/
onLookUp: Date;
onLookUp: number;
/**
* Exact time that client finished sending HTTP request to the server.
*/
requestFinish: Date;
requestFinish: number;
/**
* socket.on("connect")
*/
socketConnect: Date;
socketConnect: number;
/**
* request.on("response")
*/
onResponse: Date;
onResponse: number;
/**
* response.on("close")
*/
responseClose: Date;
responseClose: number;
/**
* milliseconds Fiddler spent in DNS looking up the server's IP address.
*/
Expand Down
27 changes: 12 additions & 15 deletions lib/core/runtime/capture/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export const hack = <T extends typeof http.request>(
};

const { timestamps } = requestLog;
timestamps.requestStart = new Date();
timestamps.requestStart = new Date().getTime();

const clearDomain = (): void => {
const parser = (request.socket as any)?.parser as any;
Expand All @@ -117,7 +117,7 @@ export const hack = <T extends typeof http.request>(
};

request.once("socket", (socket: Socket): void => {
timestamps.onSocket = new Date();
timestamps.onSocket = new Date().getTime();

if (!isIP(hostname)) {
socket.once("lookup", (
Expand All @@ -126,9 +126,8 @@ export const hack = <T extends typeof http.request>(
family: string | number,
host: string
): void => {
timestamps.onLookUp = new Date();
timestamps.dnsTime = timestamps.onLookUp.getTime()
- timestamps.onSocket.getTime();
timestamps.onLookUp = new Date().getTime();
timestamps.dnsTime = timestamps.onLookUp - timestamps.onSocket;

logger.debug(`${logPre} Dns lookup ${host} -> ${
address || "null"}. Cost ${timestamps.dnsTime}ms`);
Expand All @@ -146,12 +145,12 @@ export const hack = <T extends typeof http.request>(
}

socket.once("connect", (): void => {
timestamps.socketConnect = new Date();
timestamps.socketConnect = new Date().getTime();

logger.debug(`${logPre} Socket connected. Remote: ${
socket.remoteAddress
}:${socket.remotePort}. Cost ${
timestamps.socketConnect.getTime() - timestamps.onSocket.getTime()
timestamps.socketConnect - timestamps.onSocket
} ms`);
});

Expand All @@ -177,7 +176,7 @@ export const hack = <T extends typeof http.request>(
request.once("close", clearDomain);

request.once("finish", () => {
timestamps.requestFinish = new Date();
timestamps.requestFinish = new Date().getTime();

context.captureSN += 1;

Expand All @@ -196,14 +195,14 @@ export const hack = <T extends typeof http.request>(
logger.debug(`${logPre} Request send finish. Body size ${
length
}. Cost: ${
timestamps.requestFinish.getTime() - timestamps.onSocket.getTime()
timestamps.requestFinish - timestamps.onSocket
} ms`);

clearDomain();
});

request.once("response", (response: http.IncomingMessage): void => {
timestamps.onResponse = new Date();
timestamps.onResponse = new Date().getTime();

const { socket } = response;
requestLog.serverIp = socket.remoteAddress;
Expand All @@ -220,15 +219,14 @@ export const hack = <T extends typeof http.request>(
}:${socket.remotePort}. Response status code: ${
response.statusCode
}. Cost: ${
timestamps.onResponse.getTime()
- timestamps.onSocket.getTime()
timestamps.onResponse - timestamps.onSocket
} ms`);

// responseInfo can't retrieve data until response "end" event
const responseInfo = captureIncoming(response);

response.once("end", () => {
timestamps.responseClose = new Date();
timestamps.responseClose = new Date().getTime();

requestLog.statusCode = response.statusCode;
requestLog.responseLength = responseInfo.bodyLength;
Expand Down Expand Up @@ -262,8 +260,7 @@ export const hack = <T extends typeof http.request>(
logger.debug(`${logPre} Response on end. Body size:${
requestLog.responseLength
}. Cost: ${
timestamps.responseClose.getTime()
- timestamps.onSocket.getTime()
timestamps.responseClose - timestamps.onSocket
} ms`);

finishRequest();
Expand Down
6 changes: 3 additions & 3 deletions lib/core/runtime/create-server.hack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const hack = <T extends typeof http.createServer>(
}

const requestListenerWrap: http.RequestListener = (req, res) => {
const start = new Date();
const start = new Date().getTime();
const timestamps: RequestLog["timestamps"] = {
dnsTime: 0,
requestStart: start,
Expand Down Expand Up @@ -80,7 +80,7 @@ export const hack = <T extends typeof http.createServer>(
res.writeHead = ((fn): typeof res.writeHead => (
...args: unknown[]
): ReturnType<typeof res.writeHead> => {
timestamps.onResponse = new Date();
timestamps.onResponse = new Date().getTime();

const context = currentContext();

Expand Down Expand Up @@ -154,7 +154,7 @@ export const hack = <T extends typeof http.createServer>(
});

res.once("close", () => {
timestamps.responseClose = new Date();
timestamps.responseClose = new Date().getTime();

const context = currentContext();

Expand Down

0 comments on commit 8e6bf33

Please sign in to comment.