測試樣例: 執行的一條sql記錄的1w次插入
分兩組: 一組用nginx+pfm 來執行, 一組用swoole 來執行
公平性保證前提: @1.爲了保證公平性, 在nginx裏把 access_log, error_log都關掉. vim nginx.conf 的http模塊 access_log off; error_log off; rewrite_log off; @2. ulimit -n 10240 把進程的可操做文件描述符數調大, 默認才1024(我這每次併發都1k了) 和nginx 裏設置 worker_rlimit_nofile 30000; @3. max_connections=2048 mysql的最大可鏈接數開到2048 @4. php-fpm 數量爲5個, swoole的工做進程也只開5個.
nginx + fpm 組:
測試代碼以下:
測試代碼以下:
<?php $link = mysqli_connect("192.168.0.7","root","root", "testdb") or die(mysql_error()); $sql = "INSERT INTO test (`user_id`,`date`,`fee`) VALUE('100','2018-09-21',300)"; $result = mysqli_query($link, $sql) or die(mysqli_error($link)); ?>
腳本執行的只是一條sql記錄的插入: ab -n 10000 -c 1000 http://proxy.com/press.php [http://proxy.com 就是ip 192.168.0.7的nginx虛擬主機] -n 表示總共執行1w次請求, -c 表示每次併發數, 即每次併發1000條,總共1w條 而後開始執行nginx + php-fpm 組合版的壓測 [root@07 server]# ab -n 10000 -c 1000 http://proxy.com/press.php
Server Software: nginx
Server Hostname: proxy.com
Server Port: 80php
Document Path: /press.php
Document Length: 0 bytesmysql
Concurrency Level: 1000
Time taken for tests: 111.796 seconds
Complete requests: 10000
Failed requests: 1368
(Connect: 0, Receive: 0, Length: 1368, Exceptions: 0)
Write errors: 0
Non-2xx responses: 1368
Total transferred: 1985628 bytes
HTML transferred: 231368 bytes
Requests per second: 89.45 [#/sec] (mean)
Time per request: 11179.624 [ms] (mean)
Time per request: 11.180 [ms] (mean, across all concurrent requests)
Transfer rate: 17.34 [Kbytes/sec] receivednginx
看上面的結果,1w個請求總花費了64s才執行完,但失敗的請求就達到 7777 , 即成功的只有2223個.每秒的併發數只有155個 [nginx沒有打印任何log, 數據庫插入也確實只有2223條記錄]
而後輪到swoole組了 ab -n 10000 -c 1000 http://127.0.0.1:9501/ 完整代碼以下:
<?php // Server class Server { private $serv; public function __construct() { $this->serv = new swoole_server("0.0.0.0", 9501); $this->serv->set(array( 'worker_num' => 5, //fpm只開了5個, swoole也只開5個子進程 'daemonize' => false, )); $this->serv->on('Start', array($this, 'onStart')); $this->serv->on('Connect', array($this, 'onConnect')); $this->serv->on('Receive', array($this, 'onReceive')); $this->serv->on('Close', array($this, 'onClose')); $this->serv->start(); } public function onStart( $serv ) { echo "Start\n"; } public function onConnect( $serv, $fd, $from_id ) { } public function onReceive( swoole_server $serv, $fd, $from_id, $data ) { $link = mysqli_connect("192.168.0.7","root","root", "testdb"); $sql = "INSERT INTO test (`user_id`,`date`,`fee`) VALUE('100','2018-09-21',300)"; $result = mysqli_query($link, $sql) or die(mysqli_error($link)); $serv->close($fd); } public function onClose( $serv, $fd, $from_id ) { } } // 啓動服務器 Start the server $server = new Server(); ?>
sql
數據庫
Server Software: Server
Hostname: 127.0.0.1
Server Port: 9501
Document Path: /
Document Length: 0 bytes
Concurrency Level: 1000
Time taken for tests: 18.626 seconds <----
Complete requests: 10000
Failed requests: 0
Write errors: 0
Total transferred: 0 bytes
HTML transferred: 0 bytes
Requests per second: 536.88 [#/sec] (mean) <---
Time per request: 1862.614 [ms] (mean)
Time per request: 1.863 [ms] (mean, across all concurrent requests)
Transfer rate: 0.00 [Kbytes/sec] received
field | nginx | swoole |
---|---|---|
總耗時 | 111s | 18s |
請求失敗數 | 1368 | 0 |
每秒併發數 | 89 | 536 |