代码改变世界

.net垃圾回收学习【NET Best Practice No: 3:- Using performance counters to gather performance data】【翻译&&学习】

2011-09-12 11:03  一一九九  阅读(290)  评论(0编辑  收藏  举报

From:http://www.c-sharpcorner.com/UploadFile/shivprasadk/3253453409022009023755AM/32534534.aspx

注: 非一字一句的翻译,纯粹记录和学习。

如果你要描述一个程序的性能,需要对程序的性能有比较精准的描述:“with 2 gb ram, xyz processor and 2000 customer records the customer screen load in 20 secs”, 而描述程序性能的一个工具就是performance counters.

At the end of the day its count, calculate and display


任何性能评估方法都是先计数、然后计算和呈现性能结果。比如说你想计算内存中每秒被处理的页面数量,首先需要计算有多少页面和花费了多少时间,一旦计数完毕,我们需要计算结果,比如说使用页面数除以消耗的时间,最终我们需要呈现我们性能的结果。

3

现在我们知道性能评估的三步曲:count, calculate, display.计数的部分是由应用程序来完成的,所以应用程序需要在技术的结算提供数据,需要注意数据并不是被性能计数器自动侦知的,是应用程序提供的。计算和显示的过程是由性能技术和监视器来完成的。

4

Performance counters are not magicians


假如应用程序没有提供计数的数据,性能计数器不会自己做评估,换句话说,性能计数器需要应用程序提供数据来支持其操作。

Types of measures in application


所有的应用程序性能评估分为如下几类:
  • instantaneous values: 许多时候我们只想得到最新的数据。比如说我们只想知道有多少客户的数据被处理了,有多少RAM被使用了。这种类型的测试被称为instantaneous 或者absolute values.
  • Average values: 有时候即时数据不能说明问题,比如说仅仅说应用程序消耗了1gb的内存是不够哦,我们需要得到某些平均值。比如说每1000条记录消耗了10M内存,这样我们就能够知道应用程序内部发生了什么时期。性能计数器通过平均性能计数器来支持这种类型的测量。比如说AverageBase, AverageTimer32, AverageCount64等。
  • Rate Values:  有时候你需要知道某些事件相对于时间的比率。比如说你想知道每秒种被处理的记录数。 Rate Counter能够帮我们计算这种类型的度量维度。
  • Percentage Values: 出于对比的目的,我们希望看到某些数据以百分比的形式出现。比如说你想在两台计算机之间对比性能数据。直接对比数据不能得到一个比较公正的结果,假如我们使用来自两台计算的% 数值的话,结果将更有意义。假如我们想在不同的计数器上对比数值,百分比比绝对值更合适一些。
  • Difference Values:  更多的时候我们希望得到不同的性能数据,比如说应用程序启动了多长时间了,硬盘从那时被使用了多少。为了收集这些类型的数据,我们记录最初的数值和最近的数据。

概括如下:

5

Example on which performance counter will be tested


接下来我们将做一个简单的示例: 该示例有一个计数器,这个计数器每100毫秒生成一个随机的数值,这这些随机的数值随后被检查是否小于2,如果小于2那么函数Myfuction会被激活。

6

代码如下:

private void timer1_Tick(object sender, EventArgs e)
{
// Generate random number between 1 to 5.
Random objRnd = new Random();
int y = objRnd.Next(1, 5);
 
// If random number is less than 2 call my Function
if (y > 2)
{
MyFunction();
}
}
  
//Below is the code for 'MyFunction' which is invoked when the value of random number is less than 2. The method does not do anything as such.
 
private void MyFunction()
{
 
}

Adding our first instantaneous performance counter in 4 steps


再开始添加性能计数器前,我们先要理解性能计数器的结构。当我们创建一个性能计数器的时候需要将其添加到某个组中。所以我们需要创建一个目录然后将性能计数器放到那个目录下。

7

我们目前只想计算MyFunction被调用了多少次,我们来创建一个叫做“NumberofTimeFunctionCalled”的Instantaneous Counter。在继续之前,我们先看一下性能技术器提供的instantaneous counts的类型。

参见:http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecountertype.aspx.

numberofItem32:  呈现最近观察到的数据。

NumberofItems64:  呈现最近观察到的数据,用于比ijaoda的数据或者操作。

numberofitemsHex32.  呈现最近观察到的数据。以十六进制的形式呈现。

numberofitemsHex64。呈现最近观察到的数据。用于较大数据。

  • Step1 Cereate the counter. 我们先创建一个”numberofitems32”的计数器。有两种方式来创建这种计数器。第一种是通过代码的方式,第二种是通过VS的Server explorer的方式。可以在Vs 的View菜单中看到server explorer。点击performance count 然后选择一个新的目录。

   8

创建一个目录的时候我们同时创建一个计数器。如下图所示:

9

  • Step2 add the counter to your visual studio application: 一旦在Serve Explorer中添加了计数器,可以将这个计数器拖到ASP页面中,注意,你需要将技术的ReadOnly属性设置为False,这样可以在代码中修改计数器。

10

  • Step 3 Add the code to count the counter  最终我们需要增加计数器。我们首先在Form加载的时候将计数器清零。需要注意计数器的值是全局存储的,所以需要明确的进行清零操作。同时当我们的函数MyFunction被调用的时候,我们通过调用计数器的Increment方法来增加计数。
private void Form1_Load(object sender, EventArgs e)
{
perfNumberOfTimeFunctionCalled.RawValue = 0;
}
private void MyFunction()
{
perfNumberOfTimeFunctionCalled.Increment();
}
  • Step 4 View the counter data  现在可以使用性能监视器来显示性能计数器。Win R perfmon .可以看到有很多默认的计数器,为了清晰起见,我们移除所有的计数器,添加我们自己的技术器。
  • 11

然后可以看到如下图形显示:

12

通过performance monitor提供的 view report tab可以看到文字的表述:

13 

Creating more sensible counter


上一节中我们测量了函数MyFuntion被调用的次数,这个没有多少意义,接下来我们将对比Timer调用的次数和MyFunction被调用的次数。

创建如下的instantaneous counter。

private void timer1_Tick(object sender, EventArgs e)
{
perfNumberOfTimeTimerCalled.Increment();
Random objRnd = new Random();
int y = objRnd.Next(1, 5);
if (y > 2)
{
MyFunction();
}
}

可以看到如下的结果:

14

文字显示如下:

15

Average performance counters


接下来添加一个Average performance计数器,我们需要添加自定义函数被调用的次数和Timer调用的次数,然后给他们确定一个平均的基数来确定当timer调用的时候平均函数被调用了多少次。

16

我们需要添加两个计数器,一个作为分子,一个作为分母。分子我们需要使用AverageCount64类型,分母采用AverageBase类型。

17

需要在AverageCount64类型之后添加AverageBase计数器,否则会有以下警告:

18

//For every timer tick we increment the number of time timer called counter.
 
private void timer1_Tick(object sender, EventArgs e)
{
perfAvgNumberofTimeTimerCalled.Increment();
Random objRnd = new Random();
int y = objRnd.Next(1, 5);
if (y > 2)
{
MyFunction();
}
}
 
 
//For every function call we increment the number of time function called counter.
 
private void MyFunction()
{
 
perfNumberOfTimeFunctionCalled.Increment();
 
}

可以看到如下结果:

19

计算方式如下:

20

Rate performance counters


如果想看每秒Myfunction调用的次数,可以添加一个RateOfCountsPerSecond32类型的计数器,在Myfunction被调用的时候每次都增加一下:

21

运行结果如下:

22


Adding counters by C# code

Till now we have added the performance counter using server explorer. You can also add the counter by code. The first thing is we need to import System.Diagnostics namespace.
We then need to create object of 'CounterCreationDataCollection' object.
CounterCreationDataCollection Mycounters = new CounterCreationDataCollection();

Create our actual counter and specify the counter type.  
CounterCreationData totalOps = new CounterCreationData();
totalOps.CounterName = "Numberofoperations";
totalOps.CounterHelp = "Total number of operations executed";
totalOps.CounterType = PerformanceCounterType.NumberOfItems32;
Mycounters.Add(totalOps);

Finally create the counter inside a category. Below code snippet is creating the counter in 'MyCategory' category. 
PerformanceCounterCategory.Create("MyCategory","Sample category for Codeproject", Mycounters);
Let's ease some pain using Performance counter helper
Its quiet a pain to write the counter creation code. You can use performance counter helper to ease and make your code smaller. You can find the performance counter helper at http://perfmoncounterhelper.codeplex.com/ ,
Do not use it in production
Oh yes, use it only when you are doing development. If you are using in production ensure that there is an enabling and disabling mechanism or else it will affect your application performance. 
Conclusion
  • Use performance counters to measure application data.
  • Performance counters comes in various categories like instantaneous, average , rate etc.
  • Performance counters should not be used in production. In case it's used should have a disabling mechanism.
  • Performance counter cannot measure by itself application needs to provide data so that performance monitors can calculate and display the data.

作者的FAQ:

I do understand that this is not the right article to talk about my FAQ's. Just wanted to pat myself to complete 1 year of writing for my FAQ series. Below is the consolidated links for all:-
Silverlight FAQ :- http://www.c-sharpcorner.com/UploadFile/shivprasadk/21FAQ04242009031713AM/21FAQ.aspx
LINQ FAQ :- http://www.c-sharpcorner.com/UploadFile/shivprasadk/654654607132009040318AM/6546546.aspx
WWF FAQ :- http://www.c-sharpcorner.com/UploadFile/shivprasadk/12334512312008070235AM/123345.aspx
WCF FAQ :- http://www.c-sharpcorner.com/UploadFile/shivprasadk/122345601022009064602AM/1223456.aspx
Sharepoint FAQ :- http://www.c-sharpcorner.com/UploadFile/shivprasadk/1234567801062009045241AM/12345678.aspx
Localization and globalization :- http://www.c-sharpcorner.com/UploadFile/shivprasadk/1112401242009043742AM/11124.aspx
Project management FAQ :- http://www.c-sharpcorner.com/UploadFile/shivprasadk/PMCosting04132009051929AM/PMCosting.aspx