duanbo6482 2018-09-14 11:58
浏览 28
已采纳

too long

I'm opening multiple (75) streams through stream_socket_client() and then processing it with stream_select(). The first call of this method takes approx. 15 seconds and I have no idea why. Next calls are much faster - less than one or two seconds for whole method. I've tracked the issue to the foreach where connections are getting opened, which takes 14/15 seconds itself.

Code:

foreach ($tlds as $index => $server ) {

    $ip = gethostbyname($server);
    $con = @stream_socket_client($ip.':43',$errno, $errstr, 10, STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT);

    usleep(200);

    if (!$con) {
        $fails[] = $server;
    } else {
        $calls[$index] = $con;
        stream_set_blocking($calls[$index], false);
    }

    //get time here
}

Testing results:

╔════════╦══════════╦══════════╗
║ $index ║ 1st call ║ 2nd call ║
╠════════╬══════════╬══════════╣
║ 0      ║ 5s       ║ 0s       ║
╠════════╬══════════╬══════════╣
║ 10     ║ 6s       ║ 0s       ║
╠════════╬══════════╬══════════╣
║ 20     ║ 7s       ║ 0s       ║
╠════════╬══════════╬══════════╣
║ 30     ║ 9s       ║ 0s       ║
╠════════╬══════════╬══════════╣
║ 40     ║ 11s      ║ 0s       ║
╠════════╬══════════╬══════════╣
║ 50     ║ 12s      ║ 0s       ║
╠════════╬══════════╬══════════╣
║ 60     ║ 13s      ║ 0s       ║
╠════════╬══════════╬══════════╣
║ 70     ║ 14s      ║ 1s       ║
╠════════╬══════════╬══════════╣
║ end    ║ 14s      ║ 1s       ║
╚════════╩══════════╩══════════╝

I'm not experienced in socket programming at all so I'm greatful for any hints.

PHP 7.1, Apache/2.4.6 (CentOS)

Ask for any info you need - hope I'll be able to answer.

Note: Sometimes the second call still takes around 1/3 of the time the first call takes. But next calls are around 1 second or even less.

  • 写回答

1条回答 默认 最新

  • douxingti9307 2018-09-14 14:05
    关注

    I'm thinking that you have problem with DNS latency. You can try in linux console this in loop to detect it.

    time nslookup $server
    

    If you confirm that dns is slow, you can use NSCD service for local cache of records. In CENTOS : yum -y install nscd;systemctl enable nscd;systemctl start nscd; and after restart httpd service. Statistics from nscd: /usr/sbin/nscd -g

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)