baoshu
V2EX  ›  问与答

用了 Swoole,请求有时候 600 多 ms ,如何优化呢?

  •  
  •   baoshu · Jan 30, 2020 · 2693 views
    This topic created in 2300 days ago, the information mentioned may be changed or developed.

    Swoole 代码如下,仅仅是输出了 Hello World

    //创建 Server 对象,监听 127.0.0.1:9501 端口
    $serv = new Swoole\Server("127.0.0.1", 9501); 
    
    //监听连接进入事件
    $serv->on('Connect', function ($serv, $fd) {  
        echo "Client: Connect.\n";
    });
    
    //监听数据接收事件
    $serv->on('Receive', function ($serv, $fd, $from_id, $data) {
        $serv->send($fd, "Server: ".$data);
    });
    
    //监听连接关闭事件
    $serv->on('Close', function ($serv, $fd) {
        echo "Client: Close.\n";
    });
    
    //启动服务器
    $serv->start(); 
    

    然后用浏览器查看请求耗时,发现大部分是 60ms 左右。但是有时候高达 600 多 ms,太不正常的。 访问是用外网 IP+端口,访问的。 服务器用的腾讯云,系统是 Linux CentOS。

    RT: aaaa0192951.png

    有没有大佬知道如何优化的哈。

    8 replies    2020-01-31 20:07:49 +08:00
    baoshu
        1
    baoshu  
    OP
       Jan 30, 2020
    服务器是 1 核 2G 的。
    iamfirst
        2
    iamfirst  
       Jan 30, 2020 via iPhone
    和网络环境有关吧
    eryueyu
        3
    eryueyu  
       Jan 30, 2020 via iPhone
    查服务器性能占用,网络环境
    KasuganoSoras
        4
    KasuganoSoras  
       Jan 30, 2020
    你贴的这段代码和你截图里的根本不一样吧。。
    baoshu
        5
    baoshu  
    OP
       Jan 30, 2020
    @KasuganoSoras
    确实是

    ~~~
    <?php
    $http = new swoole_http_server("0.0.0.0", 9501);

    $http->on("start", function ($server) {
    echo "Swoole http server is started at http://127.0.0.1:9501\n";
    });

    $http->on("request", function ($request, $response) {
    $response->header("Content-Type", "text/plain");
    $response->end("Hello World\n");
    });

    $http->start();
    ~~~
    baoshu
        6
    baoshu  
    OP
       Jan 30, 2020
    @eryueyu @iamfirst
    大部分时候都是 30-60ms,极少数情况是 600ms 左右。(出现频率可能是 30/1 样式,不固定)
    看了 CPU 内存和网络都正常。
    KasuganoSoras
        7
    KasuganoSoras  
       Jan 31, 2020   ❤️ 1
    @baoshu #6 那大概就是机器性能或者网络问题了,腾讯云应该和阿里云一样会压制低配机器性能的……不过我觉得更大的可能是因为网络,你可以试下在服务器上用 ab 或者 wrk 自行压测一下看看
    iamfirst
        8
    iamfirst  
       Jan 31, 2020 via iPhone
    本地环境和网络一般比较稳定,可以在本地用 ab 测试数千百次,如果本地的测试结果比较稳定,相差不大。就基本可以确定是腾讯云的网络环境问题了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5282 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 52ms · UTC 08:25 · PVG 16:25 · LAX 01:25 · JFK 04:25
    ♥ Do have faith in what you're doing.