C#高级编程:性能监视C#高级编程:性能监视
,
性能监视可以用于获取正常运行的服务的信息。性能监视是一个很好的工具,它能帮助我们了解系统的工作负荷,观察变化及趋势。
Windows 2000有许多性能对象,例如System、Memory、Objects、Process、Processor、Thread和Cache等。这些对象都有许多的监视点。例如,使用Process对象,可以监视所有进程或某一具体进程的用户时间、句柄数、页错误和线程数等。一些应用程序也添加具体的对象,例如SQL Server。
对于QuoteService示例应用程序而言,...
C#高级编程:性能监视
,
性能监视可以用于获取正常运行的服务的信息。性能监视是一个很好的工具,它能帮助我们了解系统的工作负荷,观察变化及趋势。
Windows 2000有许多性能对象,例如System、Memory、Objects、Process、Processor、Thread和Cache等。这些对象都有许多的监视点。例如,使用Process对象,可以监视所有进程或某一具体进程的用户时间、句柄数、页错误和线程数等。一些应用程序也添加具体的对象,例如SQL Server。
对于QuoteService示例应用程序而言,要获取的信息是客户请求的数量和通过网络发送的数据有多少等。
1. 性能监视类
System.Diagnostics命名空间中包含下述性能监视类:
? PerformanceCounter类可以用于监视数量和编写数量。此外,使用这
个类还可以创建新的性能种类。
? 使用PerformanceCounterCategory可以遍历所有现有的种类并创建
新的种类。可以编程获取种类的记数器。
? PerformanceCounterInstaller类用于性能记数器的安装。这个类的用
法与前面的EventLogInstaller相似。
2. Performance Counter Builder
要创建新的性能记数器种类,可以选择Server Explorer中的性能记数器,再在弹出的菜单中选择菜单项Create New Category„,这将启动Performance Counter
Builder,如图32-25所示。
图 32-25
把性能记数器种类设置为Quote Service。
32-6中给出了服务的所有性能记数器。
表 32-6
名 称 描 述 类 型
发送给客户机的#字节总量 # of Bytes sent NumberOfItems32
一秒内发送给客户机的#字节 # of Bytes sent / NumberOfItems32
sec
请求的总数# # of Requests NumberOfItems32
一秒内请求的总数# # of Requests / sec NumberOfItems32 Performance Counter Builder把配置写到性能数据库中。使用System.Diagnostics命名空间中PerformanceCategory类的Create()方法,可以动态地把配置写到性能数据库中。使用Visual Studio .NET,可以在以后为其他系统添加安装程序。
3. 添加PerformanceCounter组件
接下来,要从工具箱中添加PerformanceCounter组件。这里不使用工具箱的种类组件,而是直接把前面创建的性能计数从Server Explorer拖放到
视图上。
这样实例会自动配置:所有对象的CategoryName属性都设置为Quote Service
Count,CounterName属性设置为选中种类中的一个可用值。这个应用程序不是
读取性能计数,而是写入,所以必须把ReadOnly属性设置为false。
private void InitializeComponent()
{
//...
// performanceCounterRequestsPerSec
//
this.performanceCounterRequestsPerSec.CategoryName = "Quote Service Counts";
this.performanceCounterRequestsPerSec.CounterName = "# of Requests / sec";
this.performanceCounterBytesSentTotal.MachineName = "NAGELC"
this.performanceCounterRequestsPerSec.ReadOnly = false;
//
// performanceCounterBytesSentTotal
//
this.performanceCounterBytesSentTotal.CategoryName = "Quote Service Counts";
this.performanceCounterBytesSentTotal.CounterName = "# of Bytes sent";
this.performanceCounterBytesSentTotal.MachineName = "NAGELC"
this.performanceCounterBytesSentTotal.ReadOnly = false;
//
// performanceCounterBytesSentPerSec
//
this.performanceCounterBytesSentPerSec.CategoryName = "Quote Service Counts";
this.performanceCounterBytesSentPerSec.CounterName = "# of Bytes sent / sec";
this.performanceCounterBytesSentTotal.MachineName = "NAGELC"
this.performanceCounterBytesSentPerSec.ReadOnly = false;
//
// performanceCounterRequestsTotal
//
this.performanceCounterRequestsTotal.CategoryName =
"Quote Service Counts";
this.performanceCounterRequestsTotal.CounterName = "# of Requests";
this.performanceCounterBytesSentTotal.MachineName = "NAGELC"
this.performanceCounterRequestsTotal.ReadOnly = false; //...
}
对于性能值的计算,必须给类QuoteServer添加两个私有变量requestPerSec
和bytesPerSec。
public class QuoteServer : System.ComponentModel.Component {
private int requestsPerSec;
private int bytesPerSec;
在QuoteServer类的Listener()方法中,直接增加显示总值的性能计数。
PerformanceCounter.Increment()用于计算请求的总数,而IncrementBy()方法计算发送的字节总数。
对于按秒显示值的性能计数而言,在Listener()方法中只更新requestsPerSec和bytessPerSec变量:
protected void Listener()
{
try
{
listener = new TCPListener(port);
listener.Start();
while (true)
{
Socket socket = listener.Accept();
string message = GetRandomQuoteOfTheDay();
UnicodeEncoding encoder = new UnicodeEncoding();
byte[] buffer = encoder.GetBytes(message);
socket.Send(buffer, buffer.Length, 0);
socket.Close();
performanceCounterRequestsTotal.Increment();
performanceCounterBytesSentTotal.IncrementBy(buffer.Length);
requestsPerSec++;
bytesPerSec += buffer.Length;
}
}
catch (Exception e)
{
string message = "Quote Server failed in Listener: "
+ e.Message;
eventLog.WriteEntry(message, EventLogEntryType.Error); }
}
为了每秒显示一次已更新的值,可以添加一个Timer组件。把OnTime()方法设
置为这个组件的Elapsed事件。如果Interval属性设置为1000,OnTime()方法
就每秒调用一次,它使用PerformanceCounter类的RawValue属性设置性能计数:
protected void OnTimer (object sender, System.Timers. ElapsedEventArgs e)
{
performanceCounterBytesSentPerSec.RawValue = bytesPerSec; performanceCounterRequestsPerSec.RawValue = requestsPerSec; bytesPerSec = 0;
requestsPerSec = 0;
}
6. perfmon.exe
现在,就可以监视服务了。执行Administrative Tools | Performance命令可以启用Performance工具。按下工具栏中的“,”按钮,可以添加性能计数。图32-26中显示出的性能对象是Quote Service,配置的所有记数器都显示在记数器列表中。
图 32-26
在添加记数器之后,可以看到服务的计数。使用这个性能工具,也可以创建日志文件,以便将来分析性能,如图32-27所示。
图 32-27
本文档为【C#高级编程:性能监视】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。