创建性能监视器计数器,记录每次通话的平均(C#) [英] Creating a PerfMon counter to record an average per call (C#)

查看:211
本文介绍了创建性能监视器计数器,记录每次通话的平均(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用性能计数器记录的方法的平均执行时间在C#?

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

到目前为止,我只发现样品code到incrememnt或递减PerfMon的计数器。

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

推荐答案

下面是一些示例code我曾经写过做到这一点。

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

这篇关于创建性能监视器计数器,记录每次通话的平均(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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