/* * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree. */ #ifndef VIDEO_STATS_COUNTER_H_ #define VIDEO_STATS_COUNTER_H_ #include <memory> #include <string> namespace webrtc { class AggregatedCounter; class Clock; class Samples; // `StatsCounterObserver` is called periodically when a metric is updated. class StatsCounterObserver { … }; struct AggregatedStats { … }; // Classes which periodically computes a metric. // // During a period, `kProcessIntervalMs`, different metrics can be computed e.g: // - `AvgCounter`: average of samples // - `PercentCounter`: percentage of samples // - `PermilleCounter`: permille of samples // // Each periodic metric can be either: // - reported to an `observer` each period // - aggregated during the call (e.g. min, max, average) // // periodically computed // GetMetric() GetMetric() => AggregatedStats // ^ ^ (e.g. min/max/avg) // | | // | * * * * | ** * * * * | ... // |<- process interval ->| // // (*) - samples // // // Example usage: // // AvgCounter counter(&clock, nullptr); // counter.Add(5); // counter.Add(1); // counter.Add(6); // process interval passed -> GetMetric() avg:4 // counter.Add(7); // counter.Add(3); // process interval passed -> GetMetric() avg:5 // counter.Add(10); // counter.Add(20); // process interval passed -> GetMetric() avg:15 // AggregatedStats stats = counter.GetStats(); // stats: {min:4, max:15, avg:8} // // Note: StatsCounter takes ownership of `observer`. class StatsCounter { … }; // AvgCounter: average of samples // // | * * * | * * | ... // | Add(5) Add(1) Add(6) | Add(5) Add(5) | // GetMetric | (5 + 1 + 6) / 3 | (5 + 5) / 2 | // // `include_empty_intervals`: If set, intervals without samples will be included // in the stats. The value for an interval is // determined by GetValueForEmptyInterval(). // class AvgCounter : public StatsCounter { … }; // MaxCounter: maximum of samples // // | * * * | * * | ... // | Add(5) Add(1) Add(6) | Add(5) Add(5) | // GetMetric | max: (5, 1, 6) | max: (5, 5) | // class MaxCounter : public StatsCounter { … }; // PercentCounter: percentage of samples // // | * * * | * * | ... // | Add(T) Add(F) Add(T) | Add(F) Add(T) | // GetMetric | 100 * 2 / 3 | 100 * 1 / 2 | // class PercentCounter : public StatsCounter { … }; // PermilleCounter: permille of samples // // | * * * | * * | ... // | Add(T) Add(F) Add(T) | Add(F) Add(T) | // GetMetric | 1000 * 2 / 3 | 1000 * 1 / 2 | // class PermilleCounter : public StatsCounter { … }; // RateCounter: units per second // // | * * * | * * | ... // | Add(5) Add(1) Add(6) | Add(5) Add(5) | // |<------ 2 sec ------->| | // GetMetric | (5 + 1 + 6) / 2 | (5 + 5) / 2 | // // `include_empty_intervals`: If set, intervals without samples will be included // in the stats. The value for an interval is // determined by GetValueForEmptyInterval(). // class RateCounter : public StatsCounter { … }; // RateAccCounter: units per second (used for counters) // // | * * * | * * | ... // | Set(5) Set(6) Set(8) | Set(11) Set(13) | // |<------ 2 sec ------->| | // GetMetric | (8 - 0) / 2 | (13 - 8) / 2 | // // `include_empty_intervals`: If set, intervals without samples will be included // in the stats. The value for an interval is // determined by GetValueForEmptyInterval(). // class RateAccCounter : public StatsCounter { … }; } // namespace webrtc #endif // VIDEO_STATS_COUNTER_H_