THX-1138
THX-1138

Reputation: 21750

Reset performance counter from command line

I want to execute a command from command line to reset given performance counter to 0.

I could write a simple "3" lines console app to do that, but wondering if VS or Windows or Windows SDK already comes with such utility. I did not find such option in either typeperf or logman.

Context: Windows 7 x64 (with Admin access)

Background:
I use a performance counter to debug/develop/stress-test a web service. Web service increments a performance counter every time it is hit.

So the scenario is to hit web service 10000 times and verify no messages have been lost (I test MSMQ + out-of-order processing + persistence + Windows Workflow Service)

Upvotes: 2

Views: 3112

Answers (1)

THX-1138
THX-1138

Reputation: 21750

While I'm waiting for a better answer, here is a complete "rstpc.exe" utility to reset performance counter (of NumberOfItems32 type):

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace ResetPerformanceCounter
{
    internal class Program
    {
        private static int Main(string[] args)
        {
            if (args.Length != 2)
            {
                string fileName = Path.GetFileName(Assembly.GetExecutingAssembly().Location);
                Console.WriteLine("Usage: {0} <PC Category> <PC Name>", fileName);
                Console.WriteLine("Examlpe: {0} {1} {2}", fileName, "GEF", "CommandCount");
                return -1;
            }

            string cat = args[0];
            string name = args[1];

            if (!PerformanceCounterCategory.CounterExists(name, cat))
            {
                Console.WriteLine("Performance Counter {0}\\{1} not found.", cat, name);
                return - 2;
            }

            var pc = new System.Diagnostics.PerformanceCounter(cat, name, false);

            if (pc.CounterType != PerformanceCounterType.NumberOfItems32)
            {
                Console.WriteLine("Performance counter is of type {0}. Only '{1}' countres are supported.", pc.CounterType.ToString(), PerformanceCounterType.NumberOfItems32);
                return -3;
            }

            Console.WriteLine("Old value: {0}", pc.RawValue);
            pc.RawValue = 0;
            Console.WriteLine("New value: {0}", pc.RawValue);
            Console.WriteLine("Done.");
            return 0;
        }
    }
}

Upvotes: 4

Related Questions