简体   繁体   English

cURL性能比方法执行慢得多

[英]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: 我通过cURL在我的应用程序中对我的另一个应用程序调用了api,代码如下所示:

$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. 此cURL调用大约需要400毫秒。 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. 运行大约需要20毫秒。 So cURL executes in 400ms, but the method it retrieves data from executes in 20ms. 因此cURL在400毫秒内执行,但从中检索数据的方法在20毫秒内执行。

What is wrong? 怎么了?

Well, the curl execution is always going to be slower than the method alone, at least 1 ms more ;). 好吧,curl的执行总是比单独的方法慢,至少要多1 ms;)。 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. 如果将请求时间,网络,服务器响应以及所有这些因素加起来,那么在一个非常强大的设置服务器环境中,至少需要200毫秒,因此,这400毫秒,我认为是正常的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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