Search code examples
phpasynchronouscurl-multi

Individual Response Time using PHP curl_multi


It is possible to record the response times of each process using curl_multi? Here's the code I'm currently using I'm just not sure how to record the response times of each process. Thanks for any help!

do 
{
    $execReturnValue = curl_multi_exec($mh, $runningHandles);
} while ($execReturnValue == CURLM_CALL_MULTI_PERFORM);

// Loop and continue processing the request
while ($runningHandles && $execReturnValue == CURLM_OK) 
{
    // Wait forever for network
    $numberReady = curl_multi_select($mh);
    if ($numberReady != -1) 
    {
        // Pull in any new data, or at least handle timeouts
        do     
        {
            $execReturnValue = curl_multi_exec($mh, $runningHandles);
        } while ($execReturnValue == CURLM_CALL_MULTI_PERFORM);
    }
}       
//End Run
//Get Data and Close
foreach($ping as $cid=>$p)
{
    $curlError = curl_error($c[$cid]);
    if($curlError == "") 
    {
        $curl_data[$cid] = curl_multi_getcontent($c[$cid]);
    } 
    else 
    {
        //email me!
    }           
curl_multi_remove_handle($mh,$c[$cid]);
curl_close($c[$cid]);
}
curl_multi_close($mh);

Solution

  • Use microtime() to get the time at the start of your forloop and then use microtime() at the end to get the time at the end of your code and subtract the time at the end from the time at the beginning to get the difference. That's how long your code took to run.

    EDIT:

    curl_setopt($ch, CURLOPT_WRITEFUNCTION, ‘read_body’);//sets callback
    

    http://www.php.net/manual/en/function.curl-setopt.php

    curl_multi launches processes in parallel. Therefore my previous suggestion wouldn't work. Solution: add a callback! take the microtime of when all those processes start and in the callback record the process id and the time it checked in. Unfortunately all those processes are going to be battling for control of that callback. I figure the solution to that is using fork() to create new processes. This is getting into quite a lot of work just to get a progress bar back but if you are interested here is a link to the php fork(): http://php.net/manual/en/function.pcntl-fork.php