# 假設專案目錄 /home/ad, PHP 安裝於 /home/ad/php
cd /home/ad
wget https://xdebug.org/files/xdebug-3.2.2.tgz # 依 PHP 版本選擇
tar -zxvf xdebug-3.2.2.tgz
cd xdebug-3.2.2
/home/ad/php/bin/phpize
./configure --enable-xdebug --with-php-config=/home/ad/php/bin/php-config
make && make install
安裝完成會提示 .so 路徑,例如 /home/ad/php/lib/php/extensions/no-debug-non-zts-20210902/xdebug.so。
[xdebug]
zend_extension = "/home/ad/php/lib/php/extensions/no-debug-non-zts-20210902/xdebug.so"
xdebug.mode = profile,develop
xdebug.start_with_request = trigger
xdebug.output_dir = "/home/ad/xdebug_log"
xdebug.profiler_output_name = "cachegrind.out.%p.%t"
; 確保輸出目錄存在且可寫
mkdir -p /home/ad/xdebug_log && chmod 777 /home/ad/xdebug_log
重啟 Apache/PHP-FPM。
<?php
// test_profile.php
function a() { echo "aaa"; }
function b() {
a(); sleep(1);
a(); sleep(1);
a();
}
b();
瀏覽器訪問:https://localhost/test_profile.php?XDEBUG_PROFILE=1 (需 start_with_request=trigger)。
apt install qcachegrind (Linux)。cachegrind.out.xxx。sleep() 佔用 2 秒,a() 呼叫 3 次,一目瞭然。單頁需聚合 3 個外部 API (淘宝、搜狐、新浪),串行請求各 500ms → 總耗時 1500ms+,嚴重拖慢首屏渲染。
<?php
function curl_fetch($url, $timeout=3) {
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_TIMEOUT => $timeout,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false, // 僅測試用
]);
$data = curl_exec($ch);
$errno = curl_errno($ch);
curl_close($ch);
return $errno ? false : $data;
}
$urls = ['taobao'=>'https://www.taobao.com', 'sohu'=>'https://www.sohu.com', 'sina'=>'https://www.sina.com.cn'];
$start = microtime(true);
$results = [];
foreach ($urls as $k=>$v) $results[$k] = curl_fetch($v);
echo "串行耗時: " . (microtime(true)-$start) . " 秒\n"; // 約 1.5~2 秒
<?php
function curl_multi_fetch(array $urls, $timeout=3) {
$mh = curl_multi_init();
$handles = [];
foreach ($urls as $key => $url) {
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_TIMEOUT => $timeout,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
]);
curl_multi_add_handle($mh, $ch);
$handles[$key] = $ch;
}
$running = null;
do {
curl_multi_exec($mh, $running);
curl_multi_select($mh, 0.1); // 等待活動,減少 CPU 空轉
} while ($running > 0);
$results = [];
foreach ($handles as $key => $ch) {
$errno = curl_errno($ch);
$results[$key] = $errno ? false : curl_multi_getcontent($ch);
curl_multi_remove_handle($mh, $ch);
curl_close($ch);
}
curl_multi_close($mh);
return $results;
}
$start = microtime(true);
$results = curl_multi_fetch($urls);
echo "並發耗時: " . (microtime(true)-$start) . " 秒\n"; // 約 0.5~0.8 秒 (取決於最慢的那個)
composer require guzzlehttp/guzzle
<?php
use GuzzleHttp\Client;
use GuzzleHttp\Promise;
$client = new Client();
$promises = [
'taobao' => $client->getAsync('https://www.taobao.com'),
'sohu' => $client->getAsync('https://www.sohu.com'),
'sina' => $client->getAsync('https://www.sina.com.cn'),
];
$responses = Promise\Utils::unwrap($promises); // 等待全部完成
foreach ($responses as $key => $resp) {
$data = $resp->getBody()->getContents();
// 處理...
}
// 總耗時 ≈ 最慢單一請求
GuzzleHttp\Pool 可限制並發數,避免衝垮下游或觸發限流。| 方案 | 3 個 500ms API 總耗時 | 複雜度 | 適用場景 |
|---|---|---|---|
| 串行 curl | ~1500ms | 低 | 簡單腳本、依賴順序 |
| curl_multi | ~500-800ms | 中 | 無框架專案、需精細控制 |
| Guzzle Async | ~500-800ms | 低 (封裝好) | 現代框架 (Laravel/Symfony) 首選 |
| Swoole 協程 | ~500ms | 高 (需 Swoole 擴展) | 高併發長連接服務 |
opcache.jit=1255 獲得額外 10-30% CPU 提升。工欲善其事,必先利其器。掌握剖析工具與並發模式,是 PHP 高效能工程師的核心技能。