跳转至

性能监控命令

概述

Windows提供了多种性能监控工具,用于监控系统性能、分析资源使用情况、诊断性能问题。

typeperf - 性能计数器命令行

typeperf - 获取性能数据

typeperf命令

从性能计数器获取数据,可用于监控和记录系统性能。

语法

Text Only
typeperf [options] [counter]

选项: - -cf <filename>:从文件读取计数器列表 - -f <csv|tsv>:输出格式 - -sc <count>:采样次数 - -si <hh:mm:ss>:采样间隔 - -o <filename>:输出到文件 - -q:查询可用计数器 - -qx:查询实例

示例

Text Only
# CPU使用率
typeperf "\Processor(_Total)\% Processor Time"

# 内存使用
typeperf "\Memory\Available MBytes"
typeperf "\Memory\% Committed Bytes In Use"

# 磁盘性能
typeperf "\PhysicalDisk(_Total)\Disk Read Bytes/sec"
typeperf "\PhysicalDisk(_Total)\Disk Write Bytes/sec"
typeperf "\PhysicalDisk(_Total)\% Disk Time"

# 网络性能
typeperf "\Network Interface(*)\Bytes Total/sec"

# 进程性能
typeperf "\Process(*)\% Processor Time"
typeperf "\Process(*)\Working Set"

# 持续采样(每秒一次,采样10次)
typeperf "\Processor(_Total)\% Processor Time" -sc 10 -si 00:00:01

# 输出到CSV文件
typeperf "\Processor(_Total)\% Processor Time" -sc 60 -f csv -o perf.csv

查询可用计数器

Text Only
1
2
3
4
typeperf -q                          # 列出所有计数器
typeperf -q "Processor"              # 列出Processor对象下的计数器
typeperf -qx                         # 列出计数器和实例
typeperf -qx "Process"               # 列出Process的实例

logman - 数据收集器

创建数据收集器

Text Only
1
2
3
4
5
# 创建性能数据收集器
logman create counter MyPerf -c "\Processor(_Total)\% Processor Time" "\Memory\Available MBytes" -si 5 -o C:\perf.blg

# 创建跟踪数据收集器
logman create trace MyTrace -p "Windows Kernel Trace" -o C:\trace.etl

启动数据收集

Text Only
1
2
3
4
5
logman start MyPerf                  # 启动收集器
logman stop MyPerf                   # 停止收集器
logman delete MyPerf                 # 删除收集器
logman query MyPerf                  # 查询收集器状态
logman query                         # 查询所有收集器

导出数据

Text Only
logman export MyPerf -f csv -o perf.csv

wmic性能监控

wmic cpu

Text Only
1
2
3
wmic cpu get loadpercentage          # CPU负载
wmic cpu get /value                  # 完整CPU信息
wmic cpu list full                   # 详细列表

wmic os

Text Only
wmic os get TotalVisibleMemorySize,FreePhysicalMemory  # 内存信息
wmic os get FreeVirtualMemory         # 虚拟内存

wmic process

Text Only
1
2
3
wmic process get name,workingsetsize  # 进程内存使用
wmic process get name,percentprocessortime  # 进程CPU时间
wmic process where "workingsetsize>100000000" get name,workingsetsize  # 内存超过100MB的进程

wmic logicaldisk

Text Only
wmic logicaldisk get size,freespace,caption  # 磁盘空间
wmic logicaldisk get volumename,size,freespace,filesystem

PowerShell性能监控

Get-Counter

PowerShell
# 获取CPU使用率
Get-Counter "\Processor(_Total)\% Processor Time"

# 获取内存信息
Get-Counter "\Memory\Available MBytes"
Get-Counter "\Memory\% Committed Bytes In Use"

# 持续采样
Get-Counter "\Processor(_Total)\% Processor Time" -SampleInterval 2 -MaxSamples 10

# 获取多组计数器
$counters = @(
    "\Processor(_Total)\% Processor Time"
    "\Memory\Available MBytes"
    "\PhysicalDisk(_Total)\% Disk Time"
)
Get-Counter -Counter $counters

# 获取实例
Get-Counter -ListSet Process | Get-Counter

# 导出数据
Get-Counter "\Processor(_Total)\% Processor Time" -SampleInterval 1 -MaxSamples 60 | 
    Export-Csv -Path cpu.csv -NoTypeInformation

Get-WmiObject

PowerShell
# CPU信息
Get-WmiObject Win32_Processor | Select-Object Name, LoadPercentage

# 内存信息
$os = Get-WmiObject Win32_OperatingSystem
$totalMem = [math]::Round($os.TotalVisibleMemorySize / 1MB, 2)
$freeMem = [math]::Round($os.FreePhysicalMemory / 1MB, 2)
Write-Host "Total Memory: $totalMem GB"
Write-Host "Free Memory: $freeMem GB"

# 进程内存使用
Get-WmiObject Win32_Process | Sort-Object WorkingSetSize -Descending | 
    Select-Object -First 10 Name, @{N='Memory(MB)';E={[math]::Round($_.WorkingSetSize/1MB,2)}}

# 磁盘使用
Get-WmiObject Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3} | 
    Select-Object DeviceID, @{N='Size(GB)';E={[math]::Round($_.Size/1GB,2)}}, 
        @{N='Free(GB)';E={[math]::Round($_.FreeSpace/1GB,2)}}

Get-CimInstance

PowerShell
# CPU负载
Get-CimInstance Win32_Processor | Select-Object Name, LoadPercentage

# 内存信息
Get-CimInstance Win32_OperatingSystem | Select-Object TotalVisibleMemorySize, FreePhysicalMemory

# 进程信息
Get-CimInstance Win32_Process | Sort-Object WorkingSetSize -Descending | 
    Select-Object -First 10 Name, @{N='CPU(s)';E={$_.KernelModeTime + $_.UserModeTime}}, 
        @{N='Memory(MB)';E={[math]::Round($_.WorkingSetSize/1MB,2)}}

性能监视器

perfmon - 图形化性能监视器

Text Only
1
2
3
perfmon                             # 打开性能监视器
perfmon /res                         # 打开资源监视器
perfmon /sys                         # 打开系统监视器

资源监视器

Text Only
resmon                              # 打开资源监视器

任务管理器命令

Text Only
taskmgr                             # 打开任务管理器

性能分析工具

xperf / wpa

Text Only
1
2
3
4
5
6
7
8
# 开始记录
xperf -on base

# 停止记录
xperf -stop -d trace.etl

# 分析
wpa trace.etl

Windows Performance Recorder

Text Only
wpr -start CPU                      # 开始CPU分析
wpr -stop trace.etl                 # 停止并保存

实时监控脚本

监控CPU使用率

PowerShell
1
2
3
4
5
6
while ($true) {
    $cpu = Get-Counter "\Processor(_Total)\% Processor Time"
    $time = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
    Write-Host "$time CPU: $([math]::Round($cpu.CounterSamples.CookedValue, 2))%"
    Start-Sleep -Seconds 1
}

监控内存使用

PowerShell
while ($true) {
    $os = Get-CimInstance Win32_OperatingSystem
    $total = [math]::Round($os.TotalVisibleMemorySize / 1MB, 2)
    $free = [math]::Round($os.FreePhysicalMemory / 1MB, 2)
    $used = [math]::Round(($total - $free), 2)
    $percent = [math]::Round(($used / $total) * 100, 2)
    
    Write-Host "Memory: $used / $total GB ($percent%)"
    Start-Sleep -Seconds 2
}

监控磁盘IO

PowerShell
1
2
3
4
5
6
7
while ($true) {
    $disk = Get-Counter "\PhysicalDisk(_Total)\Disk Bytes/sec"
    $bytes = $disk.CounterSamples.CookedValue
    $mb = [math]::Round($bytes / 1MB, 2)
    Write-Host "Disk IO: $mb MB/s"
    Start-Sleep -Seconds 1
}

监控网络流量

PowerShell
while ($true) {
    $net = Get-Counter "\Network Interface(*)\Bytes Total/sec"
    foreach ($sample in $net.CounterSamples) {
        if ($sample.InstanceName -ne "_Total") {
            $mb = [math]::Round($sample.CookedValue / 1MB, 2)
            Write-Host "$($sample.InstanceName): $mb MB/s"
        }
    }
    Write-Host "---"
    Start-Sleep -Seconds 2
}

性能报警脚本

CPU报警

PowerShell
1
2
3
4
5
6
7
8
$threshold = 80
while ($true) {
    $cpu = (Get-Counter "\Processor(_Total)\% Processor Time").CounterSamples.CookedValue
    if ($cpu -gt $threshold) {
        Write-Warning "CPU usage is high: $([math]::Round($cpu, 2))%"
    }
    Start-Sleep -Seconds 5
}

内存报警

PowerShell
1
2
3
4
5
6
7
8
9
$threshold = 90
while ($true) {
    $os = Get-CimInstance Win32_OperatingSystem
    $percent = (($os.TotalVisibleMemorySize - $os.FreePhysicalMemory) / $os.TotalVisibleMemorySize) * 100
    if ($percent -gt $threshold) {
        Write-Warning "Memory usage is high: $([math]::Round($percent, 2))%"
    }
    Start-Sleep -Seconds 5
}

磁盘空间报警

PowerShell
1
2
3
4
5
6
7
8
$threshold = 10  # 10GB
$disks = Get-CimInstance Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3}
foreach ($disk in $disks) {
    $free = [math]::Round($disk.FreeSpace / 1GB, 2)
    if ($free -lt $threshold) {
        Write-Warning "Low disk space on $($disk.DeviceID): $free GB free"
    }
}

参考资料