正确的使用方法的PerformanceCounter在.NET来衡量CPU使用率 [英] Correct way to use PerformanceCounter in .NET to measure CPU usage

查看:477
本文介绍了正确的使用方法的PerformanceCounter在.NET来衡量CPU使用率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想衡量一个web服务我商务舱的性能。我怎样才能做到这一点通过编码?

I want to measure performance of my business class in a web service. How can I do it through coding?

我想知道有多少的CPU我公务舱消耗来执行它的操作以及如何表现各不相同,如果网络服务得到多个同时服务电话。

I want to know how much CPU my business class consumes to perform its operation and how does performance vary if web service gets multiple simultaneous service calls.

我不希望来衡量我的Web服务的性能;我只是想知道我的公务舱正在执行。

I do not want to measure performance of my web service; I just want to know how my business class is performing.

我的想法:

阅读一些博客后,我决定写一些片code,将创建一个的PerformanceCounter ,如下图所示。从code,我可以为圈在一个创建多个线程。每个线程将调用我的课。而在同一个循环,我会打电话给柜台实例 NextValue()方法。

After reading some blogs, I decide to write some piece of code that will create a PerformanceCounter as shown below. From that code, I can create multiple threads in one for loop. Each thread will call my class. And in the same for loop, I will call the NextValue() method on the counter instance.

System.Diagnostics.PerformanceCounter CpuCounter = new System.Diagnostics.PerformanceCounter();
            CpuCounter.CategoryName = "Process";
            CpuCounter.CounterName = "% Processor Time";
            CpuCounter.InstanceName = Process.GetCurrentProcess().ProcessName;

问题:

  1. 是这种做法是否正确?
  2. 我将使用处理类或处理器的范畴?
  3. 如何获得性能数据来创建一些报告出来的吗?例如随着100个并发线程,CPU使用率是70%,有200个线程它会高达90%以上等。
  4. 在我的商业逻辑非常迅速结束。并且另一方面, NextValue()方法的一个第二次的时间间隔后给予正确的值中所提到的这个博客。我该如何处理呢?
  5. 有时, NextValue()方法给出的值高于100这是否意味着CPU使用率超过100%? (我想,这个计数器的值不应该PTED作为CPU的使用率%间$ P $,对吗?)
  1. Is this approach correct?
  2. Shall I use "Process" category or "Processor" category?
  3. How can I get performance data to create some report out of it? e.g. With 100 simultaneous threads, CPU usage is 70%, with 200 threads it goes upto 90% etc.
  4. My business logic ends very quickly. And on the other hand, NextValue() method gives correct value after an interval of one second as mentioned in this blog. How can I handle this?
  5. Sometimes, NextValue() method gives value above 100. Does this mean CPU usage is more than 100%? (I think, value of this counter should not be interpreted as % usage of the CPU. Am I correct?)

注:我使用Windows 7 premium和英特尔酷睿i5处理器。这是我第一次使用性能计数器。因此,一些问题可能​​是愚蠢的。

Note: I am using Windows 7 premium and Intel Core i5 processor. This is the first time I am using Performance Counters. So some of the questions may be silly.

推荐答案

我为了下面回答你的问题:

I've answered your questions in order below:

1:只要你的公务舱为你的应用程序激活的分析过程中的唯一部分,是的。但是,性能监视器是最适合的entier应用的大规模分析,它不是真正设计用于特定的方法。要找出有多少个CPU周期才能完成BusinessClass.Work(),你最好不要使用类似JetBrains公司dotTrace或展鹏的蚂蚁分析器。这两种产品都提供免费试用。

1: Provided your Business Class is the only portion of your application active during profiling, yes. However, Perfmon is best for large-scale profiling of an entier application, it's not really designed for specific methods. To find out how many CPU cycles it takes to complete your BusinessClass.Work() you're better off using something like JetBrains dotTrace or Redgate's ANTS profiler. Both of these products offer free trials.

2:流程是正确的计数器,你在做什么

2: Process is the correct counter for what you're doing

3:您可以添加自己的柜台,还是在时间段进行测试。这样做的简单的方法是时间片的方式,在这里实质上运行一系列测试,改变每个测试之间只有一个单可变。 IN的5-10秒的测试等待没有活动,这使你在性能监视器图表中的漂亮的突破。你想安装一个性能计数器日志为好,这是解释的这里

3: You can add your own counter, or perform the test in time slices. The simpler way of doing this is the time slice approach, where you essentially run a series of tests, changing only a single variable between each tests. IN between the tests wait for 5-10 seconds with no activity, which gives you a nice break in your perfmon graph. You'll want to setup a performance counter log as well, which is explained here.

4:请回答1,分析器要好得多的方法级别的时序

4: See answer 1, profilers are much better for method level timings

5:这是预期的。你最大的CPU%,实际上是100 *处理器数量。所以,如果你是酷睿i5双核,最大为200%,如果它是一个四核400%,等等...

5: This is expected. Your max CPU % will actually be 100 * Processor Count. So if your i5 is dual core, the max is 200%, if it's a quad 400%, etc...

请让我知道如果你有任何问题。

Please let me know if you have any questions.

这篇关于正确的使用方法的PerformanceCounter在.NET来衡量CPU使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆