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

Unified Diff: remoting/base/rate_counter.cc

Issue 6736009: Measure bandwidth for chromoting video channel (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: added rate counter 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/rate_counter.cc
diff --git a/remoting/base/rate_counter.cc b/remoting/base/rate_counter.cc
new file mode 100644
index 0000000000000000000000000000000000000000..76e5d7416828a4c679eebcdff955a577465e75ba
--- /dev/null
+++ b/remoting/base/rate_counter.cc
@@ -0,0 +1,48 @@
+// 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 "remoting/base/rate_counter.h"
+
+namespace remoting {
+
+RateCounter::RateCounter(base::TimeDelta time_window)
+ : time_window_(time_window) {
simonmorris 2011/04/04 10:27:34 Initialise sum_.
Alpha Left Google 2011/04/05 00:16:06 Done.
+}
+
+RateCounter::~RateCounter() {
+}
+
+void RateCounter::Record(int64 value) {
simonmorris 2011/04/04 10:27:34 It might be worth adding a Record(int64, base::Tim
Alpha Left Google 2011/04/05 00:16:06 How is this related to unit testing? I mean this i
+ base::Time current_time = base::Time::Now();
+ Evict(current_time);
+
+ base::AutoLock auto_lock(lock_);
+ sum_ += value;
+ data_points_.push(std::make_pair(current_time, value));
+}
+
+double RateCounter::Rate() {
+ Evict(base::Time::Now());
+
+ base::AutoLock auto_lock(lock_);
+ return 1.0 * base::Time::kMillisecondsPerSecond * sum_ /
simonmorris 2011/04/04 10:27:34 Clearer to explicitly cast to a double.
Alpha Left Google 2011/04/05 00:16:06 Done.
+ time_window_.InMilliseconds();
simonmorris 2011/04/04 10:27:34 What if the first data point was taken more recent
Alpha Left Google 2011/04/05 00:16:06 Done.
+}
+
+void RateCounter::Evict(base::Time current_time) {
+ base::AutoLock auto_lock(lock_);
+
+ // Remove data points outside of the window.
+ base::Time window_start = current_time - time_window_;
+
+ while (!data_points_.empty()) {
+ if (window_start < data_points_.front().first)
simonmorris 2011/04/04 10:27:34 Clearer to reverse the order, so the non-const is
Alpha Left Google 2011/04/05 00:16:06 Done.
+ break;
+
+ sum_ -= data_points_.front().second;
+ data_points_.pop();
+ }
+}
+
+} // namespace remoting

Powered by Google App Engine
This is Rietveld 408576698