概述
收到讀者提問 “使用 Swoole 開發的群聊功能,想知道并發情況,也就是想壓測下 QPS,一直未找到方法 ...”
對 swoole_http_server 壓測,咱們可以使用 Apache 的 ab 命令,
對 swoole_websocket_server 壓測,使用 ab 命令是不能壓測的,我從網上一直也沒找到合適的方法,看官方提供的代碼 benchmark/async.php 中,使用的異步模塊 swoole\http\client 方法進行壓測的,但在 Swoole 4.3 版本就移除了異步模塊,讓使用 Coroutine 協程模塊,
在本地我用 Coroutine 協程實作了一下, 測的差不多的時候,一直不確定是否正確,就在 segmentfault 發了個提問,沒想到韓老師回答了,'如果的如果'老師也回答了,非常感謝兩位老師的答案,然后整理出文章分享給大家,
測驗機
Mac 上安裝的 Parallels Desktop 虛擬機
系統:Ubuntu 16.04.3 LTS
記憶體:
數量:1
核數:2
CPU:
數量:1
大小: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();
壓測腳本
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();
壓測結果
第 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
以上是壓測結果,供參考,這里我還準備了一分學習圖和資料,如下:

鏈接:https://pan.baidu.com/s/1v5gm7n0L7TGyejCmQrMh2g 提取碼:x2p5
免費分享,但是X度限制嚴重,如若鏈接失效點擊鏈接或搜索加群 群號518475424,
小結
通過這個壓測結果,表明 Swoole 的執行效率是杠杠的!
當然還有一些引數是可以調優的,比如:worker_num、max_request、task_worker_num 等,
在真實的業務場景中,肯定會有邏輯處理,也會使用到 MySQL、Redis,
那么問題來了,前兩篇文章已經分享了,Swoole Redis 連接池、Swoole MySQL 連接池,感興趣的朋友,可以使用上兩種連接池,然后再進行壓測,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/121030.html
標籤:PHP
