简体   繁体   中英

cURL performance much slower than method execution

I have one call to api in my application via cURL to my other application, the code looks like this:

$target = 'api/someMethod';

$url = self :: buildUrl($target);
    $paramString = self :: buildParamString(array(
            'request' => $request,
            'partner' => $_SESSION['DataPartner'],
            'token' => $_SESSION['DataToken']
    ));

    $t = microtime(true);
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => $url,
        CURLOPT_POST => 1,
        CURLOPT_POSTFIELDS => $paramString,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_TIMEOUT => 600,
        CURLOPT_CONNECTTIMEOUT => 60,
    ));
    $curlResponse = curl_exec($curl);
    Log :: i(microtime(true) - $t); // ~400ms

This cURL call takes about 400ms. The problem is that the method it retrieves data from:

public function someMethod() {
   $t = microtime(true);
   // create the response, some SQL, etc
   Log :: i(microtime(true) - $t); // ~20ms
   echo $response;
}

Takes about 20ms to run. So cURL executes in 400ms, but the method it retrieves data from executes in 20ms.

What is wrong?

Well, the curl execution is always going to be slower than the method alone, at least 1 ms more ;). I don't see that much of slow difference in your case. If you sum the request time, network, the server response, and all that that will give you at least 200ms in a very strong setup server enviroment, so, those 400ms, I think is normal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM