源文地址 html
一 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
性能计数器类别实例的名称,或者为空字符串 ("")(若是该类别包含单个实例)。
二 示例方法:
须要引用命名空间
缓存
1 获取性能计数器类别列表
虽然系统中有不少可用的计数器类别,但与之交互最频繁的多是“Cache”(缓存)、“Memory”(内存)、
“Objects”(对象)服务器
、“PhysicalDisk”(物理磁盘)、“Process”(进程)、“Processor”(处理器)、
“Server”(服务器)、“System”(系统)和“Thread”(线程)等类别
函数
2 获取性能计数器类别下的实例的名称实例下的性能计数器的名称工具
3 根据categoryName,counterName,instanceName得到性能状况显示post
4 调用方法3显示cpu使用率性能
Performance Counter的使用测试
客户端性能测试经过performanceCounter监控客户端性能指标ui
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());this
//内存最高值
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",
"Handle Count",
"AliIM");
logfile("Handle Count:" + HCCounter.NextValue() .ToString());
//线程数Thread Count
PerformanceCounter TCCounter = new PerformanceCounter("Process",
"Thread Count",
"AliIM");
logfile("Thread Count:" + TCCounter.NextValue() .ToString());
//补充获得GDI OBJECTS
Process process;
process = System.Diagnostics.Process.GetProcessesByName("AliIM")[0];
logfile("GDI Objects Count:" + GetGuiResources(process.Handle, 0));
[DllImport("User32")]
extern public static int GetGuiResources(IntPtr hProcess, int uiFlags);
好比咱们有这样一个需求:
我要编码方式记录咱们当前编写的程序每秒钟抛出异常数
若是咱们直接使用 Performance 工具,就是采用下图方式依次选择:
1、选择要作性能测试的计算机
2、选择要用那个 Proformance object; 这里咱们选择: .NET CLR Exceptions
3、选择 须要的计数项,这里咱们选 # of Exceps Thrown / sec
4、选择你要对那个程序进行测试(也就是那个进程产生的异常),在这里就请选择你要测试的程序名字
若是咱们但愿用编码方式来实现这个功能的话,也很简单:
System.Diagnostics.PerformanceCounter 就是编码得到性能计数的核心
这部分的代码以下:
System.Diagnostics.PerformanceCounter pc = new PerformanceCounter();
// 获取或设置此性能计数器的性能计数器类别的名称。
pc.CategoryName = ".NET CLR Exceptions";
// 获取或设置与此 PerformanceCounter 实例关联的性能计数器的名称。
pc.CounterName = "# of Exceps Thrown / sec";
// 获取或设置此性能计数器的实例名称。
pc.InstanceName =
System.IO.Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.SetupInformation.ApplicationName);
pc.ReadOnly = true;
Console.WriteLine(pc.RawValue);
Console.WriteLine(pc.NextValue());
int num = 30;
for (int i = 0; i < num; i++)
{
try
{
throw new Exception("test Exception");
}
catch (Exception)
{
}
}
// 获取或设置此计数器的原始值(即未通过计算的值)。
Console.WriteLine(pc.RawValue);
// 获取计数器样本并为其返回计算所得值。
Console.WriteLine(pc.NextValue());
Console.WriteLine("===========");
上述代码只是一个超简单的例子,实际性能计数能够写得比这个更复杂。
这时候,你能够参考如下几个类:
类 |
说明 |
System.Diagnostics.PerformanceCounter |
表示 Windows NT 性能计数器组件。使用该类读取现有预约义的或自定义的计数器并向自定义计数器发布(写入)性能数据。 |
System.Diagnostics.PerformanceCounterCategory |
提供与计数器交互的几种方法以及该计算机上计数器的类别。 |
System.Diagnostics.PerformanceCounterInstaller |
指定 PerformanceCounter 组件的安装程序。 |
System.Diagnostics.PerformanceCounterType |
指定用于计算 PerformanceCounter 的 NextValue 方法的公式。 |
附:
咱们这个例子中须要作的性能计数器:
# of Exceps Thrown / Sec(引起的异常数/秒)
显示每秒引起的异常的数目。它包括 .NET 异常和转换成 .NET 异常的非托管异常。例如,从非托管代码返回的 HRESULT 转换为托管代码中的异常。
此计数器包括已处理和未处理的异常。此计数器不是一段时间内的平均值;它显示在最后两个样本(以取样间隔持续时间来划分)中观察到的值之间的差别。此计数器是一个潜在性能问题(若是引起多于 100 个的较多数目的异常)的指示器。
Performance Counter的使用误区
2008-07-11 20:42
不少人在使用PerfomanceCounter的时候直接new PerfomanceCounter实例,而后就去调用NextValue()方法。这样每每获得的值是0.00,今天我也犯了这么错误,找个了半天,终于发现,performance counter在计算值得时候,须要两个样本,若是咱们获取到PerformanceCounter后直接调用NextValue()方法,则只会获取到第一个样本的值,该值每每会是0。 |