Performance Counter的使用
原文地址:http://blog.csdn.net/jiangxinyu/article/details/5480401
一 PerformanceCounter 基本介绍
1 简单介绍
表示 Windows NT 性能计数器组件
命名空间:System.Diagnostics
程序集:System(在 system.dll 中)
2 构造函数(只介绍本文要用到的)
PerformanceCounter (String, String, String)
功能:
初始化 PerformanceCounter 类的新的只读实例,
并将其与本地计算机上指定的系统性能计数器或自定义性能计数器及类别实例关联
参数说明:
public PerformanceCounter (
string categoryName,
string counterName,
string instanceName
)
categoryName
性能计数器关联的性能计数器类别(性能对象)的名称。
counterName
性能计数器的名称。
instanceName
性能计数器类别实例的名称,或者为空字符串 ("")(如果该类别包含单个实例)。
二 示例方法:
需要引用命名空间
using System.Threading;
using System.Collections;
1 获取性能计数器类别列表
虽然系统中有很多可用的计数器类别,但与之交互最频繁的可能是“Cache”(缓存)、“Memory”(内存)、
“Objects”(对象)
、“PhysicalDisk”(物理磁盘)、“Process”(进程)、“Processor”(处理器)、
“Server”(服务器)、“System”(系统)和“Thread”(线程)等类别
{
PerformanceCounterCategory[] myCat2;
myCat2 = PerformanceCounterCategory.GetCategories();
for (int i = 0; i < myCat2.Length; i++)
{
Console.WriteLine(myCat2[i].CategoryName.ToString());
}
}
2 获取性能计数器类别下的实例的名称实例下的性能计数器的名称
{
string[] instanceNames;
ArrayList counters = new ArrayList();
PerformanceCounterCategory mycat = new PerformanceCounterCategory(CategoryName);
try
{
instanceNames = mycat.GetInstanceNames();
if (instanceNames.Length == 0)
{
counters.AddRange(mycat.GetCounters());
}
else
{
for (int i = 0; i < instanceNames.Length; i++)
{
counters.AddRange(mycat.GetCounters(instanceNames[i]));
}
}
for (int i = 0; i < instanceNames.Length; i++)
{
Console.WriteLine(instanceNames[i]);
}
Console.WriteLine("******************************");
foreach (PerformanceCounter counter in counters)
{
Console.WriteLine(counter.CounterName);
}
}
catch (Exception)
{
Console.WriteLine("Unable to list the counters for this category");
}
}
3 根据categoryName,counterName,instanceName获得性能情况显示
{
PerformanceCounter pc = new PerformanceCounter(CategoryName, CounterName, InstanceName);
while (true)
{
Thread.Sleep(1000); // wait for 1 second
float cpuLoad = pc.NextValue();
Console.WriteLine("CPU load = " + cpuLoad + " %.");
}
}
4 调用方法3显示cpu使用率
Performance Counter的使用
客户端性能测试通过performanceCounter监控客户端性能指标
PerformanceCounter PTCounter = new PerformanceCounter("Process",
"% Processor Time",
"AliIM");
logfile("% Processor Time:" + PTCounter.NextValue().ToString());
//内存
PerformanceCounter WSCounter = new PerformanceCounter("Process",
"Working Set",
"AliIM");
logfile("Working Set:" + ((double)WSCounter.NextValue() / 1024).ToString());
//内存最高值
PerformanceCounter MemeryCounter = new PerformanceCounter("Process",
"Working Set Peak",
"AliIM");
logfile("Working Set Peak:" + ((double)MemeryCounter.NextValue() / 1024).ToString());
//虚拟内存
PerformanceCounter PBCounter = new PerformanceCounter("Process",
"Private Bytes",
"AliIM");
logfile("Private Bytes:" + ((double)PBCounter.NextValue() / 1024).ToString());
//句柄数
PerformanceCounter HCCounter = new PerformanceCounter("Process",
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2014-09-16 打开网页自动弹出QQ临时会话 (打开网站弹出QQ聊天) qq.js文件代
2011-09-16 LINQ to XML 编程基础 创建 增删改查