压测connections 最大只有2048

yifenbushe

使用workerman压测脚本测试gateway,只有2048个connections,这就是极限了吗?已经按照要求安装扩展和优化内核,环境是centos8/php8

1086 4 0
4个回答

liziyu

mark

  • 暂无评论
walkor

贴下压测脚本

  • yifenbushe 2022-03-22

    [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个链接
    if ($count++ >= 2000) return;
    // 建立异步链接
    $con = new AsyncTcpConnection('ws://127.0.0.1:8282');
    $con->onConnect = function($con)
    {
    // 递归调用connect
    connect();
    };
    $con->onMessage = function($con, $msg)
    {
    echo "recv $msg\n";
    };
    $con->onClose = function($con)
    {
    echo "con close\n";
    };
    // 当前链接每10秒发个心跳包
    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 2022-03-22
    // 2000个链接
    if ($count++ >= 2000) return;

    你贴的压测脚本里限制了,最多发出2000连接。

  • yifenbushe 2022-03-22

    多谢walkor老师,这么晚了还没休息回答问题,赞一个!!!
    现在数量上来,但是connections一直在1.5万和3千左右跳动变化,这个正常吗

  • yifenbushe 2022-03-22

    补充下资源占用情况
    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

  • yifenbushe 2022-03-22

    还有优化空间吗

  • walkor 2022-03-23

    配置好的话gatewayWorker单机支持10万连接很轻松。
    但是压测脚本最多能发出2万左右的连接,因为压测脚本发起连接会消耗本地端口,默认本机端口就大概2万可用。

walkor

业务逻辑

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个链接
    if ($count++ >= 20000) return;
    // 建立异步链接
    $con = new AsyncTcpConnection('ws://127.0.0.1:5555');
    $con->onConnect = function($con)
    {
        // 递归调用connect
        connect();
    };
    $con->onMessage = function($con, $msg)
    {
        //echo "recv $msg\n";
    };
    $con->onClose = function($con)
    {
        echo "con close\n";
    };
    // 当前链接每10秒发个心跳包
    Timer::add(10, function()use($con)
    {
        $con->send("ping");
    });
    $con->connect();
    echo $count, " connections complete\n";

}
Worker::runAll();

截图
截图
截图

我这1核2G轻服务器127.0.0.1压测轻松2万连接,维持这2万连接cpu占用2%左右,稳定无波动。

  • yifenbushe 2022-03-23

    有做其他优化吗?Event.php按照您这么写完,还是原来那个样,connections再1万上下浮动特别大,感觉不稳定

  • walkor 2022-03-23

    没做其它优化

  • yifenbushe 2022-03-23

    能说下您服务系统及PHP版本吗?不行我重装下

  • walkor 2022-03-23
    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核 内存: 2GB
walkor

压测有问题继续在这里回复追加提问,不要新开帖子。
https://www.workerman.net/doc/workerman/debug/busy-process.html 按照手册strace下busy的进程,贴下系统调用。

  • yifenbushe 2022-03-24

    按照老师的配置,把event 所有代码注释掉,只留onConnect()代码,个人PC开启的虚拟机,2核2g内存,稳定跑出2.8万 connections。算是一个阶段胜利吧,虽然是理想状态,多谢老师指导!!!!
    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]

年代过于久远,无法发表回答
🔝