使用workerman壓測腳本測試gateway,只有2048個(gè)connections,這就是極限了嗎?已經(jīng)按照要求安裝擴(kuò)展和優(yōu)化內(nèi)核,環(huán)境是centos8/php8
貼下壓測腳本
[root@VM-0-16-centos GatewayWorker]# cat vendor/test.php
<?php
require __DIR__ . '/workerman/workerman/Autoloader.php';
use Workerman\Worker;
use Workerman\Lib\Timer;
use Workerman\Connection\AsyncTcpConnection;
$worker = new Worker();
$worker->onWorkerStart = 'connect';
function connect()
{
static $count = 0;
// 2000個(gè)鏈接
if ($count++ >= 2000) return;
// 建立異步鏈接
$con = new AsyncTcpConnection('ws://127.0.0.1:8282');
$con->onConnect = function($con)
{
// 遞歸調(diào)用connect
connect();
};
$con->onMessage = function($con, $msg)
{
echo "recv $msg\n";
};
$con->onClose = function($con)
{
echo "con close\n";
};
// 當(dāng)前鏈接每10秒發(fā)個(gè)心跳包
Timer::add(10, function()use($con)
{
$con->send("ping");
});
$con->connect();
echo $count, " connections complete\n";
}
Worker::runAll();
?>
[root@VM-0-16-centos GatewayWorker]#
多謝walkor老師,這么晚了還沒休息回答問題,贊一個(gè)?。?!
現(xiàn)在數(shù)量上來,但是connections一直在1.5萬和3千左右跳動(dòng)變化,這個(gè)正常嗎
補(bǔ)充下資源占用情況
top - 23:50:39 up 6:28, 3 users, load average: 3.24, 2.22, 1.05
Tasks: 130 total, 4 running, 126 sleeping, 0 stopped, 0 zombie
%Cpu0 : 28.2 us, 19.9 sy, 0.0 ni, 45.5 id, 0.3 wa, 0.7 hi, 5.3 si, 0.0 st
%Cpu1 : 24.8 us, 65.4 sy, 0.0 ni, 4.4 id, 0.0 wa, 1.0 hi, 4.4 si, 0.0 st
MiB Mem : 3736.8 total, 1149.5 free, 1010.3 used, 1577.0 buff/cache
MiB Swap: 0.0 total, 0.0 free, 0.0 used. 2444.0 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
76296 root 20 0 423056 140392 5144 R 83.7 3.7 0:03.15 php
74723 root 20 0 318372 36112 5368 R 8.6 0.9 0:32.51 php
74722 root 20 0 316156 33856 5368 S 8.3 0.9 0:31.50 php
74724 root 20 0 316156 33860 5368 R 8.3 0.9 0:30.81 php
74725 root 20 0 316156 33856 5368 S 8.0 0.9 0:31.09 php
74718 root 20 0 296804 14360 4984 S 4.3 0.4 0:14.81 php
74719 root 20 0 296804 14360 4984 S 4.3 0.4 0:14.86 php
74720 root 20 0 296804 14360 4984 S 4.3 0.4 0:14.75 php
74721 root 20 0 296804 14360 4984 S 4.0 0.4 0:14.73 php
配置好的話gatewayWorker單機(jī)支持10萬連接很輕松。
但是壓測腳本最多能發(fā)出2萬左右的連接,因?yàn)閴簻y腳本發(fā)起連接會(huì)消耗本地端口,默認(rèn)本機(jī)端口就大概2萬可用。
業(yè)務(wù)邏輯
class Events
{
public static function onConnect($client_id)
{
Gateway::sendToClient($client_id, "Hello $client_id\r\n");
}
}
壓測腳本
<?php
require __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
use Workerman\Lib\Timer;
use Workerman\Connection\AsyncTcpConnection;
$worker = new Worker();
$worker->onWorkerStart = 'connect';
function connect()
{
static $count = 0;
// 20000個(gè)鏈接
if ($count++ >= 20000) return;
// 建立異步鏈接
$con = new AsyncTcpConnection('ws://127.0.0.1:5555');
$con->onConnect = function($con)
{
// 遞歸調(diào)用connect
connect();
};
$con->onMessage = function($con, $msg)
{
//echo "recv $msg\n";
};
$con->onClose = function($con)
{
echo "con close\n";
};
// 當(dāng)前鏈接每10秒發(fā)個(gè)心跳包
Timer::add(10, function()use($con)
{
$con->send("ping");
});
$con->connect();
echo $count, " connections complete\n";
}
Worker::runAll();
我這1核2G輕服務(wù)器127.0.0.1壓測輕松2萬連接,維持這2萬連接cpu占用2%左右,穩(wěn)定無波動(dòng)。
有做其他優(yōu)化嗎?Event.php按照您這么寫完,還是原來那個(gè)樣,connections再1萬上下浮動(dòng)特別大,感覺不穩(wěn)定
PHP 7.4.3 (cli) (built: Mar 2 2022 15:36:52) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies
Ubuntu Server 20.04 LTS 64bit
CPU: 1核 內(nèi)存: 2GB
壓測有問題繼續(xù)在這里回復(fù)追加提問,不要新開帖子。
http://wtbis.cn/doc/workerman/debug/busy-process.html 按照手冊strace下busy的進(jìn)程,貼下系統(tǒng)調(diào)用。
按照老師的配置,把event 所有代碼注釋掉,只留onConnect()代碼,個(gè)人PC開啟的虛擬機(jī),2核2g內(nèi)存,穩(wěn)定跑出2.8萬 connections。算是一個(gè)階段勝利吧,雖然是理想狀態(tài),多謝老師指導(dǎo)!?。。?br />
root@gateway:/var/www/html/GatewayWorker# php start.php status
Workerman[start.php] status
----------------------------------------------GLOBAL STATUS----------------------------------------------------
Workerman version:4.0.27 PHP version:7.4.3
start time:2022-03-24 06:17:29 run 0 days 0 hours
load average: 1.71, 1.4, 0.76 event-loop:\Workerman\Events\Event
3 workers 9 processes
worker_name exit_status exit_count
YourAppBusinessWorker 0 0
YourAppGateway 0 0
Register 0 0
----------------------------------------------PROCESS STATUS---------------------------------------------------
pid memory listening worker_name connections send_fail timers total_request qps status
4122 4M none YourAppBusinessWorker 5 0 0 171279 0 [idle]
4123 4M none YourAppBusinessWorker 5 0 0 168504 0 [idle]
4124 4M none YourAppBusinessWorker 5 0 0 172958 0 [idle]
4125 4M none YourAppBusinessWorker 5 0 0 174293 0 [idle]
4126 52M websocket://0.0.0.0:8282 YourAppGateway 8296 0 0 187990 0 [idle]
4127 60M websocket://0.0.0.0:8282 YourAppGateway 9822 0 0 237910 0 [idle]
4128 32M websocket://0.0.0.0:8282 YourAppGateway 5219 0 0 99483 0 [idle]
4129 30M websocket://0.0.0.0:8282 YourAppGateway 4915 0 0 89428 0 [idle]
4130 2M text://0.0.0.0:1238 Register 8 0 0 8 0 [idle]
----------------------------------------------PROCESS STATUS---------------------------------------------------
Summary 192M - - 28280 0 0 1301853 0 [Summary]
root@gateway:/var/www/html/GatewayWorker# php start.php status
Workerman[start.php] status
----------------------------------------------GLOBAL STATUS----------------------------------------------------
Workerman version:4.0.27 PHP version:7.4.3
start time:2022-03-24 06:17:29 run 0 days 0 hours
load average: 1.71, 1.4, 0.76 event-loop:\Workerman\Events\Event
3 workers 9 processes
worker_name exit_status exit_count
YourAppBusinessWorker 0 0
YourAppGateway 0 0
Register 0 0
----------------------------------------------PROCESS STATUS---------------------------------------------------
pid memory listening worker_name connections send_fail timers total_request qps status
4122 4M none YourAppBusinessWorker 5 0 0 172405 0 [idle]
4123 4M none YourAppBusinessWorker 5 0 0 169656 0 [idle]
4124 4M none YourAppBusinessWorker 5 0 0 174138 0 [idle]
4125 4M none YourAppBusinessWorker 5 0 0 175488 0 [idle]
4126 52M websocket://0.0.0.0:8282 YourAppGateway 8296 0 0 189590 0 [idle]
4127 60M websocket://0.0.0.0:8282 YourAppGateway 9822 0 0 239152 0 [idle]
4128 32M websocket://0.0.0.0:8282 YourAppGateway 5219 0 0 100391 0 [idle]
4129 30M websocket://0.0.0.0:8282 YourAppGateway 4915 0 0 90327 0 [idle]
4130 2M text://0.0.0.0:1238 Register 8 0 0 8 0 [idle]
----------------------------------------------PROCESS STATUS---------------------------------------------------
Summary 192M - - 28280 0 0 1311155 0 [Summary]