创建 PerfMon 计数器以记录每次调用的平均值 (C#)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1409762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 16:14:46  来源:igfitidea点击:

Creating a PerfMon counter to record an average per call (C#)

c#perfmon

提问by Justin

How can I use PerfMon counters to record the average execution time of a method in C#?

如何使用 PerfMon 计数器记录 C# 中方法的平均执行时间?

So far I've only found sample code to incrememnt or decrement a PerfMon counter.

到目前为止,我只找到了增加或减少 PerfMon 计数器的示例代码。

采纳答案by Mark Seemann

Here's some sample code I once wrote to do exactly that.

这是我曾经编写的一些示例代码来做到这一点。

First, you need to specify and install the performance counters in question. You can do this by using an Installer:

首先,您需要指定并安装有问题的性能计数器。您可以使用安装程序执行此操作:

public class CreditPerformanceMonitorInstaller : Installer
{
    private PerformanceCounterInstaller counterInstaller_;

    public CreditPerformanceMonitorInstaller()
    {
        this.counterInstaller_ = new PerformanceCounterInstaller();
        this.counterInstaller_.CategoryName = CreditPerformanceCounter.CategoryName;
        this.counterInstaller_.CategoryType = PerformanceCounterCategoryType.SingleInstance;

        CounterCreationData transferAverageData = new CounterCreationData();
        transferAverageData.CounterName = CreditPerformanceCounter.AverageTransferTimeCounterName;
        transferAverageData.CounterHelp = "Reports the average execution time of transfer operations";
        transferAverageData.CounterType = PerformanceCounterType.AverageTimer32;
        this.counterInstaller_.Counters.Add(transferAverageData);

        CounterCreationData transferAverageBaseData = new CounterCreationData();
        transferAverageBaseData.CounterName = CreditPerformanceCounter.AverageTransferTimeBaseCounterName;
        transferAverageBaseData.CounterHelp = "Base for average transfer time counter";
        transferAverageBaseData.CounterType = PerformanceCounterType.AverageBase;
        this.counterInstaller_.Counters.Add(transferAverageBaseData);

        this.Installers.Add(this.counterInstaller_);
    }

    public Installer PerformanceCounterInstaller
    {
        get { return this.counterInstaller_; }
    }
}

To write to the performance counter, you can do it like this:

要写入性能计数器,您可以这样做:

public void RecordTransfer(long elapsedTicks)
{
    using (PerformanceCounter averageTransferTimeCounter = new PerformanceCounter(),
        averageTransferTimeBaseCounter = new PerformanceCounter())
    {
        averageTransferTimeCounter.CategoryName = CreditPerformanceCounter.CategoryName;
        averageTransferTimeCounter.CounterName = CreditPerformanceCounter.AverageTransferTimeCounterName;
        averageTransferTimeCounter.ReadOnly = false;

        averageTransferTimeBaseCounter.CategoryName = CreditPerformanceCounter.CategoryName;
        averageTransferTimeBaseCounter.CounterName = CreditPerformanceCounter.AverageTransferTimeBaseCounterName;
        averageTransferTimeBaseCounter.ReadOnly = false;

        averageTransferTimeCounter.IncrementBy(elapsedTicks);
        averageTransferTimeBaseCounter.Increment();
    }
}

回答by Ezombort

Take a look at the different PerformanceCounterTypes. There are several types for calculating average time or count. You will also find some examples.

看看不同的PerformanceCounterTypes。有几种类型可用于计算平均时间或计数。您还将找到一些示例。

Hope this helps.

希望这可以帮助。