0

问题描述

使用 wrk 压测,设置 1 客户情况下压测正常完成,若客户数量大于 1,则工作进程一致处于 IO 等待状态,响应极慢。

补充1

发现进程启动时也存在相同的状况,比如触发onWorkerStart事件后要等待5秒才在shell中回显。
经过核查,在onWorkerStart中的业务代码耗时只耗时0.015s
通过strace发现在IO操作时发生协程切换导致耗时5s

3334  14:40:09.088417 <... close resumed> ) = 0 <0.000405>
3334  14:40:09.088537 lstat("/home/vagrant/code/nxAdmin/vendor/composer/../nhzex/tp-swoole/src/Resetters2/RebindHttpContainer.php", {st_mode=S_IFREG|0775, st_size=516, ...}) = 0 <0.000118>
3334  14:40:09.088751 openat(AT_FDCWD, "/home/vagrant/code/nxAdmin/vendor/nhzex/tp-swoole/src/Resetters2/RebindHttpContainer.php", O_RDONLY <unfinished ...>
3334  14:40:14.217150 <... openat resumed> ) = 15 <5.128368>
3334  14:40:14.217250 fstat(15,  <unfinished ...>
3334  14:40:14.217306 <... fstat resumed> {st_mode=S_IFREG|0775, st_size=516, ...}) = 0 <0.000045>
3334  14:40:14.217405 fstat(15,  <unfinished ...>
3334  14:40:14.217436 <... fstat resumed> {st_mode=S_IFREG|0775, st_size=516, ...}) = 0 <0.000020>

================ 原问题 ===============

环境

PHP 7.2.18-1+ubuntu18.04.1+deb.sury.org+1
Swoole 4.3.4 & 4.3.3

Swoole Server Http 设置

Runtime::enableCoroutine(true);
[
    'daemonize' => false,
    'dispatch_mode' => 2,
    'worker_num' => 4,
    'enable_coroutine' => true,

    'task_worker_num' => 2,
    'task_enable_coroutine' => true,

    'pid_file' => App::getRuntimePath() . 'swoole.pid',
    'log_file' => App::getRuntimePath() . 'swoole.log',

    'enable_static_handler' => true,
    'document_root' => App::getRootPath() . 'public',

    'heartbeat_check_interval' => 60,
    'heartbeat_idle_time' => 1200,

    'package_max_length' => 20 * 1024 * 1024,
    'buffer_output_size' => 10 * 1024 * 1024,
    'socket_buffer_size' => 128 * 1024 * 1024,

    'send_yield' => true,
    'reload_async' => true,
]

压测正常状态

wrk --latency --timeout 3s -t1 -c1 -d30s http://127.0.0.1:9505/
Running 30s test @ http://127.0.0.1:9505/
  1 threads and 1 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    10.70ms    3.44ms  39.65ms   87.14%
    Req/Sec    94.73     19.41   121.00     79.67%
  Latency Distribution
     50%    9.39ms
     75%   10.28ms
     90%   15.44ms
     99%   24.84ms
  2838 requests in 30.08s, 5.43MB read
Requests/sec:     94.34
Transfer/sec:    184.90KB

压测失败状态

wrk --latency --timeout 3s -t4 -c4 -d30s http://127.0.0.1:9505/
Running 30s test @ http://127.0.0.1:9505/
  4 threads and 4 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    70.38ms  391.57ms   2.76s    97.96%
    Req/Sec    18.50     30.50    70.00     71.43%
  Latency Distribution
     50%   14.13ms
     75%   15.84ms
     90%   17.15ms
     99%    2.76s 
  58 requests in 30.07s, 113.68KB read
  Socket errors: connect 0, read 0, write 0, timeout 9
Requests/sec:      1.93
Transfer/sec:      3.78KB

strace 详情

[pid 12317] 10:48:30.016398 openat(AT_FDCWD, "/home/vagrant/code/project/runtime/temp/77fce4e169dad1de2ddeb9e87d2045b2eec20baa.php", O_RDONLY) = 30 <5.100331>
[pid 12317] 10:48:35.116888 fstat(30, {st_mode=S_IFREG|0775, st_size=2273, ...}) = 0 <0.000064>
......
[pid 12317] 10:48:35.152471 openat(AT_FDCWD, "/home/vagrant/code/project/runtime/temp/77fce4e169dad1de2ddeb9e87d2045b2eec20baa.php", O_RDONLY) = 30 <5.089026>
[pid 12317] 10:48:40.241569 fstat(30, {st_mode=S_IFREG|0775, st_size=2273, ...}) = 0 <0.000012>
......
[pid 12317] 10:48:50.503502 stat("/home/vagrant/code/project/app/view/dispatch_jump.blade.php", {st_mode=S_IFREG|0775, st_size=2133, ...}) = 0 <0.000085>
[pid 12317] 10:48:50.503701 stat("/home/vagrant/code/project/runtime/temp//77fce4e169dad1de2ddeb9e87d2045b2eec20baa.php", {st_mode=S_IFREG|0775, st_size=2273, ...}) = 0 <0.000062>
[pid 12317] 10:48:50.503939 openat(AT_FDCWD, "/home/vagrant/code/project/runtime/temp/77fce4e169dad1de2ddeb9e87d2045b2eec20baa.php", O_RDONLY) = 30 <0.001014>
[pid 12317] 10:48:50.505123 fstat(30, {st_mode=S_IFREG|0775, st_size=2273, ...}) = 0 <0.000106>

可以看到在opena函数执行耗时达5s

nhzex 2
2019-06-14 提问
2 个回答
0

已采纳

问题定位,是虚拟机与windows文件共享用的nfs性能限制,导致文件IO过慢。

1

其实另外一个问题、、、你在tp下用swoole、、、有个问题、TP下用swoole协程,是有问题的、、存在上下文安全问题、我建议、、你还是用专用的框架,比如EasySwoole框架

撰写答案

推广链接