C#高级编程性能监视.doc_第1页
C#高级编程性能监视.doc_第2页
C#高级编程性能监视.doc_第3页
C#高级编程性能监视.doc_第4页
C#高级编程性能监视.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

性能监视可以用于获取正常运行的服务的信息。性能监视是一个很好的工具,它能帮助我们了解系统的工作负荷,观察变化及趋势。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 / sec一秒内发送给客户机的#字节NumberOfItems32# of Requests请求的总数#NumberOfItems32# of Requests / sec一秒内请求的总数#NumberOfItems32Performance 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现在,就可以监视

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论