有很多人推薦使用php-fpm的UnixSocket的方式,嘗試測試之
簡單准備了一個測試頁面,包含一些循環、判斷、加減、以及常用的phpinfo
- <?php
- function make_seed() {
- list($usec,$sec)=explode(' ',microtime());
- return(float) $sec+((float)$usec*100000);
- }
-
- $i=0;
- $k=0;
- while($i < 1000){
- $i++;
- mt_srand(make_seed());
- if($i%2 == 0)
- $k += mt_rand();
- else
- $k -= mt_rand();
- }
- echo $k;
-
- phpinfo();
- ?>
測試方法:
- 在同網段找一台服務器
- 使用webbench來測試
- ./webbench -c 500 -t 30 http://ip/cgi-bin/Liv.php
-
- cat nginx-access.log | grep -v 200 | grep WebBench | wc -l
- 檢查返回內容的穩定性
測試結果(每種測試3次,每次都重啟nginx和php-fpm):
Tcp socket的情況
- 異常返回:4
- Speed=14502 pages/min, 11660916 bytes/sec.
- Requests: 7251 susceed, 0 failed.
- 異常返回:0
- Speed=14480 pages/min, 11644002 bytes/sec.
- Requests: 7240 susceed, 0 failed.
- 異常返回:1 (http 499 客戶端斷開連接)
- Speed=14542 pages/min, 11693312 bytes/sec.
- Requests: 7271 susceed, 0 failed.
Unix socket的情況(放在/dev/shm中)
- 異常返回:388
- Speed=15194 pages/min, 11600151 bytes/sec.
- Requests: 7596 susceed, 1 failed.
- 異常返回:271
- Speed=15020 pages/min, 11644336 bytes/sec.
- Requests: 7510 susceed, 0 failed.
- 異常返回:831
- Speed=16110 pages/min, 11625129 bytes/sec.
- Requests: 8054 susceed, 1 failed.
Unix socket的情況(放在/tmp中)
- 異常返回:230
- Speed=14972 pages/min, 11632380 bytes/sec.
- Requests: 7459 susceed, 27 failed.
- 異常返回:647
- Speed=15804 pages/min, 11687138 bytes/sec.
- Requests: 7898 susceed, 4 failed.
- 異常返回:698
- Speed=15944 pages/min, 11685551 bytes/sec.
- Requests: 7972 susceed, 0 failed.
針對上面的測試結果,簡單總結一下,未必客觀,僅供參考:
- UnixSocket性能比TcpSocket最多高不超過10%
- UnixSocket的異常率比TcpSocket至少高226倍
- UnixSocket返回異常時,Http Code 502,php-fpm返回11: Resource temporarily unavailable,google後嘗試修改backlog,但結果無明顯變化
-
- 結論:在查明UnixSocket異常原因之前,建議依然使用TcpSocket(即默認的127.0.0.1:9000方式)