locked
Slow call to PerformanceCounter.NextValue() RRS feed

  • Question

  • I have a set of PerformanceCounters (stored in a dictionary), constructed as:
    new PerformanceCounter("Process", "% Processor Time", processName);

    Each call to PerformanceCounter.NextValue() takes approximately 2.4 ms on my machine (not the fastest one, definately not the slowest one).

    Is this reasonable ?
    If so, how can Performance Monitor work at all (doing basically the same thing ?)

    Thursday, July 14, 2016 6:05 PM

Answers

  • Hi EuroEager,

    According to your description, I create a console app (code below) to test the time. the execution time of the call is between 2.4ms and 4.1ms.  So I think that 3.4ms is a normal value.

    static void Main(string[] args)
            {
                var log = new LoggerConfiguration()
                 .MinimumLevel.Debug()
                 .WriteTo.RollingFile(@"D:\log\log.txt", retainedFileCountLimit: 7)
                 .CreateLogger();
    
                Stopwatch watch = new Stopwatch();
                log.Debug("Going to initialize Diagnostic's PerformanceCounters");
                watch.Start();
                PerformanceCounter p1 = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName);
                p1.NextValue();
                //Ensure an updated value...
                System.Threading.Thread.Sleep(1000);
                p1.NextValue();
                watch.Stop();
                log.Debug("Finished initializing Diagnosticss PerformanceCounters. Time elapsed: {0}", watch.Elapsed);
            }
    

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, July 18, 2016 8:18 AM

All replies

  • Hi EuroEager,

    >>Is this reasonable?

    According to your description, it seems that you have a set of PerformanceCounters (stored in a dictionary), please refer to the following threads, the replier makes some testing, if PerformanceCounters number is 256, then read time is about 15-18ms per counter.

    http://stackoverflow.com/questions/25372073/performance-counters-nextvalue-very-slow-1-000-counters

    >> If so, how can Performance Monitor work at all (doing basically the same thing ?)

    We could also refer to the above thread which provide a solution, please see “Update” phrase.

    We could create many categories, each category having fewer counters (4-8 counters per category). This approach has allowed us to effectively avoid the performance issue.

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, July 15, 2016 8:34 AM
  • But if I simply instantiate only one counter:
    PerformanceCounter myCounter = new PerformanceCounter(
      "Process",
      "% Processor Time",
      Process.GetCurrentProcess().ProcessName);

    An then call myPerf.NextValue() from a one second timer tick handler, I will get the similar results (average execution time of the call of about 3.4 ms (even longer time than the multi-counter case)

    Friday, July 15, 2016 10:50 AM
  • Hi EuroEager,

    According to your description, I create a console app (code below) to test the time. the execution time of the call is between 2.4ms and 4.1ms.  So I think that 3.4ms is a normal value.

    static void Main(string[] args)
            {
                var log = new LoggerConfiguration()
                 .MinimumLevel.Debug()
                 .WriteTo.RollingFile(@"D:\log\log.txt", retainedFileCountLimit: 7)
                 .CreateLogger();
    
                Stopwatch watch = new Stopwatch();
                log.Debug("Going to initialize Diagnostic's PerformanceCounters");
                watch.Start();
                PerformanceCounter p1 = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName);
                p1.NextValue();
                //Ensure an updated value...
                System.Threading.Thread.Sleep(1000);
                p1.NextValue();
                watch.Stop();
                log.Debug("Finished initializing Diagnosticss PerformanceCounters. Time elapsed: {0}", watch.Elapsed);
            }
    

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, July 18, 2016 8:18 AM
  • Alright, I guess Cole's post is an answer, but not really to my first question which was if this is reasonable.

    I have tested myself, the simplest setup possibly and got results which I think is not reasonable at all.

    Cole: Do you think this is reasonable (without referring to tests, I ask for your opinion)

    Tuesday, July 26, 2016 4:10 PM