测试结果和预期不符

    创建一个简单的 Express 服务器,并使用 cluster 模块来充分利用多核 CPU

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