我如何测量服务器端 HTTP REQUEST 和 HTTP RESPONSE 之间的时间?

How can i measure the time between HTTP REQUEST & HTTP RESPONSE on server side?

所以,我有这个手机游戏和一个服务器。 我想像这样测量服务器的处理时间: 时间(发送 HTTP 响应)- 时间(收到 HTTP 请求)。

有谁能帮助我吗? 有什么工具可以做到这一点吗?

如果使用Nginx来托管PHP,你可以使用$request_time来记录request-received和response-sent之间的时间:

$request_time – Full request time, starting when NGINX reads the first byte from the client and ending when NGINX sends the last byte of the response body

更多信息请参考official document


如果使用 Apache 托管 PHP,您可以在日志中使用 %D (mod_log_config)。

%D - The time taken to serve the request, in microseconds.

请参阅此article了解更多信息。

如果您只想查看服务器响应时间,您可以使用 chrome 开发控制台并查看 request-response 周期所用的时间

如果您想以编程方式计算(假设您使用的是 Node 后端)时间,请使用:

//receive request
console.time();

// my server code...

console.timeEnd();
//send response

或者,您可以使用 js 日期对象

const timerStart = new Date();
// my server code...
const timerEnd = new Date();
//timerEnd - timerStart (get time in ms)
//send response to client