Revert 187554 "Implement IPC::ChannelFactory, a class that accep..."
[chromium-blink-merge.git] / remoting / base / running_average.h
blobbc556b9a0001cc1d5daa9753e943ac95d4a8db1b
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // RunningAverage defined in this file is used to generate statistics for
6 // bandwidth, latency and other performance metrics for remoting. Usually
7 // this data comes in as a stream and fluctuates a lot. They are processed by
8 // this class to generate a more stable value by taking average within a
9 // window of data points.
11 // All classes defined are thread-safe.
13 #ifndef REMOTING_BASE_RUNNING_AVERAGE_H_
14 #define REMOTING_BASE_RUNNING_AVERAGE_H_
16 #include <deque>
18 #include "base/basictypes.h"
19 #include "base/synchronization/lock.h"
21 namespace remoting {
23 class RunningAverage {
24 public:
25 // Construct a running average counter for a specific window size. The
26 // |windows_size| most recent values are kept and the average is reported.
27 explicit RunningAverage(int window_size);
29 virtual ~RunningAverage();
31 // Record the provided data point.
32 void Record(int64 value);
34 // Return the average of data points in the last window.
35 double Average();
37 private:
38 // Size of the window. This is of type size_t to avoid casting when comparing
39 // with the size of |data_points_|.
40 size_t window_size_;
42 // Protects |data_points_| and |sum_|.
43 base::Lock lock_;
45 // Keep the values of all the data points.
46 std::deque<int64> data_points_;
48 // Sum of values in |data_points_|.
49 int64 sum_;
51 DISALLOW_COPY_AND_ASSIGN(RunningAverage);
54 } // namespace remoting
56 #endif // REMOTING_BASE_RUNNING_AVERAGE_H_