javascript - http.get() 中的每秒请求数 - Node.js

标签 javascript node.js

我正在 node.js 中做一个 super 基本的 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 请求数。知道我如何每秒获取请求吗?

最佳答案

// 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 );

关于javascript - http.get() 中的每秒请求数 - Node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12560645/

相关文章:

javascript - 必须在 css 文件中使用 PHP 运行计数器

node.js - fs.mkdir 和 fs.rename 的 NodeJs 方法链接

javascript - 关闭按钮未显示在 Bootstrap 可关闭警报上,但功能正常

javascript - 如何将 Base64 String 转换为 javascript 文件对象,就像从文件输入表单一样?

javascript - 如何保证旧数据在 d3 的退出选择中

javascript - 为什么我需要写 JSON.parse(JSON.stringify(data))?

javascript - Node.js - 从 MySQL 查询返回值

node.js - 如何在 nodejs 中使用 avro 模式验证 json,它可以返回发生数据错误的错误列表

node.js - npm create-react-app 错误 : EACCES: permission denied. 该操作被您的操作系统拒绝

javascript - 配置中的 devServer 代理抛出 404