使用性能计数器检测应用程序的性能!

  1.  创建一个简单的 web service, 这个演示程序就是把客户端对 web service 中的两个 webmethod 总访问量,每秒访问量,总出错次数,每秒出错次数以及调用延迟的情况在性能计数器中显示出来,web service中的代码相当简单,事实上基本上看不到延迟的存在,这也只是为了演示用。
 1 None.gif using  System;
 2 None.gif using  System.Web;
 3 None.gif using  System.Web.Services;
 4 None.gif using  System.Web.Services.Protocols;
 5 None.gif
 6 None.gif[WebService(Namespace  =   " http://tempuri.org/ " )]
 7 None.gif[WebServiceBinding(ConformsTo  =  WsiProfiles.BasicProfile1_1)]
 8 None.gif public   class  Service : System.Web.Services.WebService
 9 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
10ExpandedSubBlockStart.gifContractedSubBlock.gif    public Service () dot.gif{
11InBlock.gif
12InBlock.gif        //Uncomment the following line if using designed components 
13InBlock.gif        //InitializeComponent(); 
14ExpandedSubBlockEnd.gif    }

15InBlock.gif
16InBlock.gif    [WebMethod]
17ExpandedSubBlockStart.gifContractedSubBlock.gif    public string SayHelloTo(string someBody) dot.gif{
18InBlock.gif        return "Hello "+someBody;
19ExpandedSubBlockEnd.gif    }

20InBlock.gif    [WebMethod]
21InBlock.gif    public string SayWelcomeTo(string someBody)
22ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
23InBlock.gif        return "Welcome, " + someBody;
24ExpandedSubBlockEnd.gif    }

25InBlock.gif    
26ExpandedBlockEnd.gif}

27 None.gif

2.     创建性能计数器,这里用一个简单的 cs 文件替代了 installer 文件,只需用csc命令讲下面的代码编译成exe文件,然后运行exe文件就可以在系统中创建自己的性能计数器。注意:我们并不需要做总访问次数和时间的除法运算来得到每秒的访问次数,PerformanceCounterType枚举类型专门负责这个,详细的信息请查阅msdn.
 1 None.gif using  System;
 2 None.gif using  System.Diagnostics;
 3 None.gif
 4 None.gif namespace  ZZQ.Net.Demo
 5 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 6InBlock.gif    public class Installer
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 8InBlock.gif
 9InBlock.gif        public static void Main()
10ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
11InBlock.gif            try
12ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
13InBlock.gif                if (PerformanceCounterCategory.Exists("MyPerfCategory"))
14ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
15InBlock.gif                    PerformanceCounterCategory.Delete("MyPerfCategory");
16ExpandedSubBlockEnd.gif                }

17InBlock.gif
18InBlock.gif               
19InBlock.gif                CounterCreationDataCollection counterDatas = new CounterCreationDataCollection();
20InBlock.gif
21InBlock.gif                counterDatas.Add(new CounterCreationData("calls total""number of service calls",
22InBlock.gif                    PerformanceCounterType.NumberOfItems64));
23InBlock.gif                counterDatas.Add(new CounterCreationData("calls / sec""number of service calls per second.",
24InBlock.gif                    PerformanceCounterType.RateOfCountsPerSecond64));
25InBlock.gif                counterDatas.Add(new CounterCreationData("errors total",
26InBlock.gif                    "number of errors returned form service to the client.",
27InBlock.gif                    PerformanceCounterType.NumberOfItems64));
28InBlock.gif                counterDatas.Add(new CounterCreationData("errors / sec",
29InBlock.gif                    "number of errors returned form service to the client per second.",
30InBlock.gif                    PerformanceCounterType.RateOfCountsPerSecond64));
31InBlock.gif                counterDatas.Add(new CounterCreationData("average processing time",
32InBlock.gif                    "average call processing time in milliseconds.",
33InBlock.gif                    PerformanceCounterType.AverageCount64));
34InBlock.gif                counterDatas.Add(new CounterCreationData("average processing time base""",
35InBlock.gif                    PerformanceCounterType.AverageBase));
36InBlock.gif                counterDatas.Add(new CounterCreationData("Processing time latency",
37InBlock.gif                    "Processing time in milliseconds.",
38InBlock.gif                    PerformanceCounterType.NumberOfItems32));
39InBlock.gif
40InBlock.gif                PerformanceCounterCategory.Create("MyPerfCategory""It is just for demonstration purpose!", PerformanceCounterCategoryType.MultiInstance, counterDatas);
41InBlock.gif
42InBlock.gif
43ExpandedSubBlockEnd.gif            }

44InBlock.gif            catch (Exception ex)
45ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
46InBlock.gif                Console.WriteLine("Error occurred: " + ex.ToString());
47ExpandedSubBlockEnd.gif            }

48InBlock.gif            Console.ReadLine();
49ExpandedSubBlockEnd.gif        }

50ExpandedSubBlockEnd.gif    }

51ExpandedBlockEnd.gif}

52 None.gif

3. 客户端的代码使用两个 timer tick 事件中生成的随机数做循环调用模拟当前的访问量,并且在每次调用的时候更新计数器,代码很简单。
 1 None.gif using  System;
 2 None.gif using  System.Collections.Generic;
 3 None.gif using  System.ComponentModel;
 4 None.gif using  System.Data;
 5 None.gif using  System.Drawing;
 6 None.gif using  System.Text;
 7 None.gif using  System.Windows.Forms;
 8 None.gif using  PerformanceCounterDemo.localhost;
 9 None.gif using  ZZQ.Net.Demo;
10 None.gif
11 None.gif
12 None.gif namespace  PerformanceCounterDemo
13 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
14InBlock.gif    public partial class Demo : Form
15ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
16InBlock.gif       
17InBlock.gif        public Demo()
18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
19InBlock.gif            InitializeComponent();
20ExpandedSubBlockEnd.gif        }

21InBlock.gif
22InBlock.gif        private void btnHello_Click(object sender, EventArgs e)
23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
24InBlock.gif            timer1.Enabled = true;
25ExpandedSubBlockEnd.gif        }

26InBlock.gif
27InBlock.gif
28InBlock.gif
29InBlock.gif        private void btnWelcome_Click(object sender, EventArgs e)
30ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
31InBlock.gif            timer2.Enabled = true;
32ExpandedSubBlockEnd.gif        }

33InBlock.gif
34InBlock.gif        private void btnStop_Click(object sender, EventArgs e)
35ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
36InBlock.gif
37InBlock.gif            timer1.Enabled = false;
38InBlock.gif            timer2.Enabled = false;
39ExpandedSubBlockEnd.gif        }

40InBlock.gif        private void timer1_Tick(object sender, EventArgs e)
41ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
42InBlock.gif            Random r = new Random();
43InBlock.gif            int counter = r.Next(30);
44InBlock.gif            CallHello(counter);
45InBlock.gif
46ExpandedSubBlockEnd.gif        }

47InBlock.gif
48InBlock.gif        private void timer2_Tick(object sender, EventArgs e)
49ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
50InBlock.gif            Random r = new Random();
51InBlock.gif            int counter = r.Next(30);
52InBlock.gif            CallWelcome(counter);
53ExpandedSubBlockEnd.gif        }

54InBlock.gif
55InBlock.gif
56InBlock.gif        private void CallHello(int counter)
57ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
58InBlock.gif            Service s = new Service();
59InBlock.gif            string strResult = string.Empty;
60InBlock.gif            for (int i = 0; i < counter; i++)
61ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
62InBlock.gif                DateTime start = DateTime.Now;
63InBlock.gif                strResult += "The " + i.ToString() + " time,return value is " + s.SayHelloTo("zzq" + i.ToString()) + "\r\n";
64InBlock.gif                txtHello.Text = strResult;
65InBlock.gif               TimeSpan span = DateTime.Now.Subtract(start);
66InBlock.gif               PensPerfCounterManager.UpdatePerfCounters("MyPerfCategory""SayHelloTo", span, true);
67InBlock.gif             
68ExpandedSubBlockEnd.gif            }

69ExpandedSubBlockEnd.gif        }

70InBlock.gif        private void CallWelcome(int counter)
71ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
72InBlock.gif            Service s = new Service();
73InBlock.gif            string strResult = string.Empty;
74InBlock.gif
75InBlock.gif            for (int i = 0; i < counter; i++)
76ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
77InBlock.gif                DateTime start = DateTime.Now;
78InBlock.gif                strResult += "The " + i.ToString() + " time,return value is " + s.SayHelloTo("zzq" + i.ToString()) + "\r\n";
79InBlock.gif                txtWelcome.Text = strResult;
80InBlock.gif                PensPerfCounterManager.UpdatePerfCounters("MyPerfCategory""SayWelcomeTo", start, true);
81ExpandedSubBlockEnd.gif            }

82ExpandedSubBlockEnd.gif        }

83InBlock.gif   
84ExpandedSubBlockEnd.gif    }

85ExpandedBlockEnd.gif}

我的代码中UpdatePerfCounters方法的最后一个参数总是传的true,因为这两个web method太简单了,基本不会出错。^_^。实际用的时候会针对是否出现异常来决定是否更新和错误相关的计数器。

4.运行客户端的程序然后在运行里键入perfmon打开性能计数器管理窗口,找到MyPerfCategory性能对象,并且添加所有所有计数器的所有实例。
客户端演示效果图:


性能计数器效果图:

转载于:https://www.cnblogs.com/netboy/archive/2007/04/12/711355.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值