This page last changed on Jul 02, 2005 by peli.

The PerfCounterAttribute is a TestDecorator that allows you assert on the values of PerformanceCounters. This means that you can make an assertion an every perfomance counter on your machine!

Here's a sample fixture that shows how the attribute can be used:

Unable to find source-code formatter for language: cs. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml
using System;
using MbUnit.Core.Framework;
using MbUnit.Framework;
namespace MbUnit.Demo
{
  [TestFixture]
  public class PerfCounterDemo
  {
    [Test]
    [PerfCounter(".NET CLR Memory", "% Time in GC", 10)]
    public void AllocateALotOfObjects()
    { 
      ...
    }  

    [Test]
    [PerfCounter(".NET CLR Loading", "% Time Loading", 10)]
    [PerfCounter(".NET CLR Security", "% Time in RT checks", 10000)]
    [PerfCounter(".NET CLR Security", "% Time Sig. Authenticating", 10)]
    [PerfCounter(".NET CLR Memory", "# Bytes in all Heaps", 5000000, Relative =true)]
    [PerfCounter(".NET CLR Jit", "% Time in Jit", 10)]
    public void MonitorMultipleCounters()
    {
      ...
    }
  }
}

Note that if you are too lazy to remember the names of the counters, I have written a CodeSmith template that creates a class filled with static helper methods for retreiving the counters. For example, the following class lets you write things like PerfCounterInfo.NetClrExceptions.NbofExcepsThrown.NextValue() using intellisense and without typing errors

Unable to find source-code formatter for language: cs. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml
using System; 
using System. Diagnostics;
namespace MbUnit.Core.Framework
{
 public class PerfCounterInfo
 {  
   public sealed class NetClrExceptions
   {
    const string categoryName = @".NET CLR Exceptions";  

    public sealed class NbofExcepsThrown
    {
     const string counterName = @"# of Exceps Thrown";  

     public static float NextValue()
     {
      return NextValue(Process.GetCurrentProcess().ProcessName);
     }    
    }
    ...
Document generated by Confluence on Jun 11, 2007 11:56