Performance comparison of PHP using Curl_* series functions and Curl_multi_* series functions for multi-interface calls

Source: Internet
Author: User
Tags dsn getmessage

When you call more services in a page, you use a parallel approach, that is, using the Curl_multi_* series functions is less time consuming than the Curl_* series functions.

Test environment
Ten2.4. - PHP: 5.6. + MySQL: 5.7. One CURL: 7.47. 1

Test database Select the sample database of the MySQL official website sakila,:http://dev.mysql.com/doc/index-other.html

The test page requires a call to 3 APIs:

getactorinfo.php

<?PHP//Interface 1$dsn= ' Mysql:host=localhost;dbname=sakila ';$user= ' Root ';$pwd= ' ';Try {    $pdo=NewPDO ($dsn,$user,$pwd);} Catch(pdoexception$e) {    Echo $e-getMessage ();}$sql= ' SELECT * from actor limit 0, 100 ';$query=$pdo->query ($sql);$query->setfetchmode (PDO::FETCH_ASSOC);$rs=$query-Fetchall ();Exit(Json_encode ($rs));

getaddressinfo.php

<?PHP//Interface 2$dsn= ' Mysql:host=localhost;dbname=sakila ';$user= ' Root ';$pwd= ' ';Try {    $pdo=NewPDO ($dsn,$user,$pwd);} Catch(pdoexception$e) {    Echo $e-getMessage ();}$sql= ' SELECT * From address limit 0, 100 ';$query=$pdo->query ($sql);$query->setfetchmode (PDO::FETCH_ASSOC);$rs=$query-Fetchall ();Exit(Json_encode ($rs));

getcityinfo.php

<?PHP//Interface 3$dsn= ' Mysql:host=localhost;dbname=sakila ';$user= ' Root ';$pwd= ' ';Try {    $pdo=NewPDO ($dsn,$user,$pwd);} Catch(pdoexception$e) {    Echo $e-getMessage ();}$sql= ' SELECT * from city limit 0, 100 ';$query=$pdo->query ($sql);$query->setfetchmode (PDO::FETCH_ASSOC);$rs=$query-Fetchall ();Exit(Json_encode ($rs));

First call these 3 interfaces using the Curl_* series function:

<?PHPList($usec,$sec) =Explode(" ",Microtime());$start= (float)$usec+ (float)$sec;$api= [];$api[] = ' http://127.0.0.3/php/high-performance/5/curl/api/getCityInfo.php ';$api[] = ' http://127.0.0.3/php/high-performance/5/curl/api/getAddressInfo.php ';$api[] = ' http://127.0.0.3/php/high-performance/5/curl/api/getActorInfo.php ';$ch= [];foreach($api  as $key=$val) {    $ch[$key] = Curl_init ($val); curl_setopt ($ch[$key], Curlopt_returntransfer,TRUE); $result= Curl_exec ($ch[$key]); Curl_close ($ch[$key]); Var_dump($result);}List($usec,$sec) =Explode(" ",Microtime());$end= (float)$usec+ (float)$sec;$seconds=$end-$start;Echo' Time consuming ',$seconds, ' seconds ';

Take 5 time-consuming averages, respectively:

1th time 2nd time 3rd time 4th time 5th time Average
0.055s 0.046s 0.058s 0.049s 0.052s 0.052s

Then call these 3 interfaces using the Curl_multi_* series function

<?PHPList($usec,$sec) =Explode(" ",Microtime());$start= (float)$usec+ (float)$sec;$api= [];$api[] = ' http://127.0.0.3/php/high-performance/5/curl/api/getCityInfo.php ';$api[] = ' http://127.0.0.3/php/high-performance/5/curl/api/getAddressInfo.php ';$api[] = ' http://127.0.0.3/php/high-performance/5/curl/api/getActorInfo.php ';$ch= [];foreach($api  as $key=$val) {    $ch[$key] = Curl_init ($val); curl_setopt ($ch[$key], Curlopt_returntransfer,TRUE);}//Multiple Curl resources are added to the $MH handle$MH=curl_multi_init ();foreach($ch  as $key=$val) {Curl_multi_add_handle ($MH,$ch[$key]);}//Execute batch wait all complete$running=NULL; Do{curl_multi_exec ($MH,$running);}  while($running);//get returned content when you are finishedforeach($ch  as $key=$val) {    $result= Curl_multi_getcontent ($ch[$key]); Var_dump($result); //close each handleCurl_multi_remove_handle ($MH,$ch[$key]); }List($usec,$sec) =Explode(" ",Microtime());$end= (float)$usec+ (float)$sec;$seconds=$end-$start;Echo' Time consuming ',$seconds, ' seconds ';
1th time 2nd time 3rd time 4th time 5th time Average
0.038s 0.049s 0.038s 0.026s 0.027s 0.0356s

The average time to use a multi-interface call with the Curl_* series function is 0.052 seconds, and the average time to call 5 times with curl_multi_* series functions is 0.0356 seconds.

Performance comparison of PHP using Curl_* series functions and Curl_multi_* series functions for multi-interface calls

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.