標籤:
在頁面中調用的服務較多時,使用並行方式,即使用 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_* 系列函數進行多介面調用時的效能對比