EC2 t2.micro实例上NodeJS服务器为何比PHP服务器慢一倍?
PHP vs Node.js 性能基准测试异常排查
测试背景
我在EC2 t2.micro实例上对**PHP服务器(Apache+PHP架构)与NodeJS服务器(Node.js 14+Express)**进行性能基准测试,测试内容为:
- 处理GET请求并返回JSON响应
- 接收POST请求并返回对应JSON响应
查阅资料得知NodeJS服务器应比PHP更快,但使用ApacheBench的测试结果显示,PHP的单请求耗时是NodeJS的一半。
基准测试结果
PHP服务器GET端点测试结果
Start testing GET endpoint of PHP server This is ApacheBench, Version 2.3 <$Revision: 1901567 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 3.15.210.36 (be patient) Server Software: Apache/2.4.54 Server Hostname: 3.15.210.36 Server Port: 80 Document Path: /CSE-503S-PERFORMANCE-EVALUATION-STUDY/nodejs_vs_php_server/get.php Document Length: 196 bytes Concurrency Level: 300 Time taken for tests: 1.213 seconds Complete requests: 2000 Failed requests: 0 Non-2xx responses: 2000 Total transferred: 762000 bytes HTML transferred: 392000 bytes Requests per second: 1648.74 [#/sec] (mean) Time per request: 181.957 [ms] (mean) Time per request: 0.607 [ms] (mean, across all concurrent requests) Transfer rate: 613.45 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 8 6.2 4 35 Processing: 8 166 30.8 171 228 Waiting: 1 166 30.8 171 228 Total: 8 174 31.3 179 254 Percentage of the requests served within a certain time (ms) 50% 179 66% 190 75% 194 80% 197 90% 205 95% 212 98% 228 99% 236 100% 254 (longest request)
PHP服务器POST端点测试结果
Start testing POST endpoint of PHP server This is ApacheBench, Version 2.3 <$Revision: 1901567 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 3.15.210.36 (be patient) Server Software: Apache/2.4.54 Server Hostname: 3.15.210.36 Server Port: 80 Document Path: /CSE-503S-PERFORMANCE-EVALUATION-STUDY/nodejs_vs_php_server/post.php Document Length: Variable Concurrency Level: 300 Time taken for tests: 1.218 seconds Complete requests: 2000 Failed requests: 0 Non-2xx responses: 2000 Total transferred: 762000 bytes Total body sent: 496000 HTML transferred: 392000 bytes Requests per second: 1642.23 [#/sec] (mean) Time per request: 182.678 [ms] (mean) Time per request: 0.609 [ms] (mean, across all concurrent requests) Transfer rate: 611.03 [Kbytes/sec] received 397.73 kb/s sent 1008.75 kb/s total Connection Times (ms) min mean[+/-sd] median max Connect: 1 8 4.8 6 37 Processing: 12 166 31.0 176 209 Waiting: 1 166 31.0 176 209 Total: 15 174 29.7 184 225 Percentage of the requests served within a certain time (ms) 50% 184 66% 189 75% 192 80% 194 90% 197 95% 202 98% 209 99% 211 100% 225 (longest request)
NodeJS服务器GET端点测试结果
Start testing GET endpoint of NodeJS server This is ApacheBench, Version 2.3 <$Revision: 1901567 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 3.15.210.36 (be patient) Server Software: Server Hostname: 3.15.210.36 Server Port: 3000 Document Path: / Document Length: 53 bytes Concurrency Level: 300 Time taken for tests: 2.021 seconds Complete requests: 2000 Failed requests: 0 Total transferred: 584000 bytes HTML transferred: 106000 bytes Requests per second: 989.75 [#/sec] (mean) Time per request: 303.107 [ms] (mean) Time per request: 1.010 [ms] (mean, across all concurrent requests) Transfer rate: 282.23 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 23 11.5 20 50 Processing: 46 266 59.4 273 344 Waiting: 36 158 49.7 161 291 Total: 47 289 64.4 305 354 Percentage of the requests served within a certain time (ms) 50% 305 66% 325 75% 331 80% 333 90% 342 95% 344 98% 347 99% 348 100% 354 (longest request)
NodeJS服务器POST端点测试结果
Start testing POST endpoint of NodeJS server This is ApacheBench, Version 2.3 <$Revision: 1901567 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 3.15.210.36 (be patient) Server Software: Server Hostname: 3.15.210.36 Server Port: 3000 Document Path: / Document Length: Variable Concurrency Level: 300 Time taken for tests: 2.220 seconds Complete requests: 2000 Failed requests: 0 Total transferred: 584000 bytes Total body sent: 372000 HTML transferred: 106000 bytes Requests per second: 901.01 [#/sec] (mean) Time per request: 332.961 [ms] (mean) Time per request: 1.110 [ms] (mean, across all concurrent requests) Transfer rate: 256.93 [Kbytes/sec] received 163.66 kb/s sent 420.59 kb/s total Connection Times (ms) min mean[+/-sd] median max Connect: 0 20 12.6 17 56 Processing: 20 303 89.1 310 616 Waiting: 20 160 65.3 164 383 Total: 25 322 90.3 323 653 Percentage of the requests served within a certain time (ms) 50% 323 66% 346 75% 385 80% 392 90% 406 95% 462 98% 466 99% 467 100% 653 (longest request)
服务器代码
NodeJS服务器代码
const express = require('express'); const cors = require('cors'); const app = express(); const port = 3000; app.use(cors()); app.use(express.json()); app.use(express.urlencoded({ extended: true })); app.get('/', (req, res) => { res.json({ status: 200, first_name: "Quan", last_name: "Khuc" }) }); app.post('/', (req, res) => { const { first_name, last_name } = req.body; res.json({ status: 200, first_name: first_name, last_name: last_name }) }); app.listen(port, () => { console.log(`Example app listening at http://3.15.210.36:${port}`) });
PHP服务器代码
GET端点
<?php ini_set("session.cookie_httponly", 1); header("Content-Type: application/json"); echo json_encode(array( "first_name" => "Quan", "last_name" => "Khuc", "status" => 200 )); exit; ?>
POST端点
<?php header("Content-Type: application/json"); $data_str = file_get_contents('php://input'); $data_obj = json_decode($data_str, true); $first_name = $data_obj['first_name']; $last_name = $data_obj['last_name']; echo json_encode(array( 'status' => 200, 'first_name' => $first_name, 'last_name' => $last_name )); $stmt->close(); ?>
测试脚本
echo 'Start testing GET endpoint of PHP server' ab -c 300 -n 2000 -r http://3.15.210.36/CSE-503S-PERFORMANCE-EVALUATION-STUDY/nodejs_vs_php_server/get.php echo '------------------------------------------------------------------------------------------------------------------' echo 'Start testing POST endpoint of PHP server' ab -p data.json -T application/json -c 300 -n 2000 -r -l http://3.15.210.36/CSE-503S-PERFORMANCE-EVALUATION-STUDY/nodejs_vs_php_server/post.php echo '------------------------------------------------------------------------------------------------------------------' echo 'Start testing GET endpoint of NodeJS server' ab -c 300 -n 2000 -r http://3.15.210.36:3000/ echo '------------------------------------------------------------------------------------------------------------------' echo 'Start testing POST endpoint of NodeJS server' ab -p data.json -T application/json -c 300 -n 2000 -r -l http://3.15.210.36:3000/ echo '------------------------------------------------------------------------------------------------------------------'
问题
我的测试可能存在哪些问题?
内容的提问来源于stack exchange,提问作者Quan Khuc
相关产品推荐
相关产品推荐

