首页 > 代码库 > PHP 使用 curl_* 系列函数和 curl_multi_* 系列函数进行多接口调用时的性能对比

PHP 使用 curl_* 系列函数和 curl_multi_* 系列函数进行多接口调用时的性能对比

在页面中调用的服务较多时,使用并行方式,即使用 curl_multi_* 系列函数耗时要小于 curl_* 系列函数。

测试环境
操作系统:Windows
10 x64Server:Apache 2.4.18PHP:5.6.19MySQL:5.7.11cURL:7.47.1

 

测试数据库选择 MySQL 官方网站的样本数据库 sakila,下载地址:http://dev.mysql.com/doc/index-other.html

测试页面需要调用 3 个 api:

getActorInfo.php

<?php// 接口1$dsn = ‘mysql:host=localhost;dbname=sakila‘;$user = ‘root‘;$pwd = ‘‘;try {    $pdo = new PDO($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// 接口2$dsn = ‘mysql:host=localhost;dbname=sakila‘;$user = ‘root‘;$pwd = ‘‘;try {    $pdo = new PDO($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// 接口3$dsn = ‘mysql:host=localhost;dbname=sakila‘;$user = ‘root‘;$pwd = ‘‘;try {    $pdo = new PDO($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));

 

首先使用 curl_* 系列函数调用这3个接口:

<?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 ‘耗时‘,$seconds,‘秒‘;

分别取5次耗时的平均值:

第1次第2次第3次第4次第5次平均
0.055s0.046s0.058s0.049s0.052s0.052s

 

 

再使用 curl_multi_* 系列函数调用这3个接口

<?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);}// 多个cURL资源加入到$mh句柄中$mh = curl_multi_init();foreach($ch as $key => $val) {    curl_multi_add_handle($mh, $ch[$key]);}// 执行批处理等待全部完成$running = null;do {    curl_multi_exec($mh, $running);} while($running);// 待完成后 获取返回的内容foreach($ch as $key => $val) {    $result = curl_multi_getcontent($ch[$key]);    var_dump($result);    // 关闭各个句柄    curl_multi_remove_handle($mh, $ch[$key]);    }list($usec, $sec) = explode(" ", microtime());$end = (float)$usec + (float)$sec;$seconds = $end - $start;echo ‘耗时‘,$seconds,‘秒‘;
第1次第2次第3次第4次第5次平均
0.038s0.049s0.038s0.026s0.027s0.0356s

 

使用 curl_* 系列函数多接口调用5次的平均耗时是0.052秒,使用curl_multi_*系列函数多接口调用5次的平均耗时是0.0356秒。

 

PHP 使用 curl_* 系列函数和 curl_multi_* 系列函数进行多接口调用时的性能对比