Hui Zhao
Hui Zhao

Reputation: 675

Select PerformanceCounterType

Here is the question. I am developing a method named CreateCounters that will create performance counters for an application. The method includes the following code.

void CreateCounters()
{
     if(!PerformanceCounterCategory.Exists("Contoso"))
     {
         var counters = new CounterCreateationDataCollection();
         var ccdCounter1 = new CounterCreationData
         {
             CounterName = "Counter1";
             CounterType = PerformanceCounterType.SampleFraction;
         };
         counters.Add(ccdCounter1);
         var ccdCounter2 = new CounterCreationData
         {
             CounterName = "Counter2";
             // need insert PerformanceCounterType
         };
         counters.Add(ccdCounter2);
         PerformanceCounterCategory.Create("Contoso","Help dtring",
         PerformanceCounterCategoryType.MultiInstance, counters);
    }
}

I need to ensure that Counter1 is available for use in Windows Performance Monitor (PerfMon). Which code segment should you insert?

There are four choices.

A. CounterType = PerformanccCounterType.RawBase
B. CounterType = PerformanceCounterType.AverageBase
C. CounterType = PerformanceCounterType.SampleBase
D. CounterType = PerformanceCounterType.CounterMultiBase

I don't know which one and why?

Upvotes: 6

Views: 5243

Answers (4)

Onsokumaru
Onsokumaru

Reputation: 541

The answer is, as all other answers here on stackoverflow point out, option c with the SampleBase counter type.

Unfortunately, I think it's not enough just to refer to the Microsoft documentation for the PerformanceCounterType since the important lines for this specific question in handling performance counters are easy to miss. So did I and needed to find this question and another blogpost until I recognized them.

So for clarification why c is the correct answer here is the explanation taken from the remarks section of the Microsoft documentation:

When instrumenting applications (creating and writing custom performance counters), you might be working with performance counter types that rely on an accompanying base counter that is used in the calculations. The base counter must be immediately after its associated counter in the CounterCreationDataCollection collection your application uses. The following table lists the base counter types with their corresponding performance counter types.

Base counter type  Performance counter types
AverageBase        AverageTimer32, AverageCount64
CounterMultiBase   CounterMultiTimer, CounterMultiTimerInverse, CounterMultiTimer100Ns, CounterMultiTimer100NsInverse
RawBase            RawFraction
SampleBase         SampleFraction

Upvotes: 1

Mohamad-Al-Ibrahim
Mohamad-Al-Ibrahim

Reputation: 13

I think that the Correct Answer is C. CounterType = PerformanceCounterType.SampleBase

Upvotes: 0

Tshepo Sibiya
Tshepo Sibiya

Reputation: 79

C. CounterType = PerformanceCounterType.SampleBase

Upvotes: 2

tolanj
tolanj

Reputation: 3724

See this:

https://msdn.microsoft.com/en-us/library/system.diagnostics.performancecountertype%28v=vs.110%29.aspx

there is a table in there showing that PerformanceCounterType.SampleFraction needs a denomonator of type PerformanceCounterType.SampleBase

(and RawFraction needs RawBase etc)

Upvotes: 6

Related Questions