Click or drag to resize

Counter Class

Predefined PerformanceCounter types.
Inheritance Hierarchy
SystemObject
  Demo3D.Utilities.PerformanceCounter

Namespace:  Demo3D.Utilities.Performance
Assembly:  Demo3D.Common (in Demo3D.Common.dll) Version: 18.03.00
Syntax
C#
public static class Counter
Remarks
Counters allow statistics to be gathered about a user resource or function. The CounterNumber counter is the simplest, logging a numeric value.
C#
// An example class that computes some complicated function.
// 
// The value of the functions may be interesting, and could be logged:
//    A histogram of actual values computed.
// 
// But also statistics about the calling of the function itself:
//    A simple count of how many times it's been called.
//    The rate at which it was called.
//    The percentage of real time spent computing the function.
//    A histogram of actual computation times.
public class CounterExample {
    // Logging the actual confidence values computed might be useful to the user in order for
    // them to observe confidence trends over time, or to look at the frequency of certain
    // confidence values.
    readonly Counter.Number confidence
        = new("ConfidenceLevel",               // Name that appears in the GUI menus and charts.
            minValue: 0, maxValue: 100,        // Expected min/max helps charts scale graphics.
            description: "Confidence level");  // A useful description of the value logged.

    // If this function is called often then it may be useful to log the number of times the
    // function was called, and the rate at which it was called.
    readonly Counter.Operations computations
        = new("ConfidenceComputations",
            description: "Confidences computed");

    // If the function is slow, and especially if its computational speed is variable, then
    // it may be useful to log how long it took to compute.
    readonly Counter.PercentageTime timing
        = new("ConfidenceTiming",
            description: "Percentage time spent computing confidences.");

    // An example function that computes a value.
    public double ComputeConfidence() {
        var sw    = Stopwatch.StartNew();
        var value = new Random().Next() * 100; // Some slow function

        timing.IncrementBy(sw.Elapsed.Ticks);  // Logs the execution time of a computation.
        computations.Increment();              // Logs that a value was computed.
        confidence.Log((int)value);            // Logs the actual numeric confidence value.

        return value;
    }
}
CounterNumber Logs a given value. The value is (scaled and) passed immediately to the chart.
CounterTimeTicks Logs a given number of ticks (100ns per tick). The value is scaled to milliseconds and passed immediately to the chart.
CounterTimeMs Logs a given number of milliseconds. The value is passed immediately to the chart.
CounterCount Increments a counter. The chart should sample the current count periodically.
Consider using CounterOperations instead for logging number of operations performed.
CounterPercentageCount Increments a counter numerator and denominator. The chart should sample the current value periodically.
The sample value returned to the chart is the numerator/denominator*100.
CounterPercentageTime Increments a duration by a given number of ticks (100ns). The chart should sample the current value periodically.
The sample value returned to the chart is the current accumulated number of ticks, divided by the time elapsed since the last sample, and multiplied by 100.
Useful for logging time-in-state.
CounterOperations Logs the number of operations performed, and the number of operations performed per second.
Maintains two separate values for the counter. Either or both can be charted.
CounterQueue Logs the current length of a queue, the number of items dequeued per second, and the rate at which the queue is growing/shrinking.
Maintains three separate values for the counter. Any or all can be charted.

Counters are registered in a global registry. See All, CounterRegistered and CounterUnregistered.

Various events can be used to enable/disable data collection, especially where data collection is expensive. See Subscribed and Unsubscribed.

See Also