Overview
receive reader questions "Using Swoole development group chat feature and want to know concurrency, also just wanted to pressure measured at QPS, the method has not found ..."
to swoole_http_server pressure measurement, we can use Apache's ab command.
For swoole_websocket_server pressure measurement, using the ab command pressure is not measured, I never did find a way from the Internet, look at the code official benchmark of / async.php, the use of asynchronous modules swoole \ http \ client pressure measurement methods but in Swoole version 4.3 to remove the asynchronous module, so use coroutine coroutine module.
I implemented locally by Coroutine coroutine a moment, about the same time measurement, has been uncertain whether the right, in segmentfault made a question, I did not expect the Korean teacher answered, 'if, if' the teacher answered, thank you very much two teachers answer, and then sort out the article for everyone to share.
Tester
installed on Mac Parallels Desktop virtual machine
system: Ubuntu 16.04.3 LTS
Memory:
640 wx_fmt = PNG?
Quantity: 1
Audit: 2
the CPU:
640 wx_fmt = PNG?
Quantity: 1
Size: 2G
Server 代码 <?php class Server { private $serv; public function __construct() { $this->serv = new Swoole\WebSocket\Server("0.0.0.0", 9501); $this->serv->set([ 'task_worker_num' => 10, 'enable_coroutine' => true, 'task_enable_coroutine' => true ]); $this->serv->on('open', function ($serv, $request) {}); $this->serv->on('message', function ($serv, $frame) { $serv->task($frame->data); }); $this->serv->on('task', function ($serv, $task) { foreach ($serv->connections as $fd) { $connectionInfo = $serv->connection_info($fd); if (isset($connectionInfo['websocket_status']) && intval($connectionInfo['websocket_status']) == 3) { $serv->push($fd, $task->data); } } }); $this->serv->on('finish', function ($serv, $task_id, $data) {}); $this->serv->on('close', function ($serv, $fd) {}); $this->serv->start(); } } $server = new Server();
Pressure test script
class Test { protected $concurrency; //并发量 protected $request; //请求量 protected $requested = 0; protected $start_time; function __construct() { $this->concurrency = 100; $this->request = 10000; } protected function webSocket() { go(function () { for ($c = 1; $c <= $this->concurrency; $c++ ) { $cli = new \Swoole\Coroutine\Http\Client('127.0.0.1', 9501); $cli->set(['websocket_mask' => false]); $ret = $cli->upgrade('/'); if ($ret) { $i = $this->request / $this->concurrency; while ($i >= 1) { $this->push($cli); $cli->recv(); $i--; } } } $this->finish(); }); } protected function push($cli) { $ret = $cli->push('Hello World'); if ($ret === true) { $this->requested ++ ; } } protected function finish() { $cost_time = round(microtime(true) - $this->start_time, 4); echo "Concurrency:".$this->concurrency.PHP_EOL; echo "Request num:".$this->request.PHP_EOL; echo "Success num:".$this->requested.PHP_EOL; echo "Total time:".$cost_time.PHP_EOL; echo "Request per second:" . intval($this->request / $cost_time).PHP_EOL; } public function run() { $this->start_time = microtime(true); $this->webSocket(); } } $test = new Test(); $test->run();
Pressure test results
第 1 次: Concurrency:100 Request num:10000 Success num:10000 Total time:0.846 Request per second:11820 第 2 次: Concurrency:100 Request num:10000 Success num:10000 Total time:0.9097 Request per second:10992 第 3 次: Concurrency:100 Request num:10000 Success num:10000 Total time:0.903 Request per second:11074
These are the result of pressure measurement for reference.
Summary
Through this pressure test results show the efficiency Swoole is Leverage!
Of course, there are some parameters can be tuned, for example: worker_num, max_request, task_worker_num like.
In a real business scenario, there will certainly be a logical process, also used to MySQL, Redis.
So the question is, has shared the first two articles, Swoole Redis connection pool, Swoole MySQL connection pool, interested students can use the two connection pool, and then the pressure test.