Nodejs仅4个远程HTTP请求同时执行

时间:2019-03-21 20:17:15

标签: node.js concurrency threadpool libuv

我正在尝试使用下面的NodeJS代码执行多个同时请求,但是从输出中我可以看到下面的请求以4束为单位执行。
为什么会这样?

我也将默认的libuv线程池大小更改为7。

const http = require("http");
const rq = require("request");

http.createServer(function(req, res) {
  res.write("Server started");
  var start = process.hrtime();
  rq("https://stream101.herokuapp.com/5.php", {
    pool: {
      maxSockets: 20
    }
  }, (err, res, body) => {
    if (res) {
      var end = process.hrtime(start);
      console.log(body);
      console.log("in time : %ds", end[0] + end[1] / 1e9);
    }
  });
  res.end();
}).listen(8081);

这里的stream101.herokuapp.com仅在睡眠5秒后回显内容。

这是我的节点应用程序启动的方式:

set UV_THREADPOOL_SIZE=7 && node index.js      

节点控制台输出:

Streaming after 5 seconds
in time : 6.075168388s
Streaming after 5 seconds
in time : 6.079874785s
Streaming after 5 seconds
in time : 6.077171028s
Streaming after 5 seconds
in time : 6.420050492s

Streaming after 5 seconds
in time : 10.966390463s
Streaming after 5 seconds
in time : 10.967194215s
Streaming after 5 seconds
in time : 10.998825902s
Streaming after 5 seconds
in time : 11.42817129s

Streaming after 5 seconds
in time : 16.353070153s
Streaming after 5 seconds
in time : 16.440429564s
Streaming after 5 seconds
in time : 16.643850903s
Streaming after 5 seconds
in time : 16.624362216s

上述测试用例的Apache基准命令:

ab -n12 -c5 http://localhost:8081/

因为libuv线程池可用于heruko dns查询,所以与libuv线程池有关吗?

0 个答案:

没有答案