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

Unified Diff: remoting/base/running_average.cc

Issue 6736009: Measure bandwidth for chromoting video channel (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: 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
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..d43dc1cecc3c3dc0932aaa4fb91c2efe5e280416
--- /dev/null
+++ b/remoting/base/running_average.cc
@@ -0,0 +1,73 @@
+// 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();
+}
+
+TimedRunningAverage::TimedRunningAverage(int window_size)
+ : counter_(window_size),
+ window_size_(window_size) {
+}
+
+TimedRunningAverage::~TimedRunningAverage() {
+}
+
+void TimedRunningAverage::Record(int64 value) {
+ {
+ base::AutoLock auto_lock(lock_);
+ time_.push_back(base::Time::Now());
+
+ if (time_.size() > window_size_)
+ time_.pop_front();
+ }
+ counter_.Record(value);
+}
+
+double TimedRunningAverage::Rate() {
+ int64 average = counter_.Average();
+ base::TimeDelta delta;
+
+ {
+ base::AutoLock auto_lock(lock_);
+ if (!time_.empty())
+ delta = *time_.rbegin() - *time_.begin();
+ }
+
+ if (delta.InMilliseconds())
+ return 1000.0 * average / delta.InMilliseconds();
simonmorris 2011/03/25 09:24:59 I'm not sure the math is right here. For a rate, d
Wez 2011/03/25 10:43:19 Yes. The rate (bandwidth in our case) would norma
+ return 0;
+}
+
+} // namespace remoting

Powered by Google App Engine
This is Rietveld 408576698