在页面中调用的服务较多时,使用并行方式,即使用 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秒。

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

  1. Python的程序结构[4] -> 函数/Function[2] -> 匿名函数

    匿名函数 / Anonymous Function 匿名函数是一种不需要绑定函数名的函数 (i.e. functions that are not bound to a name).匿名函数通过 la ...

  2. 浅析php curl_multi_*系列函数进行批量http请求

    何起: 一系列 数量很大 数据不热 还希望被蜘蛛大量抓取的页面,在蜘蛛抓取高峰时,响应时间会被拉得很高. 前人做了这样一个事儿:页面分3块,用3个内部接口提供,入口文件用curl_multi_*系列函 ...

  3. 使用file_get_content系列函数和使用curl系列函数采集图片的性能对比

    由于公司的一个汽车网站的后台的汽车内容都是主要是来自与汽车之家的,编辑的同事们必须天天手动去对着汽车之家来添加汽车,实在是太蛋疼了.于是乎,为了改变这种状况,作为一个开发码农,我的任务就来了...那就 ...

  4. 深入理解this机制系列第三篇——箭头函数

    × 目录 [1]痛点 [2]解决 [3]基本用法[4]回调函数[5]注意事项 前面的话 this机制与函数调用有关,而作用域则与函数定义有关.有没有什么是可以将this机制和作用域联系起来的呢?本文将 ...

  5. 【函数】Oracle函数系列(2)--数学函数及日期函数

    [函数]Oracle函数系列(2)--数学函数及日期函数 1  BLOG文档结构图 2  前言部分 2.1  导读和注意事项 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不 ...

  6. 开心菜鸟系列----函数作用域(javascript入门篇)

      1 <!DOCTYPE html>   2 <html>   3 <script src="./jquery-1.7.2.js"></ ...

  7. SSE 系列内置函数中的 shuffle 函数

    SSE 系列内置函数中的 shuffle 函数 邮箱: quarrying@qq.com 博客: http://www.cnblogs.com/quarryman/ 发布时间: 2017年04月18日 ...

  8. 深入理解javascript函数进阶系列第一篇——高阶函数

    前面的话 前面的函数系列中介绍了函数的基础用法.从本文开始,将介绍javascript函数进阶系列,本文将详细介绍高阶函数 定义 高阶函数(higher-order function)指操作函数的函数 ...

  9. 大白话Vue源码系列(03):生成render函数

    阅读目录 优化 AST 生成 render 函数 小结 本来以为 Vue 的编译器模块比较好欺负,结果发现并没有那么简单.每一种语法指令都要考虑到,处理起来相当复杂.上篇已经生成了 AST,本篇依然对 ...

随机推荐

  1. css content 的 attr 用法 (实现悬浮提示)

    content 的attr 实现 鼠标悬浮 显示 悬浮提示, content 属性与 :before 及 :after 伪元素配合使用,来插入生成内容. 方法/步骤 1 <div> < ...

  2. angular自己最近学的一种筛选方法

    投资状态vm.statusList = [ {name:"项目状态",value:-1}, {name:"上线",value:0}, {name:"投 ...

  3. AC自动机

    AC自动机,全称Aho-Corasick自动机.如果没记错的话好像就是前缀自动机. 其实AC自动机就是KMP上树的产物.理解了KMP,那AC自动机应该也是很好理解的. 与KMP类似,AC自动机也是扔一 ...

  4. asp.net中membership使用oracle数据库(二)

    需要安装的东西都准备好了,继续生成后台表.过程.函数.触发器等.ps/sql中 @@E:\oracle\product\11.2.0\client_1\ASP.NET\SQL\InstallAllOr ...

  5. LeetCode之371. Sum of Two Integers

    ---------------------------------- 使用位运算实现加法: a^b 加不同部分(a&b)<<1 加相同部分递归相加 AC代码: public cla ...

  6. hdu 1300 Pearls

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1300 思路:用dp[i]表示前i种花费最低的情况,则有dp[i]=min(dp[i],dp[j+1]+(( ...

  7. AT常见问题

    https://m.douban.com/note/247040789/?from=author

  8. JavaOO面向对象中的注意点

    1.JavaOO宗旨思想: ★万物皆对象,对象因关注而产生★ ☆类是对象的抽取,对象是类的实例☆ 2.JavaOO的三大特征: 封装.继承.多态  (第四大特征 抽象 现还有争议) 3.属性与行为: ...

  9. 使用sp_xml_preparedocument处理XML文档

    有时会在存储过程中处理一些XML格式的数据,所以会用到sp_xml_preparedocument,他可以将XML数据进行读取,然后使用 MSXML 分析器 (Msxmlsql.dll) 对其进行分析 ...

  10. Linux学习笔记(2)-开机

    今天开始学习Linux系统. 打开虚拟机,输入密码后,令人激动的画面就蹦出来了-- Ubuntu的主要基调是橙色,给人一种蠢萌蠢萌的感觉,和Windows不同,它只在左边有一条任务栏,上面有些东西,搜 ...