I'm working on a charge balancing system and thus I need to know the charge of each machine. PerformanceCounter seem the way to go, but creating the first one take between 38 and 60 sec. Each subsequent new Counter or 'NextValue' call is nearly instant however.
Here is the code I'm using :
[TestClass]
public class PerfMon
{
[TestMethod]
public void SimpleCreationTest()
{
Stopwatch Time = new Stopwatch();
Time.Start();
Debug.WriteLine("Time is : " + Time.ElapsedMilliseconds);
// Create
PerformanceCounter RAM = new PerformanceCounter("Memory", "Available MBytes");
Debug.WriteLine("Time is : " + Time.ElapsedMilliseconds + " => RAM created");
PerformanceCounter CPU = new PerformanceCounter("Processor", "% Processor Time", "_Total");
Debug.WriteLine("Time is : " + Time.ElapsedMilliseconds + " => CPU created");
PerformanceCounter GC = new PerformanceCounter(".NET CLR Memory", "% Time in GC", "_Global_");
Debug.WriteLine("Time is : " + Time.ElapsedMilliseconds + " => GC created");
// Read
float Value = RAM.NextValue();
Debug.WriteLine("Time is : " + Time.ElapsedMilliseconds + " => RAM value is : " + Value);
Value = CPU.NextValue();
Debug.WriteLine("Time is : " + Time.ElapsedMilliseconds + " => CPU value is : " + Value);
Value = GC.NextValue();
Debug.WriteLine("Time is : " + Time.ElapsedMilliseconds + " => GC value is : " + Value);
}
}
Research
PerformanceCounter extremely slow in connecting remote server
Creating a new System.Diagnostics.PerformanceCounter is very slow
I tried using the other constructors and giving a precise 'MachineName' but it doesn't change anything.
Why a call to PerformanceCounter is slow?
http://craigandera.blogspot.fr/2005/06/performancecounter-constructor-horribly_21.html
According to this two threads, the problem seem to be about the fact that performance counters are a shared resource. However I don't understand how I could solve that.
Running Visual Studio in Administrator 'accelerate' the first creation from 38 sec to 26 sec, so it doesn't solve the problem either.
Thanks for your help.
network issue
makes no sense. – Bowdlerize