PHP 使用 curl_* 系列函數和 curl_multi_* 系列函數進行多接口調用時的性能對比

在頁面中調用的服務較多時,使用並行方式,即便用 curl_multi_* 系列函數耗時要小於 curl_* 系列函數。php

測試環境
操做系統: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.htmlhtml

測試頁面須要調用 3 個 api:mysql

getActorInfo.phpsql

<?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.phpjson

<?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個接口:api

<?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次耗時的平均值:curl

第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秒。測試

相關文章
相關標籤/搜索