summaryrefslogtreecommitdiffstats
path: root/remoting/base
diff options
context:
space:
mode:
Diffstat (limited to 'remoting/base')
-rw-r--r--remoting/base/rate_counter.cc49
-rw-r--r--remoting/base/rate_counter.h61
-rw-r--r--remoting/base/running_average.cc39
-rw-r--r--remoting/base/running_average.h57
4 files changed, 206 insertions, 0 deletions
diff --git a/remoting/base/rate_counter.cc b/remoting/base/rate_counter.cc
new file mode 100644
index 0000000..fff9b6c
--- /dev/null
+++ b/remoting/base/rate_counter.cc
@@ -0,0 +1,49 @@
+// 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),
+ sum_(0) {
+}
+
+RateCounter::~RateCounter() {
+}
+
+void RateCounter::Record(int64 value) {
+ 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 static_cast<double>(base::Time::kMillisecondsPerSecond) * sum_ /
+ time_window_.InMilliseconds();
+}
+
+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 (data_points_.front().first > window_start)
+ break;
+
+ sum_ -= data_points_.front().second;
+ data_points_.pop();
+ }
+}
+
+} // namespace remoting
diff --git a/remoting/base/rate_counter.h b/remoting/base/rate_counter.h
new file mode 100644
index 0000000..c427b96
--- /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_
diff --git a/remoting/base/running_average.cc b/remoting/base/running_average.cc
new file mode 100644
index 0000000..4daa650
--- /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();
+ }
+}
+
+double RunningAverage::Average() {
+ base::AutoLock auto_lock(lock_);
+
+ if (data_points_.empty())
+ return 0;
+ return static_cast<double>(sum_) / data_points_.size();
+}
+
+} // namespace remoting
diff --git a/remoting/base/running_average.h b/remoting/base/running_average.h
new file mode 100644
index 0000000..fadea1f
--- /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
+// this data comes in as a stream and fluctuates a lot. They are processed by
+// 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
+ // |windows_size| most recent values are kept and the average is reported.
+ 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.
+ double Average();
+
+ 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_