Cylindric
Cylindric

Reputation: 5894

Creating a new System.Diagnostics.PerformanceCounter is very slow

I have a simple monitoring application that is getting some values from PerfMon counters. Even when testing on the local machine, it is taking over 30 seconds to create a new PerformanceCounter object.

using System;
using System.Diagnostics;

namespace test_slow_perfmon
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch w = new Stopwatch();

            w.Start();
            PerformanceCounter c = new PerformanceCounter("PhysicalDisk", "Avg. Disk Read Queue Length", "_Total", "localhost");
            w.Stop();
            Console.WriteLine(string.Format("Creating a counter took {0}ms", w.Elapsed.TotalMilliseconds));
        }
    }
}

Output from that indicates over 32s to create each counter.

What can I do (if anything) to speed up the creation of the counters?

Upvotes: 2

Views: 2442

Answers (1)

Justin
Justin

Reputation: 86789

30 seconds sounds to me suspiciously like a timeout, indicating to me that this could be some sort of network issue.

Try creating your perfmon counter using the constructor that doesn't specify a hostname and see if that helps:

PerformanceCounter c = new PerformanceCounter("PhysicalDisk", "Avg. Disk Read Queue Length", "_Total");

Upvotes: 3

Related Questions