Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用enable_static_handler下载静态文件大量套接字错误 (macos下,wrk压测吞吐率低) #4639

Closed
ttc0419 opened this issue Mar 3, 2022 · 5 comments
Labels

Comments

@ttc0419
Copy link

ttc0419 commented Mar 3, 2022

  1. What did you do? If possible, provide a simple script for reproducing the error.
    server.php:
<?php
use Swoole\Http\Server;
use Swoole\Http\Request;
use Swoole\Http\Response;

const ADDRESS = '127.0.0.1';
const PORT = 8080;

Co::set(['hook_flags' => SWOOLE_HOOK_ALL]);
$server = new Server(ADDRESS, PORT, SWOOLE_PROCESS);
$server->set([
    'enable_coroutine' => true,
    'enable_static_handler' => true,
    'document_root' =>  __DIR__ . DIRECTORY_SEPARATOR . 'public'
]);

$server->on('start', function () {
    echo "Server started on http://" . ADDRESS . ':' . PORT . PHP_EOL;
});

$server->on('request', function (Request $request, Response $response) {
});

$server->start();

使用wrk压测结果:

[william@mbp-2020 benchmark]% wrk -t 2 -d 30s http://localhost:8080/css/bootstrap.min.css
Running 30s test @ http://localhost:8080/css/bootstrap.min.css
  2 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     3.22ms   28.63ms 492.25ms   99.09%
    Req/Sec     3.71k     1.76k    4.86k    81.82%
  16313 requests in 30.08s, 2.52GB read
  Socket errors: connect 0, read 16313, write 6, timeout 0
Requests/sec:    542.34
Transfer/sec:     85.82MB

但是如果使用on request方式传输文件,就没有报错并且性能更好。

<?php
use Swoole\Http\Server;
use Swoole\Http\Request;
use Swoole\Http\Response;

const ADDRESS = '127.0.0.1';
const PORT = 8080;

Co::set(['hook_flags' => SWOOLE_HOOK_ALL]);
$server = new Server(ADDRESS, PORT, SWOOLE_BASE);
$server->set([
    'enable_coroutine' => true,
    'enable_static_handler' => false
]);

$server->on('start', function () {
    echo "Server started on http://" . ADDRESS . ':' . PORT . PHP_EOL;
});

$server->on('request', function (Request $request, Response $response) {
    $response->end(file_get_contents("bootstrap.min.css"));
});

$server->start();

使用wrk压测结果:

[william@mbp-2020 william]% wrk -t 2 -d 30s http://localhost:8080/bootstrap.min.css 
Running 30s test @ http://localhost:8080/bootstrap.min.css
  2 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     2.16ms  248.25us   5.61ms   89.81%
    Req/Sec     2.31k   144.78     4.39k    93.84%
  138110 requests in 30.10s, 21.34GB read
Requests/sec:   4588.05
Transfer/sec:    725.88MB
  1. What did you expect to see?
    起码10K?

  2. What did you see instead?
    542.34,wrk输出显示有大量的套接字读取错误,通过htop观察,前期CPU占有率很高之后就几乎为0

  3. What version of Swoole are you using (show your php --ri swoole)?

swoole

Swoole => enabled
Author => Swoole Team <team@swoole.com>
Version => 4.8.7
Built => Feb 23 2022 10:56:25
coroutine => enabled with boost asm context
debug => enabled
trace_log => enabled
kqueue => enabled
rwlock => enabled
curl-native => enabled
pcre => enabled
zlib => 1.2.11
brotli => E16777225/D16777225
async_redis => enabled

Directive => Local Value => Master Value
swoole.enable_coroutine => On => On
swoole.enable_library => On => On
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => On => On
swoole.unixsock_buffer_size => 262144 => 262144
  1. What is your machine environment used (show your uname -a & php -v & gcc -v) ?
Darwin mbp-2020 21.3.0 Darwin Kernel Version 21.3.0: Wed Jan  5 21:37:58 PST 2022; root:xnu-8019.80.24~20/RELEASE_X86_64 x86_64

PHP 8.1.3 (cli) (built: Feb 18 2022 09:32:50) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.3, Copyright (c) Zend Technologies with Zend OPcache v8.1.3, Copyright (c), by Zend Technologies

Apple clang version 13.0.0 (clang-1300.0.29.30)
Target: x86_64-apple-darwin21.3.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
@ttc0419 ttc0419 changed the title 使用enable_static_handler下载静态文件速率低下 使用enable_static_handler下载静态文件大量套接字错误 Mar 4, 2022
@NathanFreeman
Copy link
Member

因为开启了enable_static_handler之后,遇到匹配的请求是直接在reactor线程就直接返回结果了,请求并不会下发到worker进程。
所以就没用到协程,并发会低一点。

@ttc0419
Copy link
Author

ttc0419 commented Mar 5, 2022

@NathanFreeman 所以这个结果是正常的嘛?我看所有套接字错误应该都在几秒后产生的,前期CPU利用率都很高,后期几乎没有占有率,应该不正常吧?

@NathanFreeman
Copy link
Member

能上传一下套接字的错误信息吗,谢谢,我这边看看

@ttc0419
Copy link
Author

ttc0419 commented Mar 5, 2022

@NathanFreeman 在第一份wrk输出里,Socket errors: connect 0, read 16313, write 6, timeout 0

matyhtf pushed a commit that referenced this issue Mar 7, 2022
matyhtf pushed a commit that referenced this issue Mar 7, 2022
@NathanFreeman NathanFreeman changed the title 使用enable_static_handler下载静态文件大量套接字错误 使用enable_static_handler下载静态文件大量套接字错误 (macos下,wrk压测吞吐率低) Mar 8, 2022
@matyhtf matyhtf closed this as completed Mar 16, 2022
@ttc0419
Copy link
Author

ttc0419 commented Mar 18, 2022

@matyhtf 这个issue还有一个问题还没有解决。使用swoole 4.8.8和php 8.1.4的情况下静态文件吞吐量还是低下。
@NathanFreeman 在merge request中建议继续open等待问题完全解决。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants