Windows 內建的 PDH (Performance Data Helper) API 與命令行工具 typeperf 是無需安裝第三方軟體即可進行系統效能計數器採樣、記錄與分析的利器。本文匯總常用計數器路徑、typeperf 實戰參數與自動化腳本技巧。
| 監控目標 | 計數器路徑 | 說明 |
|---|---|---|
| 各進程 CPU 佔用率 | \Process(*)\% Processor Time | 通配符 * 表示所有進程實例 |
| 各進程工作集記憶體 | \Process(*)\Working Set | 進程當前佔用的物理記憶體頁面 |
| 系統提交記憶體使用率 | \Memory\% Committed Bytes In Use | 反映虛擬記憶體壓力 |
| 指定進程 CPU (完整路徑) | \Process(C:\Path\To\chrome.exe)\% Processor Time | 需完整路徑含副檔名 |
| 指定進程名 CPU | \Process(chrome)\% Processor Time | 僅進程名(無副檔名),多實例會聚合 |
typeperf "計數器路徑1" ["計數器路徑2" ...] [選項]
| 參數 | 功能 | 範例 |
|---|---|---|
-sc <N> | 採樣次數(預設無限) | typeperf "\Process(*)\% Processor Time" -sc 10 |
-si <秒> | 採樣間隔(預設 1 秒) | typeperf "\Memory\Available MBytes" -si 5 |
-o <檔案> | 輸出至 CSV/TSV/二進制日誌 | typeperf "\Process(*)\Working Set" -o perf.csv -f CSV |
-f <CSV|TSV|BIN> | 輸出格式 | 同上 |
-q | 查詢本機可用計數器(含實例) | typeperf -q "\Process" |
-qx | 查詢所有計數器(含解釋) | typeperf -qx > all_counters.txt |
typeperf "\Process(*)\% Processor Time" "\Process(*)\Working Set" -sc 1
輸出 CSV 格式,首行標題,次行數值,可直接導入 Excel 分析。
typeperf "\Process(xdict)\% Processor Time" "\Process(xdict)\Working Set" -si 5 -sc 12 -o xdict_perf.csv
typeperf -qx > counters_full.txt
$cpu = (typeperf "\Process(chrome)\% Processor Time" -sc 1)[1].Split(',')[1].Trim('"')
if ([double]$cpu -gt 80) { Send-MailMessage ... }
#1、#2,需用 typeperf -q "\Process" 確認實際實例名。\處理器(*)\% 處理器時間),建議統一用英文路徑或用 typeperf -q 確認當前語系下的路徑。typeperf -s \\RemoteHost "\Processor(_Total)\% Processor Time" 需開啟遠端登錄服務與防火牆規則。typeperf 是 Windows 原生、零依賴、可腳本化的效能採樣利器。熟練掌握計數器路徑命名規則、採樣參數組合與輸出格式控制,能快速構建輕量級監控、故障複現與容量規劃數據來源,是運維與開發人員的必備技能包。