问题
I want to measure performance of my business class in a web service. How can I do it through coding?
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.
I do not want to measure performance of my web service; I just want to know how my business class is performing.
My thought:
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;
Questions:
- Is this approach correct?
- Shall I use "Process" category or "Processor" category?
- 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.
- 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? - 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?)
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.
回答1:
I've answered your questions in order below:
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: Process is the correct counter for what you're doing
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: See answer 1, profilers are much better for method level timings
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.
来源:https://stackoverflow.com/questions/11504438/correct-way-to-use-performancecounter-in-net-to-measure-cpu-usage