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.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秒。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架