nicojs
nicojs

Reputation: 2055

Why is my ASP.NET MVC Multi instance Performance counter not working

I'm having trouble getting our multi-instance performance counter to work in ASP.NET MVC 3. Creating it as single instance works fine and running our multi-instance perf counter code in a simple console app works fine as well. But when I create it multi-instance and try to increment it using ASP.NET, it seams to struggle.

I don't get an exception. I simply do not see any measurement output in perf mon. I can see the 2 instances, however.

This is the console app code to create the performance counter category:

private const string PERF_COUNTER_CATEGORY_NAME = "MvcPerformance";
private const string PERF_COUNTER_BASE_NAME = "MvcAction_Base";
private const string PERF_COUNTER_NAME = "MvcAction";

static void Main(string[] args)
{
    if (PerformanceCounterCategory.Exists(PERF_COUNTER_CATEGORY_NAME))
    {
        PerformanceCounterCategory.Delete(PERF_COUNTER_CATEGORY_NAME);
    }

    CounterCreationDataCollection counters = new CounterCreationDataCollection();

    // Add the counter.
    CounterCreationData avgDuration = new CounterCreationData();
    avgDuration.CounterType = PerformanceCounterType.AverageTimer32;
    avgDuration.CounterName = PERF_COUNTER_NAME;
    counters.Add(avgDuration);

    CounterCreationData avgDurationBase = new CounterCreationData();
    avgDurationBase.CounterName = PERF_COUNTER_BASE_NAME;
    avgDurationBase.CounterType = PerformanceCounterType.AverageBase;
    counters.Add(avgDurationBase);

    // Create the category.
    PerformanceCounterCategory.Create(PERF_COUNTER_CATEGORY_NAME, "Performance counters for MVC!", PerformanceCounterCategoryType.MultiInstance, counters);
}

Then our HomeController in the ASP.NET MVC app looks something like this:

    private static readonly Random _Random = new Random();
    private const string PERF_COUNTER_CATEGORY_NAME = "MvcPerformance";
    private const string PERF_COUNTER_BASE_NAME = "MvcAction_Base";
    private const string PERF_COUNTER_NAME = "MvcAction";
    private static PerformanceCounter _MvcActionCounter = new PerformanceCounter
    {
        CounterName = PERF_COUNTER_NAME, 
        CategoryName = PERF_COUNTER_CATEGORY_NAME,
        ReadOnly = false,
        InstanceName = "Home_Index",
    };
    private static PerformanceCounter _MvcActionCounterBase = new PerformanceCounter
    {
        CounterName = PERF_COUNTER_BASE_NAME,
        CategoryName = PERF_COUNTER_CATEGORY_NAME,
        ReadOnly = false,
        InstanceName = "Home_Index_Base",
    };

    public ActionResult Index()
    {
        Stopwatch s = new Stopwatch();
        s.Start();
        Thread.Sleep(_Random.Next(1000));
        s.Stop();

        // Increment the performance counters.
        _MvcActionCounter.IncrementBy(s.ElapsedMilliseconds);
        _MvcActionCounterBase.Increment();
        return View(new HomeViewModel { ElapsedTime = s.Elapsed });
    }

It's probably something easy I'm forgetting. Maybe something to do with rights or user management?

Upvotes: 1

Views: 1042

Answers (1)

nicojs
nicojs

Reputation: 2055

A colleague of mine just mailed me the answer. The instance name of the base counter must be the same as the instance name of the average counter.

If you encounter a problem like this, be sure that is the first thing you check... can save you a lot of time.

Upvotes: 3

Related Questions