首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP Swoole vs Node.js 性能實測:ab 壓測數據對比分析

     編輯:PHP 相關知識

測試背景

團隊技術負責人宣稱 Swoole 使 PHP 具備多線程、異步非阻塞能力,性能可媲美 Node.js。為驗證該結論,筆者在本地環境進行了基準壓測。

測試環境

  • CPU: Intel Core i7 2.7 GHz
  • 內存: 16 GB DDR3 1600 MHz
  • 系統: macOS
  • 工具: ApacheBench (ab)
  • 並發: 100
  • 總請求: 5,000

測試代碼邏輯

兩側均實現極簡 HTTP 服務,僅返回 "Hello World",無業務邏輯、無數據庫、無 I/O,純測框架吞吐能力。

壓測結果數據

1. PHP Swoole (端口 8000)

Server Software:
Server Hostname: 127.0.0.1
Server Port: 8000
Document Path: /
Document Length: 0 bytes
Concurrency Level: 100
Time taken for tests: 1.411 seconds
Complete requests: 5000
Failed requests: 0
Requests per second: 3543.00 [#/sec] (mean)
Time per request: 28.225 [ms] (mean)
Time per request: 0.282 [ms] (mean, across all concurrent requests)
Transfer rate: 38.29 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       0    3   1.2      3       7
Processing:   17   25  16.7     23     143
Waiting:       1    9  12.9      7     136
Total:        20   28  16.6     26     143

Percentage of requests served within a certain time (ms)
 50%   26
 66%   27
 75%   27
 80%   27
 90%   28
 95%   29
 98%  141
 99%  142
100%  143 (longest request)

2. Node.js (端口 1337)

Server Software:
Server Hostname: 127.0.0.1
Server Port: 1337
Document Path: /
Document Length: 12 bytes
Concurrency Level: 100
Time taken for tests: 1.215 seconds
Complete requests: 5000
Failed requests: 0
Requests per second: 4116.49 [#/sec] (mean)
Time per request: 24.293 [ms] (mean)
Time per request: 0.243 [ms] (mean, across all concurrent requests)
Transfer rate: 454.26 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       0    0   0.6      0       5
Processing:    2   24  21.0     21     175
Waiting:       2   24  20.8     21     174
Total:         6   24  20.9     21     175

Percentage of requests served within a certain time (ms)
 50%   21
 66%   22
 75%   23
 80%   24
 90%   26
 95%   32
 98%  159
 99%  170
100%  175 (longest request)

關鍵指標對比

指標SwooleNode.js差異
總耗時1.411 s1.215 sNode.js 快 14%
QPS (吞吐率)3,5434,116Node.js 高 16%
平均延遲28.2 ms24.3 msNode.js 低 14%
P99 延遲142 ms170 msSwoole 更穩定
最大延遲143 ms175 msSwoole 更穩定

結果分析與結論

  1. Node.js 在純計算/簡單響應場景下略勝一籌,得益於 V8 引擎優化與事件循環模型成熟度。
  2. Swoole 表現優異,已達同量級:QPS 3500+ 遠超傳統 PHP-FPM(通常僅 300-800),證明 C 級擴展賦予的異步非阻塞能力確立有效。
  3. 長尾延遲 Swoole 更優:P99/P100 顯著低於 Node.js,說明 Swoole 在高並發下抖動更小,適合對尾部延遲敏感的業務。
  4. 測試局限性: Swoole 以 --debug 模式運行(產生大量日誌輸出),且未啟用 OpCache、未調優進程數、未使用 HTTP 服務器優化選項。生產級調優後性能仍有提升空間。
  5. 技術選型建議: 不應單純以「滅掉 Node.js」為目標。Swoole 最大價值在於讓 PHP 團隊用熟悉的語言實現高性能網絡服務(WebSocket、TCP/UDP 服務、異步任務、毫秒定時器等),避免多技術棧維護成本。

後續將在公司測試環境(Linux、生產級配置)進行複測,並補充內存佔用、CPU 佔用、長連接穩定性等維度數據。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved