OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
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. | |
10 | |
11 // All classes defined are thread-safe. | |
12 | |
13 #ifndef REMOTING_BASE_RUNNING_AVERAGE_H_ | 5 #ifndef REMOTING_BASE_RUNNING_AVERAGE_H_ |
14 #define REMOTING_BASE_RUNNING_AVERAGE_H_ | 6 #define REMOTING_BASE_RUNNING_AVERAGE_H_ |
15 | 7 |
16 #include <deque> | 8 #include <deque> |
17 | 9 |
18 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
19 #include "base/synchronization/lock.h" | 11 #include "base/threading/non_thread_safe.h" |
20 | 12 |
21 namespace remoting { | 13 namespace remoting { |
22 | 14 |
23 class RunningAverage { | 15 // Calculates the average of the most recent N recorded samples. |
| 16 // This is typically used to smooth out random variation in point samples |
| 17 // over bandwidth, frame rate, etc. |
| 18 class RunningAverage : public base::NonThreadSafe { |
24 public: | 19 public: |
25 // Construct a running average counter for a specific window size. The | 20 // Constructs a helper to average over the |window_size| most recent samples. |
26 // |windows_size| most recent values are kept and the average is reported. | |
27 explicit RunningAverage(int window_size); | 21 explicit RunningAverage(int window_size); |
28 | |
29 virtual ~RunningAverage(); | 22 virtual ~RunningAverage(); |
30 | 23 |
31 // Record the provided data point. | 24 // Records a point sample. |
32 void Record(int64 value); | 25 void Record(int64 value); |
33 | 26 |
34 // Return the average of data points in the last window. | 27 // Returns the average over up to |window_size| of the most recent samples. |
35 double Average(); | 28 double Average() const; |
36 | 29 |
37 private: | 30 private: |
38 // Size of the window. This is of type size_t to avoid casting when comparing | 31 // Stores the desired window size, as size_t to avoid casting when comparing |
39 // with the size of |data_points_|. | 32 // with the size of |data_points_|. |
40 size_t window_size_; | 33 const size_t window_size_; |
41 | 34 |
42 // Protects |data_points_| and |sum_|. | 35 // Stores the |window_size| most recently recorded samples. |
43 base::Lock lock_; | |
44 | |
45 // Keep the values of all the data points. | |
46 std::deque<int64> data_points_; | 36 std::deque<int64> data_points_; |
47 | 37 |
48 // Sum of values in |data_points_|. | 38 // Holds the sum of the samples in |data_points_|. |
49 int64 sum_; | 39 int64 sum_; |
50 | 40 |
51 DISALLOW_COPY_AND_ASSIGN(RunningAverage); | 41 DISALLOW_COPY_AND_ASSIGN(RunningAverage); |
52 }; | 42 }; |
53 | 43 |
54 } // namespace remoting | 44 } // namespace remoting |
55 | 45 |
56 #endif // REMOTING_BASE_RUNNING_AVERAGE_H_ | 46 #endif // REMOTING_BASE_RUNNING_AVERAGE_H_ |
OLD | NEW |