dontoften8899 2012-02-29 19:26
浏览 66
已采纳

PHP - curl_exec需要100毫秒

My PHP project works by grabbing data from a WCF REStful web service with curl.

However, for some reason the curl_exec function always takes around 100ms to return, even though the WCF web service is on the same machine, and the WCF service function being called only takes 3ms to execute.

I've profiled the WCF service and the entire WCF stack accepts a HTTP request and returns the resultant XML (a tiny XML structure with 5 elements) in under 10ms.

Below is my cURL set-up code:

    $curl = curl_init($this->uri);

    curl_setopt($curl, CURLOPT_HEADER        , true ); // return response HTTP headers in output
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true ); // return result instead of echoing
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // stop cURL from verifying the peer's certificate. This is set because by default (and right now) cURL does not trust /any/ certificate.
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true ); // follow redirects, Location: headers (but does it really?)
    curl_setopt($curl, CURLOPT_MAXREDIRS     , 10   ); // but dont redirect more than 10 times

    curl_setopt($curl, CURLOPT_CUSTOMREQUEST , $this->method );

    if( isset( $this->username  ) )
        curl_setopt($curl, CURLOPT_USERPWD  , $this->username . ':' . $this->password );

    if( isset( $this->userAgent ) )
        curl_setopt($curl, CURLOPT_USERAGENT, $this->userAgent);

    if( isset( $this->content ) ) {
        curl_setopt($curl, CURLOPT_POSTFIELDS, $this->content );

        array_push( $this->headers, 'Content-Length: '. $this->contentLength );

        if( isset( $this->contentType ) )
            array_push( $this->headers, 'Content-Type: ' . $this->contentType );
    }

    if( isset( $this->contentFile ) ) {
        curl_setopt($curl, CURLOPT_INFILE    , $this->contentFile   );
        curl_setopt($curl, CURLOPT_INFILESIZE, $this->contentLength );
        array_push( $this->headers, 'Content-Length: '. $this->contentLength );
        array_push( $this->headers, 'Content-Type: '  . $this->contentType );
    }

    if( count( $this->headers ) > 0 )
        curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers );

The call to curl_exec happens immediately after this block of code, and it's the curl_exec call that I measure at 100ms consistently.

It's important I get the load times of this PHP site down, and I can't understand why this happens.

UPDATE:

cURL's curl_exec always seems to take 100ms regardless of what is being requested, although very interestingly when I make a second curl_exec request (in the same PHP page execution) it completes a lot quicker - around 20ms.

The mystery deepens.

UPDATE 2:

I realised that curl_getinfo() returns an associative-array containing timing information. When I dump the info from the first cURL request I see the following:

'total_time' => 0.125,
'namelookup_time' => 0.125,
'connect_time' => 0.125,
'pretransfer_time' => 0.125,
'starttransfer_time' => 0.125,

Whereas all subsequent requests are like this (or better):

'total_time' => 0.016,
'namelookup_time' => 0,
'connect_time' => 0,
'pretransfer_time' => 0,
'starttransfer_time' => 0.016,

It seems the delay is in namelookup, but I don't know why DNS lookup takes so long given that Windows (the server OS) already has the DNS information cached and other applications (desktop, ASP.NET, etc) can resolve the name in under 1ms, but cURL does it in 125ms.

What's so special about cURL that it can't use the OS's DNS cache?

  • 写回答

2条回答 默认 最新

  • duanpei4455 2012-08-31 00:26
    关注

    I think it's a bug in the version of cURL included in my PHP installation. After I updated cURL (and PHP) to a more recent version the problem disappeared and curl_exec executes in under 20ms now.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答