Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1183)

Unified Diff: webrtc/video/stats_counter.h

Issue 1640053003: Add class which periodically computes statistics. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: rebase Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..780a41c670a2981d497b282b2e18380caea61c12
--- /dev/null
+++ b/webrtc/video/stats_counter.h
@@ -0,0 +1,171 @@
+/*
+ * 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 <memory>
+
+#include "webrtc/base/constructormagic.h"
+#include "webrtc/typedefs.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 AggStats {
mflodman 2016/05/06 07:33:29 What does 'Agg' stand for? Can you write the whole
åsapersson 2016/05/09 14:36:50 Have moved the nested classes and renamed classes.
+ public:
+ AggStats();
+ ~AggStats() {}
+
+ struct Stats {
+ Stats();
+ int64_t num_samples;
mflodman 2016/05/06 07:33:29 You can set default values here instead of using a
åsapersson 2016/05/09 14:36:50 Done.
+ int min;
+ int max;
+ int average;
+ // TODO(asapersson): Consider adding median/percentiles.
+ };
+
+ void Add(int sample);
+ // Computes and returns |stats_|.
stefan-webrtc 2016/05/06 11:10:17 Empty line above.
åsapersson 2016/05/09 14:36:50 Done.
+ Stats ComputeStats();
+
+ private:
+ void Compute();
+ int64_t sum_;
+ Stats stats_;
+ };
+
+ // Returns stats from |agg_stats_|.
+ AggStats::Stats GetStats();
+
+ protected:
+ StatsCounter(Clock* clock,
+ bool include_empty_intervals,
+ StatsCounterObserver* observer);
+
+ void Add(int sample);
mflodman 2016/05/06 07:33:29 It would be good to add a comment for these two me
åsapersson 2016/05/09 14:36:50 Added more comments and examples.
+ 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 std::unique_ptr<StatsCounterObserver> observer_;
+ int64_t last_process_time_ms_;
+ AggStats agg_stats_;
+};
+
+// Classes which periodically computes a metric.
+//
+// During a period, |kProcessIntervalMs|, different metrics can be computed:
+// - |AvgCounter|: average of samples
+// - |PercentCounter|: percentage of samples
+// - |PermilleCounter|: permille of samples
+// - |RateCounter|: units per second (sums added |sample|)
+// - |AccRateCounter|: units per second (uses differences of set |sample|, to be
+// used for counters)
+//
+// Each periodic metric can be either:
+// - reported to an |observer| each period
+// - aggregated during the call (e.g. min, max, average)
+//
+// Note: takes ownership of |observer|.
+
+class AvgCounter : public StatsCounter {
+ public:
+ AvgCounter(Clock* clock, StatsCounterObserver* observer);
+ ~AvgCounter() override {}
+
+ void Add(int sample);
+
+ private:
+ bool GetMetric(int* metric) const override;
+
+ RTC_DISALLOW_COPY_AND_ASSIGN(AvgCounter);
+};
+
+class PercentCounter : public StatsCounter {
+ public:
+ PercentCounter(Clock* clock, StatsCounterObserver* observer);
+ ~PercentCounter() override {}
+
+ void Add(bool sample);
+
+ private:
+ bool GetMetric(int* metric) const override;
+
+ RTC_DISALLOW_COPY_AND_ASSIGN(PercentCounter);
+};
+
+class PermilleCounter : public StatsCounter {
+ public:
+ PermilleCounter(Clock* clock, StatsCounterObserver* observer);
+ ~PermilleCounter() override {}
+
+ void Add(bool sample);
+
+ private:
+ bool GetMetric(int* metric) const override;
+
+ RTC_DISALLOW_COPY_AND_ASSIGN(PermilleCounter);
+};
+
+class RateCounter : public StatsCounter {
+ public:
+ RateCounter(Clock* clock, StatsCounterObserver* observer);
+ ~RateCounter() override {}
+
+ void Add(int sample);
+
+ private:
+ bool GetMetric(int* metric) const override;
+
+ RTC_DISALLOW_COPY_AND_ASSIGN(RateCounter);
+};
+
+class RateAccCounter : public StatsCounter {
mflodman 2016/05/06 07:33:29 It would be good with a class description for thes
åsapersson 2016/05/09 14:36:50 Done.
+ public:
+ RateAccCounter(Clock* clock, StatsCounterObserver* observer);
+ ~RateAccCounter() override {}
+
+ void Set(int sample);
+
+ private:
+ bool GetMetric(int* metric) const override;
+
+ RTC_DISALLOW_COPY_AND_ASSIGN(RateAccCounter);
+};
+
+} // namespace webrtc
+
+#endif // WEBRTC_VIDEO_STATS_COUNTER_H_

Powered by Google App Engine
This is Rietveld 408576698