我的node.js/express 应用程序的开头有类似 microtime() 函数的东西。
function microtime (get_as_float) {
// Returns either a string or a float containing the current time in seconds and microseconds
//
// version: 1109.2015
// discuss at: http://phpjs.org/functions/microtime
// + original by: Paulo Freitas
// * example 1: timeStamp = microtime(true);
// * results 1: timeStamp > 1000000000 && timeStamp < 2000000000
var now = new Date().getTime() / 1000;
var s = parseInt(now, 10);
return (get_as_float) ? now : (Math.round((now - s) * 1000) / 1000) + ' ' + s;
}
实际应用程序的代码如下所示:
application.post('/', function(request, response) {
t1 = microtime(true);
//code
//code
response.send(something);
console.log("Time elapsed: " + (microtime(true) - t1));
}
Time elapsed: 0.00599980354309082
我的问题是,这是否意味着从 POST 请求到达服务器到发送响应的时间大约需要 0.005 秒?
我已经在客户端进行了测量,但我的互联网速度非常慢,所以我认为存在一些与应用程序本身无关的延迟。有什么快速简便的方法可以检查请求的处理速度?
最佳答案
无耻的插件在这里。我编写了一个代理来跟踪每个 Express 请求的时间使用情况。
http://blog.notifymode.com/blog/2012/07/17/profiling-express-web-framwork-with-notifymode/
事实上,当我第一次开始编写代理时,我采取了相同的方法。但我很快就意识到这是不准确的。我的实现通过替换 Express 路由器来跟踪请求和响应之间的时间差。这使我能够添加跟踪器功能。欢迎尝试一下。
关于performance - 测量 js 执行时间是否可以判断应用程序响应请求的速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11589402/