在页面中调用的服务较多时,使用并行方式,即使用 curl_multi_* 系列函数耗时要小于 curl_* 系列函数。
测试环境
操作系统:Windows x64
Server:Apache 2.4.
PHP:5.6.
MySQL:5.7.
cURL:7.47.
测试数据库选择 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个接口:
<?php list($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.055s | 0.046s | 0.058s | 0.049s | 0.052s | 0.052s |
再使用 curl_multi_* 系列函数调用这3个接口
<?php list($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.038s | 0.049s | 0.038s | 0.026s | 0.027s | 0.0356s |
使用 curl_* 系列函数多接口调用5次的平均耗时是0.052秒,使用curl_multi_*系列函数多接口调用5次的平均耗时是0.0356秒。