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

发布于 2024-08-04 15:45:35 字数 83 浏览 6 评论 0原文

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请别遗忘我 2024-08-11 15:45:35

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

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

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

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

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

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();
    }
}
灼疼热情 2024-08-11 15:45:35

查看不同的 PerformanceCounterTypes
有多种计算平均时间或计数的类型。您还会发现一些示例。

希望这有帮助。

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

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文