Index: remoting/base/running_average.h |
diff --git a/remoting/base/running_average.h b/remoting/base/running_average.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0b6705c3e7f5b971ba9b992c654899e875b86e3d |
--- /dev/null |
+++ b/remoting/base/running_average.h |
@@ -0,0 +1,57 @@ |
+// 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. |
+ |
+// RunningAverage defined in this file is used to generate statistics for |
+// bandwidth, latency and other performance metrics for remoting. Usually |
+// these data come in as a stream and fluctuates a lot. They are processed by |
simonmorris
2011/04/04 10:27:34
"this...comes...fluctuates" or "these...come...flu
Alpha Left Google
2011/04/05 00:16:06
Done.
Alpha Left Google
2011/04/05 00:16:06
Done.
|
+// this class to generate a more stable value by taking average within a |
+// window of data points. |
+ |
+// All classes defined are thread-safe. |
+ |
+#ifndef REMOTING_BASE_RUNNING_AVERAGE_H_ |
+#define REMOTING_BASE_RUNNING_AVERAGE_H_ |
+ |
+#include <deque> |
+ |
+#include "base/basictypes.h" |
+#include "base/synchronization/lock.h" |
+#include "base/time.h" |
+ |
+namespace remoting { |
+ |
+class RunningAverage { |
+ public: |
+ // Construct a running average counter for a specific window size. The most |
+ // |windows_size| recent values are kept and the average is reported. |
simonmorris
2011/04/04 10:27:34
"|windows_size| most recent", not "most |windows_s
Alpha Left Google
2011/04/05 00:16:06
Done.
|
+ RunningAverage(int window_size); |
+ |
+ virtual ~RunningAverage(); |
+ |
+ // Record the provided data point. |
+ void Record(int64 value); |
+ |
+ // Return the average of data points in the last window. |
+ int64 Average(); |
simonmorris
2011/04/04 10:27:34
Is int64 better than the expected double?
Alpha Left Google
2011/04/05 00:16:06
In a later patch this is changed to double.
|
+ |
+ private: |
+ // Size of the window. This is of type size_t to avoid casting when comparing |
+ // with the size of |data_points_|. |
+ size_t window_size_; |
+ |
+ // Protects |data_points_| and |sum_|. |
+ base::Lock lock_; |
+ |
+ // Keep the values of all the data points. |
+ std::deque<int64> data_points_; |
+ |
+ // Sum of values in |data_points_|. |
+ int64 sum_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(RunningAverage); |
+}; |
+ |
+} // namespace remoting |
+ |
+#endif // REMOTING_BASE_RUNNING_AVERAGE_H_ |