Chromium Code Reviews| Index: webrtc/video/stats_counter.h |
| diff --git a/webrtc/video/stats_counter.h b/webrtc/video/stats_counter.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4541d2374cea888065ace2f79b85d5f6db8ea6c8 |
| --- /dev/null |
| +++ b/webrtc/video/stats_counter.h |
| @@ -0,0 +1,179 @@ |
| +/* |
| + * 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 WEBRTC_VIDEO_STATS_COUNTER_H_ |
| +#define WEBRTC_VIDEO_STATS_COUNTER_H_ |
| + |
| +#include <map> |
| + |
| +#include "webrtc/base/scoped_ptr.h" |
| + |
| +namespace webrtc { |
| + |
| +class Clock; |
| + |
| +// StatsCounterObserver is called periodically when a metric is updated. |
| +class StatsCounterObserver { |
| + public: |
| + virtual void OnMetricUpdated(int sample) = 0; |
| + |
| + virtual ~StatsCounterObserver() {} |
| +}; |
| + |
| +class StatsCounter { |
| + public: |
| + virtual ~StatsCounter() {} |
| + |
| + virtual bool GetMetric(int* metric) const = 0; |
| + |
| + // Class holding periodically computed metrics. |
| + class Histogram { |
| + public: |
| + Histogram(size_t bucket_count, size_t bucket_max); |
| + ~Histogram() {} |
| + |
| + void Add(int sample); |
| + |
| + struct Stats { |
| + Stats(); |
| + int64_t num_samples; |
| + int min; |
| + int max; |
| + int average; |
| + int percentile10; |
| + int percentile50; |
| + int percentile90; |
| + }; |
| + |
| + // Computes and returns |stats_|. |
| + Stats stats(); |
|
stefan-webrtc
2016/01/29 15:22:46
ComputeStats() or ComputeAndGetStats()? Should pro
|
| + |
| + private: |
| + void InitializeBuckets(); |
| + void AddToBucket(size_t sample); |
| + void Compute(); |
| + |
| + // Number of samples per bucket mapped by start of bucket. |
| + std::map<size_t, size_t> buckets_; |
| + const size_t bucket_count_; |
| + const size_t bucket_max_; |
| + const bool bucket_size1_; |
|
stefan-webrtc
2016/01/29 15:22:46
bucket_size_?
|
| + int64_t sum_; |
| + Stats stats_; |
| + }; |
| + |
| + // Returns stats from |histogram_|. |
| + Histogram::Stats GetStats(); |
| + |
| + protected: |
| + StatsCounter(Clock* clock, |
| + size_t bucket_count, |
| + size_t bucket_max, |
| + bool include_empty_intervals, |
| + StatsCounterObserver* observer); |
| + |
| + // Adds/sets sample. Periodically calls GetMetric and adds it to |histogram_|. |
| + void Add(int sample); |
| + void Set(int sample); |
| + |
| + int64_t sum_; |
| + int64_t num_samples_; |
| + int64_t last_sum_; |
| + |
| + private: |
| + bool TimeToProcess(); |
| + void Process(); |
| + |
| + Clock* const clock_; |
| + const bool include_empty_intervals_; |
| + const rtc::scoped_ptr<StatsCounterObserver> observer_; |
| + int64_t last_process_time_ms_; |
| + Histogram histogram_; |
| +}; |
|
stefan-webrtc
2016/01/29 15:22:46
You probably want to disable the copy constructor
|
| + |
| +// Classes which periodically computes a metric (based on added samples in |
| +// the interval) and adds it to a histogram. |
| +// |
| +// |bucket_count| == |bucket_max|: => linear histogram (bucket size:1) |
| +// |bucket_count| < |bucket_max|: => exponential histogram |
| +// |
| +// Note: takes ownership of |observer|. |
| + |
| +class AvgCounter : public StatsCounter { |
| + public: |
| + AvgCounter(Clock* clock, |
| + size_t bucket_count, |
| + size_t bucket_max, |
| + StatsCounterObserver* observer); |
| + ~AvgCounter() override {} |
| + |
| + void Add(int sample); |
| + |
| + private: |
| + bool GetMetric(int* metric) const override; |
| +}; |
| + |
| +class PercentCounter : public StatsCounter { |
| + public: |
| + PercentCounter(Clock* clock, StatsCounterObserver* observer); |
| + ~PercentCounter() override {} |
| + |
| + void Add(bool sample); |
| + |
| + private: |
| + bool GetMetric(int* metric) const override; |
| +}; |
| + |
| +class PermilleCounter : public StatsCounter { |
| + public: |
| + PermilleCounter(Clock* clock, |
| + size_t bucket_count, |
| + StatsCounterObserver* observer); |
| + ~PermilleCounter() override {} |
| + |
| + void Add(bool sample); |
| + |
| + private: |
| + bool GetMetric(int* metric) const override; |
| +}; |
| + |
| +class RateCounter : public StatsCounter { |
| + public: |
| + RateCounter(Clock* clock, |
| + size_t bucket_count, |
| + size_t bucket_max, |
| + bool include_empty_intervals, |
| + StatsCounterObserver* observer); |
| + ~RateCounter() override {} |
| + |
| + void Add(int sample); |
| + |
| + private: |
| + bool GetMetric(int* metric) const override; |
| +}; |
| + |
| +class RateAccCounter : public StatsCounter { |
| + public: |
| + RateAccCounter(Clock* clock, |
| + size_t bucket_count, |
| + size_t bucket_max, |
| + bool include_empty_intervals, |
| + StatsCounterObserver* observer); |
| + ~RateAccCounter() override {} |
| + |
| + void Set(int sample); |
| + |
| + private: |
| + bool GetMetric(int* metric) const override; |
| +}; |
| + |
| +} // namespace webrtc |
| + |
| +#endif // WEBRTC_VIDEO_STATS_COUNTER_H_ |