Reset performance counter from command line

时光总嘲笑我的痴心妄想 提交于 2019-12-11 02:41:50

问题


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)


回答1:


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;
        }
    }
}


来源:https://stackoverflow.com/questions/9195851/reset-performance-counter-from-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!