【问题标题】:Is measuring js execution time a way to tell how quickly the app is responding to requests?测量 js 执行时间是一种判断应用程序响应请求速度的方法吗?
【发布时间】:2012-08-16 19:22:41
【问题描述】:

在我的 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));
}

经过的时间:0.00599980354309082

我的问题是,这是否意味着从 POST 请求到达服务器到发出响应的时间大约为 0.005 秒?

我已经在客户端对其进行了测量,但我的互联网速度很慢,所以我认为存在一些与应用程序本身无关的延迟。有什么方法可以快速简便地检查请求的处理速度?

【问题讨论】:

  • 这只是告诉你处理函数所需的时间很短。要完成整个过程,您必须在第一个输入处挂钩 express 并登录 express 流程循环的最终退出。您可能可以为此使用 express.logger,或者您可能希望为此编写自己的中间件提供程序。
  • 在heroku上托管时可以做这些事情吗?
  • 我不知道你可以在heroku上配置什么,对不起

标签: performance node.js heroku express


【解决方案1】:

这里无耻的插头。我编写了一个代理来跟踪每个 Express 请求的时间使用情况。

http://blog.notifymode.com/blog/2012/07/17/profiling-express-web-framwork-with-notifymode/

事实上,当我第一次开始编写代理时,我采用了相同的方法。但很快我就意识到这并不准确。我的实现通过替换 Express 路由器来跟踪请求和响应之间的时间差。这让我可以添加跟踪器功能。随意尝试一下。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 2014-11-09
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    相关资源
    最近更新 更多