dqq46733 2015-05-23 12:56
浏览 520

PHP cURL多处理性能比普通cURL慢

I am trying to use cURL curl_multi_init() as a means to speed up requests. What I am experiencing though is that the request with the multi handles initinally take longer than the same request made with curl_init().

Below is an example of two identical requests. Consistently the multi request takes about 4/5 times longer than the single request. The cURL options in both requests are identical. In this example the multi cURL only makes one request.

Additional info: PHP 5.3.3/cURL 7.21.0, Windows server 2008/IIS 7x.

I am totally flabbergasted about what could be causing this slow response. The request is being made to a server which resides in the same network, years of usage of the normal cURL has given me the experience that this kind of request, to this particular backend with non multi handling, take on average between 0.2/0.3 seconds.

The question is: what could be causing this slowness of the multi cURL request.

Below are the results of a test, two requests, one being done with curl_init(), the other with curl_multi_init (both in the same script). Notice the cURL info, the requests being exactly the same in terms of header_size, request_size, size_upload, size_download and download_content_length.

Test with normal cURL:

Array
(
    [url] => http://myurl.com
    [content_type] => text/xml;charset=UTF-8
    [http_code] => 200
    [header_size] => 261
    [request_size] => 312
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.203
    [namelookup_time] => 0
    [connect_time] => 0.015
    [pretransfer_time] => 0.015
    [size_upload] => 174
    [size_download] => 236
    [speed_download] => 1162
    [speed_upload] => 857
    [download_content_length] => 236
    [upload_content_length] => 0
    [starttransfer_time] => 0.203
    [redirect_time] => 0
)

Test with multi cURL (notice the speed upload/download is lower, connect and name lookup time higher):

Array
(
    [client] => Array
        (
            [url] => http://myurl.com
            [content_type] => text/xml;charset=UTF-8
            [http_code] => 200
            [header_size] => 261
            [request_size] => 312
            [filetime] => -1
            [ssl_verify_result] => 0
            [redirect_count] => 0
            [total_time] => 1.047
            [namelookup_time] => 0.61
            [connect_time] => 0.61
            [pretransfer_time] => 0.61
            [size_upload] => 174
            [size_download] => 236
            [speed_download] => 225
            [speed_upload] => 166
            [download_content_length] => 236
            [upload_content_length] => 0
            [starttransfer_time] => 1.047
            [redirect_time] => 0
            [multi_handle_info] => Array
                (
                    [msg] => 1
                    [result] => 0
                    [handle] => Resource id #5
                )

        )

)

Example of the code for the multi cURL (options are same as in the code for normal cURL):

$curl = array();        
$result = array();      
$mh = curl_multi_init();

foreach (array_keys($queries) as $id) {

    $curl[$id] = curl_init();           
    curl_setopt($curl[$id], CURLOPT_URL,$queries[$id]['url']); 
    curl_setopt($curl[$id], CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($curl[$id], CURLOPT_CONNECTTIMEOUT,60); 
    curl_setopt($curl[$id], CURLOPT_DNS_CACHE_TIMEOUT,3600); 
    curl_setopt($curl[$id], CURLOPT_TIMEOUT, 240);      
    curl_setopt($curl[$id], CURLOPT_POSTFIELDS, $queries[$id]['post']);         

    curl_multi_add_handle($mh, $curl[$id]);
}

$running = null;
do {
    curl_multi_exec($mh, $running);             
} while($running > 0);

// get content and remove handles
foreach($curl as $id => $c) {

    $result[$id] = curl_multi_getcontent($c);           
    curl_multi_remove_handle($mh, $c);
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
    • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
    • ¥15 乘性高斯噪声在深度学习网络中的应用
    • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛