Typeperf

Typeperf 是 cmd 中默认自带的一个记录系统资源使用情况的工具,它可以监控并记录指定程序或系统的运行情况。

监控程序使用情况并记录到文本

因为本人使用的是 cmder,所以命令的开头会有 λ

Typeperf 帮助

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
>λ  Typeperf -h

>Microsoft ® TypePerf.exe (10.0.17763.1)

>Typeperf 将性能数据写入命令窗口或日志文件。要停止 Typeperf,请按 CTRL+C.

>用法:
>C:\Windows\system32\typeperf.exe { <counter [counter ...]> | -cf <filename> | -q [object] | -qx [object] } [options]

>参数:
<counter [counter ...]> 要监视的性能计数器。

>选项:
-? 显示跟上下文相关的帮助。
-f <CSV|TSV|BIN|SQL> 输出文件格式。默认值是 CSV。
-cf <filename> 含有监视的性能计数器的文件,一个计数器一行。
-si <[[hh:]mm:]ss> 示例间的时间。默认值是 1 秒。
-o <filename> 输出文件或 SQL 数据库的路径。默认值为 STDOUT。
-q [object] 列出已安装的计数器(无实例)。要列出某个对象的计数器,包括对象名,如 Processor。
-qx [object] 列出已安装的计数器(带实例)。要列出某个对象的计数器,包括对象名,如 Processor。
-sc <samples> 要收集的示例数量。默认值为,在 CTRL+C 之前都进行采样。
-config <filename> 含有命令选项的设置文件。
-s <computer_name> 在计数器路径中没有指定服务器的情况下要监视的服务器。
-y 不用提示对所有问题都回答 yes。

>注意:
Counter 是性能计数器的全名,格式为
"\\<Computer>\<Object>(<Instance>)\<Counter>";
例如 "\\Server1\Processor(0)\% User Time"。

>示例:
typeperf "\Processor(_Total)\% Processor Time"
typeperf -cf counters.txt -si 5 -sc 50 -f TSV -o domain2.tsv
typeperf -qx PhysicalDisk -o counters.txt

操作步骤

目标: 监控 idea64 并记录成 csv 文件

  1. 获取可用监视器

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    λ  Typeperf -qx Process | findstr "idea64"
    \Process(idea64)\% Processor Time
    \Process(idea64)\% User Time
    \Process(idea64)\% Privileged Time
    \Process(idea64)\Virtual Bytes Peak
    \Process(idea64)\Virtual Bytes
    \Process(idea64)\Page Faults/sec
    \Process(idea64)\Working Set Peak
    \Process(idea64)\Working Set
    \Process(idea64)\Page File Bytes Peak
    \Process(idea64)\Page File Bytes
    \Process(idea64)\Private Bytes
    \Process(idea64)\Thread Count
    \Process(idea64)\Priority Base
    \Process(idea64)\Elapsed Time
    \Process(idea64)\ID Process
    \Process(idea64)\Creating Process ID
    \Process(idea64)\Pool Paged Bytes
    \Process(idea64)\Pool Nonpaged Bytes
    \Process(idea64)\Handle Count
    \Process(idea64)\IO Read Operations/sec
    \Process(idea64)\IO Write Operations/sec
    \Process(idea64)\IO Data Operations/sec
    \Process(idea64)\IO Other Operations/sec
    \Process(idea64)\IO Read Bytes/sec
    \Process(idea64)\IO Write Bytes/sec
    \Process(idea64)\IO Data Bytes/sec
    \Process(idea64)\IO Other Bytes/sec
    \Process(idea64)\Working Set - Private
  2. 监控程序的硬盘读写

    1
    λ  Typeperf -qx Process | findstr "idea64" | findstr "IO" >> counters.txt

    这时候会将一下数据写进 counters.txt 文本(该目录不存在该文件时会自动创建文件,但如果不存在父级目录则会创建失败,存在时会将数据写到文本的末尾)

    1
    2
    3
    4
    5
    6
    7
    8
    \Process(idea64)\IO Read Operations/sec
    \Process(idea64)\IO Write Operations/sec
    \Process(idea64)\IO Data Operations/sec
    \Process(idea64)\IO Other Operations/sec
    \Process(idea64)\IO Read Bytes/sec
    \Process(idea64)\IO Write Bytes/sec
    \Process(idea64)\IO Data Bytes/sec
    \Process(idea64)\IO Other Bytes/sec

    注:
    建议打开文本确认一下编码格式,我这里是 UCS-2 LE BOM,这种格式在我的环境中,后续的操作是无法识别文本内容的,建议改成 utf-8
    在我的环境中,如果不修改编码格式,就会报下面这个错误。

    错误: 没有有效计数器。

  3. 监控

    1
    2
    3
    λ  typeperf -f CSV -o test.csv -si 5 -sc 5 -cf .\counters.txt

    命令成功结束。

    等待命令完成,则 test.csv 中会存在需要的数据了。
    内容大致如下:

    1
    2
    3
    4
    5
    6
    "(PDH-CSV 4.0) (","\\COMPUTER-NAME\Process(idea64)\IO Read Operations/sec","\\COMPUTER-NAME\Process(idea64)\IO Write Operations/sec","\\COMPUTER-NAME\Process(idea64)\IO Data Operations/sec","\\COMPUTER-NAME\Process(idea64)\IO Other Operations/sec","\\COMPUTER-NAME\Process(idea64)\IO Read Bytes/sec","\\COMPUTER-NAME\Process(idea64)\IO Write Bytes/sec","\\COMPUTER-NAME\Process(idea64)\IO Data Bytes/sec","\\COMPUTER-NAME\Process(idea64)\IO Other Bytes/sec"
    "04/24/2019 14:58:16.286"," "," "," "," "," "," "," "," "
    "04/24/2019 14:58:21.343","4.5609839747845013","0","4.5609839747845013","11.105004460344873", "362.69737782090664","0","362.69737782090664","177.68007136551796"
    "04/24/2019 14:58:26.368","5.1746771996557648","0","5.1746771996557648","12.5386409068582", "433.47872849424061","0","433.47872849424061","197.43383777148148"
    "04/24/2019 14:58:31.394","0.79587117949501573","0","0.79587117949501573","4.9741948718438485", "83.765441641850401","0","83.765441641850401","78.990214564880318"
    "04/24/2019 14:58:36.421","6.5637937200127858","0","6.5637937200127858","15.116615840029446", "530.47387428103332","0","530.47387428103332","241.86585344047114"