PHP 使用 curl_* 系列函數和 curl_multi_* 系列函數進行多介面調用時的效能對比

來源:互聯網
上載者:User

標籤:

在頁面中調用的服務較多時,使用並行方式,即使用 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.055s 0.046s 0.058s 0.049s 0.052s 0.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.038s 0.049s 0.038s 0.026s 0.027s 0.0356s

 

使用 curl_* 系列函數多介面調用5次的平均耗時是0.052秒,使用curl_multi_*系列函數多介面調用5次的平均耗時是0.0356秒。

 

PHP 使用 curl_* 系列函數和 curl_multi_* 系列函數進行多介面調用時的效能對比

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.