C# 性能诊断工具 dotnet-counters 的使用

发布时间 2023-09-14 14:35:15作者: 蛋蛋十二月

创建.NET程序Dump的几种姿势

下载 dotnet-counters 工具

简介

dotnet-counters 是一个性能监视工具,用于初级运行状况监视和性能调查。它通过 EventCounter API 观察已发布的性能计数器值。例如,可以快速监视CUP使用情况或.NET Core 应用程序中的异常率等指标

安装

通过nuget包安装:

dotnet tool install --global dotnet-counters

主要命令

1、dotnet-counters ps 查看服务器上运行中的Dotnet进程列表

2、dotnet-counters list 显示可用的计数器名称和说明(场景1和2都可以用这个查看说明)。

3、dotnet-counters collect 定期收集所选计数器的值,并将它们导出为指定的文件格式以进行后续处理。

4、dotnet-counters monitor 监控并显示所选计数器(dotnet-counters list可查看有哪些计数器可用)的定期刷新值。

使用方法:

1、dotnet-counters ps 查看进程ID

2、dotnet-counters monitor --process-id 14613 --refresh-interval 1

场景一:dotnet-counters monitor -p 14660 --refresh-interval 1 监视id为14660的进程,每隔1秒钟刷新一次。

[System.Runtime]
    % Time in GC since last GC (%)                                 0    #GC垃圾回收百分比
    Allocation Rate (B / 1 sec)                               73,440    #内存分配量
    CPU Usage (%)                                                  0    #CPU使用率
    Exception Count (Count / 1 sec)                                0    #异常数量
    GC Fragmentation (%)                                           5.437#GC 堆碎片率
    GC Heap Size (MB)                                             22    #GC堆内存分配量(消耗内存)
    Gen 0 GC Count (Count / 1 sec)                                 0    #0代垃圾回收次数
    Gen 0 Size (B)                                         1,042,696    #0代垃圾回收大小
    Gen 1 GC Count (Count / 1 sec)                                 0    #1代垃圾回收次数
    Gen 1 Size (B)                                           180,520    #1代垃圾回收大小
    Gen 2 GC Count (Count / 1 sec)                                 0    #2代垃圾回收次数
    Gen 2 Size (B)                                         2,014,768    #2代垃圾回收大小
    IL Bytes Jitted (B)                                      448,237    #JIT编译的IL总节数
    LOH Size (B)                                          16,875,680    #大对象占用内存(大于85000字节的对象)
    Monitor Lock Contention Count (Count / 1 sec)                  0    #锁竞争次数
    Number of Active Timers                                        3    #活动计数器
    Number of Assemblies Loaded                                  108    #程序集加载数量
    Number of Methods Jitted                                   6,086    #JIT编译的方法总数
    POH (Pinned Object Heap) Size (B)                        113,608    #固定对象堆的字节数
    ThreadPool Completed Work Item Count (Count / 1 sec)           2    #线程池完成任务数量
    ThreadPool Queue Length                                        0    #线程池工作项队列长度
    ThreadPool Thread Count                                        5    #线程池线程数量
    Working Set (MB)                                              64    #总分配内存

场景二:dotnet-counters monitor -p 32693 --refresh-interval 1 --counters Microsoft.AspNetCore.Hosting 监视id为32693的Web进程,每隔1秒钟刷新一次。

[Microsoft.AspNetCore.Hosting]
    Current Requests                                   0    #当前请求数量
    Failed Requests                                    0    #失败数量
    Request Rate (Count / 1 sec)                       1    #每秒请求数量
    Total Requests                                     3    #总请求数量

参考:https://blog.csdn.net/qq_26900081/article/details/123734674

安装注意事项:

1、安装的版本要和Dotnet SDK版本一致

2、检查Dotnet SDK版本:

dotnet --list-sdks

下载地址目录:

dotnet tool install 命令 - .NET CLI | Microsoft Learn

如下载指定版本号:

dotnet tool install --global dotnet-dump --version 3.1.412

dotnet tool install --global dotnet-counters --version 3.1.412

dotnet tool install --global dotnet-trace --version 3.1.412

参考:https://zhuanlan.zhihu.com/p/463827397

微软官方提供:https://learn.microsoft.com/zh-cn/dotnet/core/diagnostics/dotnet-trace