这些天,我对实现多处理的nodejs集群模块感兴趣。
这是有助于解释我的问题的代码。
process.env.UV_THREADPOOL_SIZE = 1;
const cluster = require('cluster');
if(cluster.isMaster){
cluster.fork();
cluster.fork();
} else {
const express = require('express');
const crypto = require('crypto');
const app = express();
app.get('/', (req, res) => {
crypto.pbkdf2('a', 'b', 100000, 512, 'sha512', () => {
res.send('benchmark test');
});
});
app.listen(3000, () => {
console.log('listen on port 3000');
});
}
为简单起见,我将UV_THREADPOOL_SIZE
设置为1(默认设置为4)
我正在使用2017 macbook pro,它是具有4个线程(逻辑内核)的双核计算机
和该代码中的pbkdf2
函数大约需要1秒钟在我的计算机上进行处理。 (更准确地说是970ms)
我正在使用apache基准测试性能,但是有些困惑。
使用命令
ab -c 4 -n 4 localhost:3000/
Time taken for tests: 1.785 seconds
Complete requests: 4
Failed requests: 0
Total transferred: 840 bytes
HTML transferred: 44 bytes
Requests per second: 2.24 [#/sec] (mean)
Time per request: 1785.403 [ms] (mean)
Time per request: 446.351 [ms] (mean, across all concurrent requests)
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 1769 1779 7.1 1783 1785
Waiting: 1769 1779 7.3 1783 1785
Total: 1769 1779 7.1 1783 1785
我的计算机有两个核心(物理核心),并且有两个nodejs实例(集群)。
所以我认为一个核心可以采用一个实例。
因此,当四个请求同时发生时,
我假设必须处理前2个请求,并且必须处理后2个请求。
但是同时处理了4个请求,最小和最大连接时间几乎相同。
任何建议都将不胜感激。