V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
baoshu
V2EX  ›  问与答

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

  •  
  •   baoshu · 2020-01-30 19:32:11 +08:00 · 1861 次点击
    这是一个创建于 1519 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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 条回复    2020-01-31 20:07:49 +08:00
    baoshu
        1
    baoshu  
    OP
       2020-01-30 19:34:36 +08:00
    服务器是 1 核 2G 的。
    iamfirst
        2
    iamfirst  
       2020-01-30 20:04:07 +08:00 via iPhone
    和网络环境有关吧
    eryueyu
        3
    eryueyu  
       2020-01-30 20:22:47 +08:00 via iPhone
    查服务器性能占用,网络环境
    KasuganoSoras
        4
    KasuganoSoras  
       2020-01-30 20:46:30 +08:00
    你贴的这段代码和你截图里的根本不一样吧。。
    baoshu
        5
    baoshu  
    OP
       2020-01-30 21:47:51 +08:00
    @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
       2020-01-30 21:52:55 +08:00
    @eryueyu @iamfirst
    大部分时候都是 30-60ms,极少数情况是 600ms 左右。(出现频率可能是 30/1 样式,不固定)
    看了 CPU 内存和网络都正常。
    KasuganoSoras
        7
    KasuganoSoras  
       2020-01-31 06:53:19 +08:00   ❤️ 1
    @baoshu #6 那大概就是机器性能或者网络问题了,腾讯云应该和阿里云一样会压制低配机器性能的……不过我觉得更大的可能是因为网络,你可以试下在服务器上用 ab 或者 wrk 自行压测一下看看
    iamfirst
        8
    iamfirst  
       2020-01-31 20:07:49 +08:00 via iPhone
    本地环境和网络一般比较稳定,可以在本地用 ab 测试数千百次,如果本地的测试结果比较稳定,相差不大。就基本可以确定是腾讯云的网络环境问题了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3239 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 11:41 · PVG 19:41 · LAX 04:41 · JFK 07:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.