初步学习swoole,简单压测
今天只是简单测试一下swoole的基础学习,安装,搭建一个简单的请求文件测试。
aliyun配置
- 服务器系统版本: CentOS 7.2
- PHP版本: 7.2.0
- swoole版本: 4.2.7
- nginx版本: 1.8.0
- 机器配置: 1 vCPU 2 GiB 1Mbsp(测试服务器 有点小)
- 压力测试数据解释连接:https://www.cnblogs.com/myvic/p/7703973.html(感谢该博主!)
- 服务问题:https://blog.csdn.net/qq_38287952/article/details/80561282(感谢该博主!)
阿里云ecs需要开启测试的端口(aliyunECS -> 安全组)
启动服务的时候出现问题
[root@izm5e91wcylu0jb4js001yz ~]# php /usr/local/nginx/html/test/swoolest.php
[2019-09-20 16:57:43 @4727.0] WARNING swSocket_bind (ERROR 502): bind(0.0.0.0:9501) failed. Error: Address already in use [98]
PHP Fatal error: Uncaught Swoole\Exception: failed to listen server port[0.0.0.0:9501]. Error: Address already in use[98]. in /usr/local/nginx/html/test/swoolest.php:3
Stack trace:
#0 /usr/local/nginx/html/test/swoolest.php(3): Swoole\Server->__construct('0.0.0.0', 9501)
#1 {main}
thrown in /usr/local/nginx/html/test/swoolest.php on line 3
Fatal error: Uncaught Swoole\Exception: failed to listen server port[0.0.0.0:9501]. Error: Address already in use[98]. in /usr/local/nginx/html/test/swoolest.php:3
Stack trace:
#0 /usr/local/nginx/html/test/swoolest.php(3): Swoole\Server->__construct('0.0.0.0', 9501)
#1 {main}
thrown in /usr/local/nginx/html/test/swoolest.php on line 3
杀掉占用端口的进程
[root@izm5e91wcylu0jb4js001yz ~]# netstat -anp |grep 9501
tcp 0 0 0.0.0.0:9501 0.0.0.0:* LISTEN 4295/php
tcp 0 0 172.31.3.96:9501 222.85.157.73:2132 ESTABLISHED 4295/php
[root@izm5e91wcylu0jb4js001yz ~]# kill 4295
测试结果
[root@izm5e91wcylu0jb4js001yz /]# ab -c100 -n200 -k http://---.---.---.---/index.html
Concurrency Level: 100
Time taken for tests: 1.637 seconds
Complete requests: 200
Failed requests: 0
Write errors: 0
Keep-Alive requests: 200
Total transferred: 172600 bytes
HTML transferred: 125200 bytes
Requests per second: 122.16 [#/sec] (mean)
Time per request: 818.627 [ms] (mean)
Time per request: 8.186 [ms] (mean, across all concurrent requests)
Transfer rate: 102.95 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 2 1.9 0 5
Processing: 5 494 563.5 205 1621
Waiting: 0 283 289.9 201 614
Total: 6 496 562.0 205 1621
[root@izm5e91wcylu0jb4js001yz /]# ab -c100 -n200 -k http://---.---.---.---/swoolest.php
Concurrency Level: 100
Time taken for tests: 0.016 seconds
Complete requests: 200
Failed requests: 0
Write errors: 0
Keep-Alive requests: 200
Total transferred: 35800 bytes
HTML transferred: 2400 bytes
Requests per second: 12236.16 [#/sec] (mean)
Time per request: 8.172 [ms] (mean)
Time per request: 0.082 [ms] (mean, across all concurrent requests)
Transfer rate: 2138.94 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 2 1.8 0 5
Processing: 2 5 2.4 6 8
Waiting: 0 5 2.4 6 8
Total: 2 7 4.1 8 13
文件代码
php文件:swoolest.php (9501端口)
<?php
$http = new swoole_http_server('0.0.0.0',9501);
//设置
$http->set([
'worker_num' => 2, //工作的进程数量
'max_request' => 1000 //单个进程请求的最大次数
]);
//相关事件的监听
//开始事件
$http->on('start',function(swoole_server $server){
echo '服务启动;\n';
});
// 处理请求事件
// $request 请求对象 服务器请求得到客户发过来的数据对象
// $response 响应对象 服务器向客户发送数据所用的对象
$http->on('request',function(swoole_http_request $request ,swoole_http_response $response){
//加上头信息
$response->header('Content-Type','text/html;charset=utf-8');
$response->end('这是今天的测试信息,成功请求!');
});
//启动服务
$http->start();
html代码(index.html) 这个随意写 ( 80端口 )
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>请求成功!
</h1>
</body>
</html>
测试结果
此次测试没有过多的让服务器增加压力 ,我试过-c100 -n400 服务器太小就有点承受不住了。
[root@izm5e91wcylu0jb4js001yz html]# ab -c100 -n400 -k http://---.---.---.---/swoolest.php
Concurrency Level: 100
Time taken for tests: 1.636 seconds
Complete requests: 400
Failed requests: 0
Write errors: 0
Keep-Alive requests: 400
Total transferred: 71600 bytes
HTML transferred: 4800 bytes
Requests per second: 244.53 [#/sec] (mean)
Time per request: 408.954 [ms] (mean)
Time per request: 4.090 [ms] (mean, across all concurrent requests)
Transfer rate: 42.74 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 1.5 0 5
Processing: 3 216 298.8 5 1619
Waiting: 0 216 298.8 5 1619
Total: 3 217 298.2 8 1619
[root@izm5e91wcylu0jb4js001yz html]# ab -c100 -n400 -k http://--.--.--.--/index.html
Concurrency Level: 100
Time taken for tests: 3.293 seconds
Complete requests: 400
Failed requests: 0
Write errors: 0
Keep-Alive requests: 400
Total transferred: 345200 bytes
HTML transferred: 250400 bytes
Requests per second: 121.49 [#/sec] (mean)
Time per request: 823.145 [ms] (mean)
Time per request: 8.231 [ms] (mean, across all concurrent requests)
Transfer rate: 102.38 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 1.9 0 6
Processing: 0 677 1134.2 11 3274
Waiting: 0 606 1102.3 11 3234
Total: 0 678 1133.6 16 3274
但显示结果依然是swoole胜利。
正常情况下 swoole 超过nginx 5-10倍。
过量情况下 swoole 也能达到2倍。
数据很可观哟,加油 ,继续学习