3

我有一种图书馆,它使用一堆自己的性能计数器。但即使没有安装性能计数器,我也希望我的库能够正常工作。

所以我在 PerformanceCounter 周围创建了包装器,并在第一次使用时检查 PerfCounter 是否存在。如果它们存在,那么我将使用本机 PerformanceCounter,而不是使用不执行任何操作的包装器。

所以要检查性能计数器是否存在,我使用 PerformanceCounterCategory.Exists

问题是,如果没有这样的类别,那么 PerformanceCounterCategory.Exists 调用(在我的机器上)大约需要 10 秒!不用说它太慢了。

我能做些什么?

自己尝试的代码: using System; 使用 System.Diagnostics;

class Program
{
    static void Main(string[] args)
    {
        var ts = Stopwatch.StartNew();
        var res = PerformanceCounterCategory.Exists("XYZ");
        Console.WriteLine(ts.ElapsedMilliseconds);
        Console.WriteLine("result:" + res);
}
}
4

2 回答 2

6

这是似乎无法避免的事情。来自 MSDN:

使用 Exists 方法可能会导致明显的性能损失,同时检查机器上的所有性能计数器的可用性。 如果您只写入性能计数器,则可以通过在安装应用程序时创建性能计数器并在访问计数器时假设存在类别来避免全局搜索性能计数器。从性能计数器读取时,无法避免性能计数器搜索。

重点是我的。

于 2010-04-16T17:58:18.920 回答
0

这是一个 8 年前的问题,但只是分享为我解决的问题:

The performance counters setup time in my application dropped from 2:30 minutes to around 20 seconds by ensuring it was being executed as a 64bit process. The interesting is that I had the performance issue only in a Windows 2012 VM. No problems in Windows 10.

于 2018-06-25T12:25:10.230 回答