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

Unified Diff: remoting/base/rate_counter.h

Issue 6736009: Measure bandwidth for chromoting video channel (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: done Created 9 years, 9 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
« no previous file with comments | « no previous file | remoting/base/rate_counter.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: remoting/base/rate_counter.h
diff --git a/remoting/base/rate_counter.h b/remoting/base/rate_counter.h
new file mode 100644
index 0000000000000000000000000000000000000000..c427b9603f8c6041dcd40ad8bc1efcd2f23f08b8
--- /dev/null
+++ b/remoting/base/rate_counter.h
@@ -0,0 +1,61 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// RateCounter is defined to measure average rate over a given time window.
+// Rate is reported as the sum of values recorded divided by the time window.
+// This can be used for measuring bandwidth, bitrate, etc.
+
+// This class is thread-safe.
+
+#ifndef REMOTING_BASE_RATE_COUNTER_H_
+#define REMOTING_BASE_RATE_COUNTER_H_
+
+#include <queue>
+#include <utility>
+
+#include "base/basictypes.h"
+#include "base/synchronization/lock.h"
+#include "base/time.h"
+
+namespace remoting {
+
+class RateCounter {
+ public:
+ // Construct a counter for a specific time window.
+ RateCounter(base::TimeDelta time_window);
+
+ virtual ~RateCounter();
+
+ // Record the data point.
+ void Record(int64 value);
+
+ // Report the rate recorded. At the beginning of recording the numbers before
+ // |time_window| is reached the reported rate will not be accurate.
+ double Rate();
+
+ private:
+ // Helper function to evict old data points.
+ void Evict(base::Time current_time);
+
+ // A data point consists of a timestamp and a data value.
+ typedef std::pair<base::Time, int64> DataPoint;
+
+ // Duration of the time window.
+ base::TimeDelta time_window_;
+
+ // Protects |data_points_| and |sum_|.
+ base::Lock lock_;
+
+ // Keep the values of all the data points in a queue.
+ std::queue<DataPoint> data_points_;
+
+ // Sum of values in |data_points_|.
+ int64 sum_;
+
+ DISALLOW_COPY_AND_ASSIGN(RateCounter);
+};
+
+} // namespace remoting
+
+#endif // REMOTING_BASE_RATE_COUNTER_H_
« no previous file with comments | « no previous file | remoting/base/rate_counter.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698