从命令行重置性能计数器 [英] Reset performance counter from command line

查看:407
本文介绍了从命令行重置性能计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从命令行执行命令以将给定的性能计数器重置为0.

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

我可以编写一个简单的"3"行控制台应用程序来执行此操作,但是想知道VS还是Windows或Windows SDK已经附带了该实用程序.在typeperf或logman中都找不到这种选项.

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.

上下文: Windows 7 x64(具有管理员访问权限)

Context: Windows 7 x64 (with Admin access)

背景:
我使用性能计数器来调试/开发/压力测试Web服务. Web服务每次被命中都会增加一个性能计数器.

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.

因此,方案是要访问Web服务10000次,并确保没有消息丢失(我测试了MSMQ +乱序处理+持久性+ Windows Workflow Service)

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)

推荐答案

在等待更好的答案时,这是一个完整的"rstpc.exe"实用程序,用于重置性能计数器(NumberOfItems32类型):

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

这篇关于从命令行重置性能计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆