性能计数器每5分钟平均一次

时间:2016-05-12 18:35:54

标签: c# performance

使用perfmon工具我尝试每5分钟捕获一次事务所花费的平均时间。我得到的结果似乎是错误的。有人可以帮助我。

 // Caller`enter code here`
 if (!_perfCounterDefinition.IsRegistered)
 {
    CreateCounter("name");
 }

 // initialize perfomance counter

var averageTransactions = new PerformanceCounter(name, "Average Transactions", false);
var baseAverage = new PerformanceCounter(name, "Average Transactions Base", false);
//check if elapsed time is 5 min; initialized counters
if (_stopwatch.ElapsedMilliseconds > (5000*60))
{
    //counter reset
    baseAverage.RawValue = 0;
    averageTransactions.RawValue = 0;
    _stopwatch.Reset();
}
else
{
    _stopwatch.Stop();
    averageTransactions.IncrementBy(_stopwatch.ElapsedTicks);
    baseAverage.Increment();
}
//Helper
private static void CreateCounter(string name)
 {
            var averageTransactions = new CounterCreationData()
            {
                CounterName = "Average Transactions",
                CounterHelp = "Average Transactions",
                CounterType = PerformanceCounterType.AverageTimer32
            };
            PerformanceCounterCategory.Create(name, name, PerformanceCounterCategoryType.SingleInstance, counterData);
}

0 个答案:

没有答案