swoole / swoole-src

🚀 Coroutine-based concurrency library for PHP
https://www.swoole.com
Apache License 2.0
18.48k stars 3.16k forks source link

V6多线程压测性能不理想 #5455

Closed LIngMax closed 3 months ago

LIngMax commented 3 months ago

   // $server = new Swoole\WebSocket\Server('0.0.0.0', 4455,SWOOLE_PROCESS);
  $server = new Swoole\WebSocket\Server("0.0.0.0",4455,SWOOLE_THREAD );

  'worker_num'            => 1,#CPU核数的1-4倍 默认值:CPU 核数 Swoole 6多线程版本
  $t=$xi =0;

  $server->on('request', function ($request, $response){

        global $t, $xi,$server;
        $xi++;
        if($xi%10000==0){
            $t1 =microtime(true);
            $m = $t1-$t+0.01;
            echo "进程id   push:".$xi." 秒:".$m."  平均速率:".round($xi/$m,2)." \n";
            $t = $t1;
            $xi = 0;
        }
        @$response->end("啊哈哈哈哈");return;

  });

压测

ab -n 40000 -c 200 "http://127.0.0.1:4455/"

SWOOLE_PROCESS 同样配置 达到3w QPS

SWOOLE_THREAD 8k QPS

差了接近4倍

增加worker_num 数量也是同样结果

怎么调整参数也是差不多 有什么好的优化建议吗

https://github.com/jingjingxyk/swoole-cli/releases/download/swoole-cli-v0.0.9/swoole-cli-v6.0.0-dev-zts-linux-x64.tar.xz

matyhtf commented 3 months ago

未发生性能问题。

测试代码


$server = new Swoole\Http\Server("0.0.0.0", 4455, SWOOLE_BASE);

$server->set([
    'worker_num' => 4, #CPU核数的1-4倍 默认值:CPU 核数 Swoole 6多线程版本
]);

$t = $xi = 0;

$server->on('request', function ($request, $response) {
    global $t, $xi, $server;
    $xi++;
    if ($xi % 10000 == 0) {
        $t1 = microtime(true);
        $m = $t1 - $t + 0.01;
        echo "进程id =" . $server->getWorkerId() . "   push:" . $xi . " 秒:" . $m . "  平均速率:" . round($xi / $m, 2) . " \n";
        $t = $t1;
        $xi = 0;
    }
    $response->end("啊哈哈哈哈");
});

$server->start();

BASE 模式

ab -n 40000 -c 200 "http://127.0.0.1:4455/"
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 4000 requests
Completed 8000 requests
Completed 12000 requests
Completed 16000 requests
Completed 20000 requests
Completed 24000 requests
Completed 28000 requests
Completed 32000 requests
Completed 36000 requests
Completed 40000 requests
Finished 40000 requests

Server Software:        swoole-http-server
Server Hostname:        127.0.0.1
Server Port:            4455

Document Path:          /
Document Length:        15 bytes

Concurrency Level:      200
Time taken for tests:   2.940 seconds
Complete requests:      40000
Failed requests:        0
Total transferred:      6520000 bytes
HTML transferred:       600000 bytes
Requests per second:    13604.04 [#/sec] (mean)
Time per request:       14.702 [ms] (mean)
Time per request:       0.074 [ms] (mean, across all concurrent requests)
Transfer rate:          2165.49 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    7   1.7      6      18
Processing:     2    8   2.1      8      27
Waiting:        1    6   1.9      5      26
Total:          8   15   2.6     14      34

Percentage of the requests served within a certain time (ms)
  50%     14
  66%     15
  75%     15
  80%     16
  90%     18
  95%     20
  98%     22
  99%     24
 100%     34 (longest request)

THREAD 模式

ab -n 40000 -c 200 "http://127.0.0.1:4455/"
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 4000 requests
Completed 8000 requests
Completed 12000 requests
Completed 16000 requests
Completed 20000 requests
Completed 24000 requests
Completed 28000 requests
Completed 32000 requests
Completed 36000 requests
Completed 40000 requests
Finished 40000 requests

Server Software:        swoole-http-server
Server Hostname:        127.0.0.1
Server Port:            4455

Document Path:          /
Document Length:        15 bytes

Concurrency Level:      200
Time taken for tests:   2.985 seconds
Complete requests:      40000
Failed requests:        0
Total transferred:      6520000 bytes
HTML transferred:       600000 bytes
Requests per second:    13401.56 [#/sec] (mean)
Time per request:       14.924 [ms] (mean)
Time per request:       0.075 [ms] (mean, across all concurrent requests)
Transfer rate:          2133.26 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    7   2.0      7      16
Processing:     2    8   2.2      7      20
Waiting:        1    5   1.9      5      16
Total:          9   15   2.1     15      33

Percentage of the requests served within a certain time (ms)
  50%     15
  66%     15
  75%     16
  80%     16
  90%     17
  95%     19
  98%     20
  99%     21
 100%     33 (longest request)
matyhtf commented 3 months ago

检查你的代码,如果是大量使用了 线程资源,造成了严重的争抢,确实会导致性能下降。

LIngMax commented 3 months ago

检查你的代码,如果是大量使用了 线程资源,造成了严重的争抢,确实会导致性能下降。

我用你上面的代码 重新测了一下 华为云 4核

SWOOLE_BASE 31878.96 [#/sec] (mean)

SWOOLE_THREAD 13263.89 [#/sec] (mean)

接近3倍差距 测试结果

LIngMax commented 3 months ago

注意 部分虚拟机上限就在 3000-15000左右 QPS

切换高性能机器马上暴露 WSL1.0 swoole 3600 低端云服务器swoole 10000 真机服务器swoole 34000

真机Nginx 42000