Index: remoting/base/running_average.cc |
diff --git a/remoting/base/running_average.cc b/remoting/base/running_average.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3be1b3a9ef6ef1be8466d360bd449844f3193a11 |
--- /dev/null |
+++ b/remoting/base/running_average.cc |
@@ -0,0 +1,39 @@ |
+// 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. |
+ |
+#include "base/logging.h" |
+#include "remoting/base/running_average.h" |
+ |
+namespace remoting { |
+ |
+RunningAverage::RunningAverage(int window_size) |
+ : window_size_(window_size), |
+ sum_(0) { |
+ CHECK(window_size_); |
+} |
+ |
+RunningAverage::~RunningAverage() { |
+} |
+ |
+void RunningAverage::Record(int64 value) { |
+ base::AutoLock auto_lock(lock_); |
+ |
+ data_points_.push_back(value); |
+ sum_ += value; |
+ |
+ if (data_points_.size() > window_size_) { |
+ sum_ -= data_points_[0]; |
+ data_points_.pop_front(); |
+ } |
+} |
+ |
+int64 RunningAverage::Average() { |
+ base::AutoLock auto_lock(lock_); |
+ |
+ if (data_points_.empty()) |
+ return 0; |
+ return sum_ / data_points_.size(); |
+} |
+ |
+} // namespace remoting |