利用Attribute实现对性能计数器的封装
2008-12-09 10:17
218 查看
本文装载自:http://it.dianping.com/using_attribute_wrapping_performance_counter.htm
为要使用性能计数器对程序做监控,所以去MSDN查了一下性能计数器的实现,看完第一个感觉就是。。好麻烦。。如果要用计数器,要实现以下几步:
新建计数器分类
新建计数器集
新建计数器
创建计数器实例
初始化计数器实例
使用计数器实例
我想如果对于开发人员来说,为了实现监控,要写那么一大坨初始化代码,肯定晕倒。。所以有必要对性能计数器的实现做一下封装,让开发变得更加方便简单。
网上也看了很多种计数器的封装实现,大多是用工厂模式进行的封装,但感觉还是会有一定的冗余代码。于是尝试了一下使用Attribute方式进行封装,感觉效果还不错,所以和大家分享一下。
我预期的效果是这样的,首先是定义计数器集:
通过这样的封装,大部分的重复代码都被包起来了,使用起来会方便很多。具体的代码我附在参考资料里,供大家学习研究,望多提宝贵意见:)
本文转载自:http://it.dianping.com/using_attribute_wrapping_performance_counter.htm
参考资料下载: http://it.dianping.com/using_attribute_wrapping_performance_counter.htm
为要使用性能计数器对程序做监控,所以去MSDN查了一下性能计数器的实现,看完第一个感觉就是。。好麻烦。。如果要用计数器,要实现以下几步:
新建计数器分类
新建计数器集
新建计数器
创建计数器实例
初始化计数器实例
使用计数器实例
我想如果对于开发人员来说,为了实现监控,要写那么一大坨初始化代码,肯定晕倒。。所以有必要对性能计数器的实现做一下封装,让开发变得更加方便简单。
网上也看了很多种计数器的封装实现,大多是用工厂模式进行的封装,但感觉还是会有一定的冗余代码。于是尝试了一下使用Attribute方式进行封装,感觉效果还不错,所以和大家分享一下。
我预期的效果是这样的,首先是定义计数器集:
public static bool Register<T>() where T : class, new() { //取得category名字 object[] attribs = typeof(T).GetCustomAttributes(typeof(CounterCategoryAttribute), false); if (attribs.Length == 0) return false; CounterCategoryAttribute attr = (CounterCategoryAttribute)attribs[0]; string category = attr.Name; //检查category是否已存在 bool countersExist = PerformanceCounterCategory.Exists(category); //如果不存在,需要创建 if (countersExist == false) { //创建一个计数器集 CounterCreationDataCollection list = new CounterCreationDataCollection(); foreach (FieldInfo prop in typeof(T).GetFields()) { attribs = prop.GetCustomAttributes(typeof(CounterUnitAttribute), false); foreach (CounterUnitAttribute fieldAttrib in attribs) { //创建计数器 CounterCreationData data = new CounterCreationData(); data.CounterName = fieldAttrib.Name; data.CounterHelp = fieldAttrib.Help; data.CounterType = fieldAttrib.Type; //加入计数器集 list.Add(data); } } //创建category和计数器集 PerformanceCounterCategory.Create(category, "", PerformanceCounterCategoryType.SingleInstance, list); } //创建并绑定计数器实例 T instance = new T(); foreach (FieldInfo prop in typeof(T).GetFields()) { attribs = prop.GetCustomAttributes(typeof(CounterUnitAttribute), false); foreach (CounterUnitAttribute fieldAttrib in attribs) { PerformanceCounter pc = new PerformanceCounter(category, fieldAttrib.Name, "",false); pc.RawValue = 0; prop.SetValue(instance, pc); } } //存入hash表中,供Resolve函数读取 _cntList.Add(typeof(T),instance); return true; }
通过这样的封装,大部分的重复代码都被包起来了,使用起来会方便很多。具体的代码我附在参考资料里,供大家学习研究,望多提宝贵意见:)
本文转载自:http://it.dianping.com/using_attribute_wrapping_performance_counter.htm
参考资料下载: http://it.dianping.com/using_attribute_wrapping_performance_counter.htm
相关文章推荐
- 利用cacti和性能计数器实现自定义远程监控
- 利用snmptools实现cacti自定义监控Windows性能计数器
- 利用cacti和性能计数器实现自定义远程监控
- 如何利用内部类的类型封装功能实现线程安全的类以提高程序的性能
- 利用Java针对MySql封装的jdbc框架类 JdbcUtils 完整实现(包含增删改查、JavaBean反射原理,附源码)
- C#利用Emit反射实现AOP,以及平台化框架封装思路
- 性能监视计数器封装组件PDHWrapper说明
- 利用Attribute实现AOP
- 利用 C++ 11 特性实现多线程计数器
- 利用C++模板,代替虚函数,实现类的静态多态性(加入性能测试部分)
- 利用Windows性能计数器(PerformanceCounter)监控
- 利用SpringMVC原生封装的对象实现图片上传及图片回显功能的实现及案例代码
- 利用FutureTask和ExecutorService实现一个任务拆分成多个任务,实现性能提高
- 利用Fitnesse和Jmeter实现接口性能测试
- 利用Windows性能计数器(PerformanceCounter)监控
- 学习笔记---元数据、程序集、GAC版本控制、属性(Attribute)、反射(利用.NET编译器实现表达式计算器)
- epoll定时器实现系列文章:linux c++ 利用timerfd和epoll封装计时器(Timer)类
- 课时43|封装-方法重载,包的实现和利用
- 利用《双缓冲技术及其在VC的GDI环境下的实现》理解armv7计数器中的双缓冲技术
- 利用Attribute实现的 MVC动态表单