首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

PHP 執行時間監測 Timer 類:多標籤計時、微秒精度、輸出格式化、效能分析利器

     編輯:PHP 綜合知識

精準測量程式碼片段執行時間是效能優化基礎。本文實作 Timer 類,支援多標籤並行計時、微秒精度、自動格式化輸出,零依賴、極輕量,適合開發環境即時偵測瓶頸。

類設計特點

  • 多標籤隔離:以字串 Key 區分不同計時區塊 (如 db_query, api_call, render)。
  • 微秒精度:使用 hrtime(true) (PHP 7.3+) 納秒級單調時鐘,避免系統時間調整影響。
  • 疊加計時:同一 Key 可多次 start/end 疊加總耗時。
  • 靈活輸出:getTime() 回傳浮點秒,printTime() 直接輸出毫秒格式。

完整類實作

<?php
class Timer {
    private array $starts = [];
    private array $ends = [];
    private array $totals = []; // 疊加總時間
    private string $defaultKey = '_default';
    private string $prefix = 'timer_';

    /** 開始計時 */
    public function start(string $key = ''): void {
        $key = $this->resolveKey($key);
        $this->starts[$key] = hrtime(true);
    }

    /** 結束計時並疊加 */
    public function end(string $key = ''): void {
        $key = $this->resolveKey($key);
        if (!isset($this->starts[$key])) return; // 未配對 start 忽略
        $elapsed = (hrtime(true) - $this->starts[$key]) / 1e9; // 納秒轉秒
        $this->totals[$key] = ($this->totals[$key] ?? 0) + $elapsed;
        unset($this->starts[$key]);
    }

    /** 獲取總耗時 (秒) */
    public function getTime(string $key = ''): float {
        $key = $this->resolveKey($key);
        return $this->totals[$key] ?? 0;
    }

    /** 格式化輸出毫秒 */
    public function printTime(string $key = '', int $precision = 3): void {
        $key = $this->resolveKey($key);
        $ms = $this->getTime($key) * 1000;
        $label = $key === $this->defaultKey ? 'Total' : $key;
        printf("%s: %.{$precision}f ms\n", $label, $ms);
    }

    /** 獲取所有計時摘要 */
    public function getSummary(): array {
        $summary = [];
        foreach ($this->totals as $k => $v) {
            $label = $k === $this->defaultKey ? 'Total' : $k;
            $summary[$label] = round($v * 1000, 3); // ms
        }
        return $summary;
    }

    /** 重置所有計時 */
    public function reset(): void {
        $this->starts = $this->ends = $this->totals = [];
    }

    private function resolveKey(string $key): string {
        return $key === '' ? $this->defaultKey : $this->prefix . $key;
    }
}
?>

使用範例

<?php
require 'Timer.php';

$timer = new Timer();

// 整體計時
$timer->start();

// 資料庫查詢模擬
$timer->start('db_query');
usleep(mt_rand(50000, 200000)); // 50-200ms
$timer->end('db_query');

// 外部 API 呼叫模擬
$timer->start('api_call');
usleep(mt_rand(100000, 300000));
$timer->end('api_call');

// 視圖渲染模擬
$timer->start('render');
usleep(mt_rand(10000, 50000));
$timer->end('render');

$timer->end(); // 結束整體

// 輸出各區塊耗時
$timer->printTime('db_query');
$timer->printTime('api_call');
$timer->printTime('render');
$timer->printTime(); // Total

// 獲取摘要陣列 (適合記錄日誌/監控上報)
$summary = $timer->getSummary();
/*
Array
(
    [db_query] => 123.456
    [api_call] => 234.567
    [render] => 34.567
    [Total] => 392.590
)
*/
error_log('Request Timing: ' . json_encode($summary));
?>

進階:整合至框架中介軟體自動監測

<?php
// Laravel 風格中介軟體範例
class TimingMiddleware {
    public function handle($request, Closure $next) {
        $timer = new Timer();
        $timer->start('total');

        // 路由解析
        $timer->start('routing');
        $response = $next($request);
        $timer->end('routing');

        // 響應發送前記錄
        $timer->end('total');
        $summary = $timer->getSummary();

        // 註入 Header 供前端/監控讀取
        $response->headers->set('X-Timing-Total', $summary['Total']);
        $response->headers->set('X-Timing-Detail', json_encode($summary));

        return $response;
    }
}
?>

精度對比:hrtime vs microtime

函式精度單調性PHP 版本適用場景
hrtime(true)納秒 (1e-9 秒)✅ 單調時鐘,不受 NTP/手動調整影響7.3+高精度效能分析、基準測試
microtime(true)微秒 (1e-6 秒)❌ 受系統時間影響所有相容舊版本、一般日誌記錄

效能開銷

  • 單次 start/end 約 **50-100 奈秒**,可忽略不計。
  • 記憶體佔用:每個 Key 約 100 位元組。
  • 生產環境可啟用採樣 (如 1% 請求) 避免全量開銷。

Timer 類以 hrtime() 為核心,提供微秒級精度、多標籤隔離、疊加計時與即時輸出,是 PHP 效能分析、API 耗時監控、慢查詢定位的輕量利器。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved