title: Correct way to use PerformanceCounter in .NET to measure CPU usage
body: <p>I want to measure performance of my business class in a web service. How can I do it through coding?</p>
  
  <p>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.</p>
  
  <p>I do not want to measure performance of my web service; I just want to know how my business class is performing.</p>
  
  <p><strong>My thought:</strong></p>
  
  <p>After reading some blogs, I decide to write some piece of code that will create a <code>PerformanceCounter</code> as shown below. From that code, I can create multiple threads in one <code>for</code> loop. Each thread will call my class. And in the same <code>for</code> loop, I will call the <code>NextValue()</code> method on the counter instance.</p>
  
  <pre><code>System.Diagnostics.PerformanceCounter CpuCounter = new System.Diagnostics.PerformanceCounter();
              CpuCounter.CategoryName = "Process";
              CpuCounter.CounterName = "% Processor Time";
              CpuCounter.InstanceName = Process.GetCurrentProcess().ProcessName;
  </code></pre>
  
  <p><strong>Questions:</strong></p>
  
  <ol>
  <li>Is this approach correct?</li>
  <li>Shall I use "Process" category or "Processor" category? </li>
  <li>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.</li>
  <li>My business logic ends very quickly. And on the other hand, <code>NextValue()</code> method gives correct value after an interval of one second as mentioned in <a href="http://blogs.msdn.com/b/bclteam/archive/2006/06/02/618156.aspx" rel="nofollow">this</a> blog. How can I handle this?</li>
  <li>Sometimes, <code>NextValue()</code> 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?)</li>
  </ol>
  
  <p>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.</p>