测试样例:
执行的一条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: 80
Document Path: /press.php
Document Length: 0 bytes
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] received
看上面的结果,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 "; } 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(); ?>
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
来总结两者的对比就会发现swoole的强大得有点可怕 ...
field | nginx | swoole |
---|---|---|
总耗时 | 111s | 18s |
请求失败数 | 1368 | 0 |
每秒并发数 | 89 | 536 |