【问题标题】:Requests per second in http.get() - Node.jshttp.get() 中的每秒请求数 - Node.js
【发布时间】:2012-09-24 15:51:16
【问题描述】:

我在node.js做一个超级基础的http请求应用。

var http = require('http');

var options = {
  host: 'www.domain-here.com',
  port: 80,
  path: '/index.html'
};

for(var i = 0; i < 500; i++) {
    http.get(options, function(res) {
            console.log("[" + this.count + "] Response: " + res.statusCode);
    }.bind({ count: i })).on('error', function(e) {
            console.log("[" + this.count + "] Error: " + e.message);
    }.bind({ count: i }));
}

不过,我需要获取每秒的 http 请求数。知道我如何每秒获取请求吗?

【问题讨论】:

    标签: javascript node.js


    【解决方案1】:
    // begin timestamp
    var begin = new Date();
    
    for(var i = 0; i < 500; i++) {
        http.get(options, function(res) {
                console.log("[" + this.count + "] Response: " + res.statusCode);
        }.bind({ count: i })).on('error', function(e) {
                console.log("[" + this.count + "] Error: " + e.message);
        }.bind({ count: i }));
    }
    
    // end timestamp
    var end = new Date();
    
    // Then, you need a simple calculation
    var reqPerSec = i / ( end - begin );
    

    【讨论】:

      【解决方案2】:

      使用Date 对象记录时间并在之后分析数字。

      如果您需要实时数据,也可以将setInterval 与计数器变量结合使用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-23
        • 2018-03-23
        • 2016-10-22
        • 1970-01-01
        • 2023-03-20
        • 1970-01-01
        • 1970-01-01
        • 2016-03-18
        相关资源
        最近更新 更多