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);
}