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 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件