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

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

测试环境
操作系统:Windows
10 x64 Server:Apache 2.4.18 PHP:5.6.19 MySQL:5.7.11 cURL: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个接口:

<?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.055s0.046s0.058s0.049s0.052s0.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.038s0.049s0.038s0.026s0.027s0.0356s

 

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值