安装swoole
下载安装最新版本swoole 4.2.13
# 下载
wget https://github.com/swoole/swoole-src/archive/v4.2.13.tar.gz
# 解压进入
tar -xzvf v4.2.13.tar.gz
cd swoole-src-4.2.13
# 编译参数:
./configure --with-php-config=/usr/local/php/bin/php-config --enable-openssl --enable-http2 --enable-sockets --enable-mysqlnd
# 安装
make & make install
# 加入php.ini
extension=swoole.so
# php.ini disable_functions中去掉以下函数
proc_open, proc_get_status
安装压测工具webbench
局域网内另外一台服务器上安装webbench
# 安装ctag先
yum install ctags
wget http://blog.s135.com/soft/linux/webbench/webbench-1.5.tar.gz
tar zxvf webbench-1.5.tar.gz
cd webbench-1.5
make && make install
执行webbench --help
[root@li1205-86 webbench-1.5]# webbench --help
webbench [option]... URL
-f|--force Don't wait for reply from server.
-r|--reload Send reload request - Pragma: no-cache.
-t|--time <sec> Run benchmark for <sec> seconds. Default 30.
-p|--proxy <server:port> Use proxy server for request.
-c|--clients <n> Run <n> HTTP clients at once. Default one.
-9|--http09 Use HTTP/0.9 style requests.
-1|--http10 Use HTTP/1.0 protocol.
-2|--http11 Use HTTP/1.1 protocol.
--get Use GET request method.
--head Use HEAD request method.
--options Use OPTIONS request method.
--trace Use TRACE request method.
-?|-h|--help This information.
-V|--version Display program version.
目录结构
- 创建项目目录tsr_test
- composer安装 "respect/validation"
- 创建swooloe_server.php和normal.php
composer init
composer require "respect/validation"
编写常规代码 normal.php
<?php
/**
* Created by PhpStorm.
* User: shyandsy
* Date: 2/21/2019
* Time: 1:45 AM
*/
require_once(__DIR__ . '/vendor/autoload.php');
use Respect\Validation\Validator as V;
try{
//$this
$validator = V::create()
->key('user', V::email())
->key('pass', V::alnum()->length(8, 32))
->key('token', V::alnum()->length(1));
$validator->assert($_GET);
$email = (string)$_GET['user'];
$pass = (string)$_GET['pass'];
$token = (string)$_GET['token'];
}catch(Respect\Validation\Exceptions\NestedValidationException $e){
}
echo 111;
编写swoole版本
<?php
/**
* Created by PhpStorm.
* User: shyandsy
* Date: 2/21/2019
* Time: 1:44 AM
*/
require_once(__DIR__ . '/vendor/autoload.php');
use Respect\Validation\Validator as V;
$http = new swoole_http_server("127.0.0.1", 9000);
$http->on('start', function ($server) {
echo "Swoole http server is started at http://127.0.0.1:9000\n";
});
$http->on('request', function ($request, $response) {
try{
//$this
$validator = V::create()
->key('user', V::email())
->key('pass', V::alnum()->length(8, 32))
->key('token', V::alnum()->length(1));
$validator->assert($request->get);
$email = (string)$request->get['user'];
$pass = (string)$request->get['pass'];
$token = (string)$request->get['token'];
}catch(Respect\Validation\Exceptions\NestedValidationException $e){
}
$response->header("Content-Type", "text/plain");
$response->end("111\n");
});
$http->start();
测试swoole_server.php
启动swoole_server
php swooloe_server.php
- 500并发60秒测试
webbench -c 500 -t 60 http://x.x.x.x:9000/
[root@li1205-86 ~]# webbench -c 500 -t 60 http://x.x.x.x:9000/
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Benchmarking: GET http://x.x.x.x:9000/
500 clients, running 60 sec.
Speed=286088 pages/min, 724756 bytes/sec.
Requests: 286088 susceed, 0 failed.
- 1000并发60秒测试
webbench -c 1000 -t 60 http://x.x.x.x:9000/
[root@li1205-86 ~]# webbench -c 1000 -t 60 http://x.x.x.x:9000/
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Benchmarking: GET http://x.x.x.x:9000/
1000 clients, running 60 sec.
Speed=272317 pages/min, 689869 bytes/sec.
Requests: 272317 susceed, 0 failed.
- 2000并发60秒测试
webbench -c 2000 -t 60 http://x.x.x.x:9000/
[root@li1205-86 ~]# webbench -c 2000 -t 60 http://x.x.x.x:9000/
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Benchmarking: GET http://x.x.x.x:9000/
2000 clients, running 60 sec.
Speed=271270 pages/min, 687214 bytes/sec.
Requests: 271270 susceed, 0 failed.
测试normal.php
nginx中把ip访问直接导向normal.php
- 100并发60秒测试
webbench -c 100 -t 60 http://x.x.x.x/
[root@li1205-86 ~]# webbench -c 100 -t 60 http://x.x.x.x/
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Benchmarking: GET http://x.x.x.x/
100 clients, running 60 sec.
Speed=10104 pages/min, 26775 bytes/sec.
Requests: 10104 susceed, 0 failed.
- 500并发60秒测试
webbench -c 500 -t 60 http://x.x.x.x/
502 Bad Gateway.
数据汇总
- swoole_server.php
client(N) | time(sec) | page/min | bytes/sec |
---|---|---|---|
500 | 60 | 286088 | 724756 |
1000 | 60 | 272317 | 689869 |
2000 | 60 | 271270 | 687214 |
- normal.php
client(N) | time(sec) | page/min | bytes/sec |
---|---|---|---|
100 | 60 | 10104 | 26775 |
500 | - | - | - |
- 对比
client(N) | swoole_server.php page/min | normal.php page/min | ratio(swooloe/normal) |
---|---|---|---|
500(100) | 286088 | 10104 | 10.6848 |
- 总结
- 本测试中swoole_server.php没有经过nginx代理,相比normal.php少一个环节
- swoole_server.php只需要一次加载respect/validator,而normal每次都要加载。本测试中仅仅是加载了respect/validator,实际项目中我们会用到很多组件,每次重复加载都会耗费cpu,swoole的优势会更加明显。
这个文章必须顶。。