测试结果和预期不符
创建一个简单的 Express 服务器,并使用 cluster
模块来充分利用多核 CPU
const express = require('express');
const cluster = require('cluster');
const os = require('os');
const PORT = 3000;
// Mock 数据,模拟从数据库或其他资源获取的数据
const fetchData = () => {
return "Hello, High Concurrency!";
};
const app = express();
app.get('/', (req, res) => {
const data = fetchData();
res.send(data);
});
if (cluster.isMaster) {
// 当主进程被调用时,我们会为每个核心创建一个子进程
const numCPUs = os.cpus().length;
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker) => {
console.log(`Worker ${worker.process.pid} died`);
cluster.fork(); // Create a new worker if one dies
});
} else {
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
}
PS C:\Users\AINUC\Desktop\23.08> node .\server4.js
Server is running on port 3000
Server is running on port 3000
Server is running on port 3000
Server is running on port 3000
Server is running on port 3000
Server is running on port 3000
Server is running on port 3000
Server is running on port 3000
PS C:\Users\AINUC\Desktop\23.08> loadtest -c 100 -n 100000 http://localhost:3000/
[Mon Aug 14 2023 14:25:51 GMT+0800 (中国标准时间)] INFO Requests: 0 (0%), requests per second: 0, mean latency: 0 ms
[Mon Aug 14 2023 14:25:56 GMT+0800 (中国标准时间)] INFO Requests: 10778 (11%), requests per second: 2154, mean latency: 46 ms
[Mon Aug 14 2023 14:26:01 GMT+0800 (中国标准时间)] INFO Requests: 20459 (20%), requests per second: 1945, mean latency: 52 ms
[Mon Aug 14 2023 14:26:06 GMT+0800 (中国标准时间)] INFO Requests: 32156 (32%), requests per second: 2337, mean latency: 42.8 ms
[Mon Aug 14 2023 14:26:11 GMT+0800 (中国标准时间)] INFO Requests: 44187 (44%), requests per second: 2406, mean latency: 41.6 ms
[Mon Aug 14 2023 14:26:16 GMT+0800 (中国标准时间)] INFO Requests: 56323 (56%), requests per second: 2427, mean latency: 41.2 ms
[Mon Aug 14 2023 14:26:21 GMT+0800 (中国标准时间)] INFO Requests: 68295 (68%), requests per second: 2399, mean latency: 41.7 ms
[Mon Aug 14 2023 14:26:26 GMT+0800 (中国标准时间)] INFO Requests: 80059 (80%), requests per second: 2353, mean latency: 42.5 ms
[Mon Aug 14 2023 14:26:31 GMT+0800 (中国标准时间)] INFO Requests: 92542 (93%), requests per second: 2497, mean latency: 40 ms
[Mon Aug 14 2023 14:26:34 GMT+0800 (中国标准时间)] INFO
[Mon Aug 14 2023 14:26:34 GMT+0800 (中国标准时间)] INFO Target URL: http://localhost:3000/
[Mon Aug 14 2023 14:26:34 GMT+0800 (中国标准时间)] INFO Max requests: 100000
[Mon Aug 14 2023 14:26:34 GMT+0800 (中国标准时间)] INFO Concurrency level: 100
[Mon Aug 14 2023 14:26:34 GMT+0800 (中国标准时间)] INFO Agent: none
[Mon Aug 14 2023 14:26:34 GMT+0800 (中国标准时间)] INFO
[Mon Aug 14 2023 14:26:34 GMT+0800 (中国标准时间)] INFO Completed requests: 100000
[Mon Aug 14 2023 14:26:34 GMT+0800 (中国标准时间)] INFO Total errors: 0
[Mon Aug 14 2023 14:26:34 GMT+0800 (中国标准时间)] INFO Total time: 42.7582621 s
[Mon Aug 14 2023 14:26:34 GMT+0800 (中国标准时间)] INFO Requests per second: 2339
[Mon Aug 14 2023 14:26:34 GMT+0800 (中国标准时间)] INFO Mean latency: 42.7 ms
[Mon Aug 14 2023 14:26:34 GMT+0800 (中国标准时间)] INFO
[Mon Aug 14 2023 14:26:34 GMT+0800 (中国标准时间)] INFO Percentage of the requests served within a certain time
[Mon Aug 14 2023 14:26:34 GMT+0800 (中国标准时间)] INFO 50% 41 ms
[Mon Aug 14 2023 14:26:34 GMT+0800 (中国标准时间)] INFO 90% 53 ms
[Mon Aug 14 2023 14:26:34 GMT+0800 (中国标准时间)] INFO 95% 57 ms
[Mon Aug 14 2023 14:26:34 GMT+0800 (中国标准时间)] INFO 99% 75 ms
[Mon Aug 14 2023 14:26:34 GMT+0800 (中国标准时间)] INFO 100% 232 ms (longest request)
const express = require('express');
// const cluster = require('cluster');
// const os = require('os');
const PORT = 3000;
// Mock 数据,模拟从数据库或其他资源获取的数据
const fetchData = () => {
return "Hello, High Concurrency!";
};
const app = express();
app.get('/', (req, res) => {
const data = fetchData();
res.send(data);
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
// if (cluster.isMaster) {
// // 当主进程被调用时,我们会为每个核心创建一个子进程
// const numCPUs = os.cpus().length;
// for (let i = 0; i < numCPUs; i++) {
// cluster.fork();
// }
// cluster.on('exit', (worker) => {
// console.log(`Worker ${worker.process.pid} died`);
// cluster.fork(); // Create a new worker if one dies
// });
// } else {
// app.listen(PORT, () => {
// console.log(`Server is running on port ${PORT}`);
// });
// }
PS C:\Users\AINUC\Desktop\23.08> node .\server4.js
Server is running on port 3000
PS C:\Users\AINUC\Desktop\23.08> loadtest -c 100 -n 100000 http://localhost:3000/
[Mon Aug 14 2023 14:24:25 GMT+0800 (中国标准时间)] INFO Requests: 0 (0%), requests per second: 0, mean latency: 0 ms
[Mon Aug 14 2023 14:24:30 GMT+0800 (中国标准时间)] INFO Requests: 11578 (12%), requests per second: 2315, mean latency: 43.1 ms
[Mon Aug 14 2023 14:24:35 GMT+0800 (中国标准时间)] INFO Requests: 23698 (24%), requests per second: 2425, mean latency: 41.2 ms
[Mon Aug 14 2023 14:24:40 GMT+0800 (中国标准时间)] INFO Requests: 35721 (36%), requests per second: 2404, mean latency: 41.6 ms
[Mon Aug 14 2023 14:24:45 GMT+0800 (中国标准时间)] INFO Requests: 47837 (48%), requests per second: 2423, mean latency: 41.3 ms
[Mon Aug 14 2023 14:24:50 GMT+0800 (中国标准时间)] INFO Requests: 59907 (60%), requests per second: 2418, mean latency: 41.3 ms
[Mon Aug 14 2023 14:24:55 GMT+0800 (中国标准时间)] INFO Requests: 72616 (73%), requests per second: 2539, mean latency: 39.4 ms
[Mon Aug 14 2023 14:25:00 GMT+0800 (中国标准时间)] INFO Requests: 85385 (85%), requests per second: 2556, mean latency: 39.1 ms
[Mon Aug 14 2023 14:25:05 GMT+0800 (中国标准时间)] INFO Requests: 97826 (98%), requests per second: 2485, mean latency: 40.2 ms
[Mon Aug 14 2023 14:25:06 GMT+0800 (中国标准时间)] INFO
[Mon Aug 14 2023 14:25:06 GMT+0800 (中国标准时间)] INFO Target URL: http://localhost:3000/
[Mon Aug 14 2023 14:25:06 GMT+0800 (中国标准时间)] INFO Max requests: 100000
[Mon Aug 14 2023 14:25:06 GMT+0800 (中国标准时间)] INFO Concurrency level: 100
[Mon Aug 14 2023 14:25:06 GMT+0800 (中国标准时间)] INFO Agent: none
[Mon Aug 14 2023 14:25:06 GMT+0800 (中国标准时间)] INFO
[Mon Aug 14 2023 14:25:06 GMT+0800 (中国标准时间)] INFO Completed requests: 100000
[Mon Aug 14 2023 14:25:06 GMT+0800 (中国标准时间)] INFO Total errors: 0
[Mon Aug 14 2023 14:25:06 GMT+0800 (中国标准时间)] INFO Total time: 40.919846299999996 s
[Mon Aug 14 2023 14:25:06 GMT+0800 (中国标准时间)] INFO Requests per second: 2444
[Mon Aug 14 2023 14:25:06 GMT+0800 (中国标准时间)] INFO Mean latency: 40.9 ms
[Mon Aug 14 2023 14:25:06 GMT+0800 (中国标准时间)] INFO
[Mon Aug 14 2023 14:25:06 GMT+0800 (中国标准时间)] INFO Percentage of the requests served within a certain time
[Mon Aug 14 2023 14:25:06 GMT+0800 (中国标准时间)] INFO 50% 40 ms
[Mon Aug 14 2023 14:25:06 GMT+0800 (中国标准时间)] INFO 90% 50 ms
[Mon Aug 14 2023 14:25:06 GMT+0800 (中国标准时间)] INFO 95% 53 ms
[Mon Aug 14 2023 14:25:06 GMT+0800 (中国标准时间)] INFO 99% 59 ms
[Mon Aug 14 2023 14:25:06 GMT+0800 (中国标准时间)] INFO 100% 74 ms (longest request)