huang-yi / shadowfax

Run Laravel on Swoole.
MIT License
352 stars 35 forks source link

wrk 多次压测性能逐渐下降 #45

Closed cexll closed 4 years ago

cexll commented 4 years ago

我又来提问啦, 今天压测api发现, 接口性能多次压测会逐渐下降, 压测接口为 helloWorld 单字符串, 为了重现, 我下载了最新的

这是api.php

Route::get('test', function () {
    return 'HelloWorld';
});

这是 shadowfax.yml 配置

name: shadowfax
type: http
host: 127.0.0.1
port: 1215
mode: process
access_log: true
app_pool_capacity: 10

server:
  worker_num: 2
  enable_coroutine: false
...
下面的没有改动

这里列上三次压测结果, (均为连续压测

第一次
Running 10s test @ http://localhost:1215/api/test
  10 threads and 1000 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.10s   568.27ms   2.00s    57.44%
    Req/Sec   106.35    109.94     0.89k    87.24%
  4583 requests in 10.03s, 8.28MB read
  Socket errors: connect 0, read 0, write 0, timeout 1888
  Non-2xx or 3xx responses: 4524
Requests/sec:    457.00
Transfer/sec:    845.78KB
第二次
Running 10s test @ http://localhost:1215/api/test
  10 threads and 1000 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     0.00us    0.00us   0.00us    -nan%
    Req/Sec    37.33     32.67   232.00     75.14%
  1425 requests in 10.02s, 2.60MB read
  Socket errors: connect 0, read 0, write 0, timeout 1425
  Non-2xx or 3xx responses: 1425
Requests/sec:    142.23
Transfer/sec:    266.06KB
第三次
Running 10s test @ http://localhost:1215/api/test
  10 threads and 1000 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     0.00us    0.00us   0.00us    -nan%
    Req/Sec    28.96     27.29   150.00     81.82%
  587 requests in 10.02s, 1.07MB read
  Socket errors: connect 0, read 0, write 0, timeout 587
  Non-2xx or 3xx responses: 587
Requests/sec:     58.58
Transfer/sec:    109.61KB
cexll commented 4 years ago
name: shadowfax
type: http
host: 127.0.0.1
port: 1215
mode: base
access_log: true
app_pool_capacity: 10

server:
  worker_num: 2
  enable_coroutine: true

如果我把 mode 设置为 base 性能会高很多, 但是也会下降 ( 哦, 对咯, coroutine开启性能提高很多

开启coroutine压测结果

1000 -> 700 -> 600 -> 500 -> 400 
huang-yi commented 4 years ago

我估计你测试的时候没有去掉session相关的中间件,Laravel默认使用file作为session的驱动,所以你的测试基本上就耗在创建这些session文件了,没有意义。我之前写的博文《Shadowfax: 让Laravel佩上Swoole之剑》里开头就有对压测的描述。

文件操作是非常耗资源的,你运行压测时的每一个请求都会去创建一个session文件,如此频繁性能下降也是必然的。如果你非要测试文件操作的性能,建议启用swoole的coroutine,以及一键协程化,这样性能会好很多。

cexll commented 4 years ago

我估计你测试的时候没有去掉session相关的中间件,Laravel默认使用file作为session的驱动,所以你的测试基本上就耗在创建这些session文件了,没有意义。我之前写的博文《Shadowfax: 让Laravel佩上Swoole之剑》里开头就有对压测的描述。

文件操作是非常耗资源的,你运行压测时的每一个请求都会去创建一个session文件,如此频繁性能下降也是必然的。如果你非要测试文件操作的性能,建议启用swoole的coroutine,以及一键协程化,这样性能会好很多。

好的, 谢谢解答,我去学习一下