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 | 5 // RunningAverage defined in this file is used to generate statistics for |
6 // bandwidth, latency and other performance metrics for remoting. Usually | 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 | 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 | 8 // this class to generate a more stable value by taking average within a |
9 // window of data points. | 9 // window of data points. |
10 | 10 |
11 // All classes defined are thread-safe. | 11 // All classes defined are thread-safe. |
12 | 12 |
13 #ifndef REMOTING_BASE_RUNNING_AVERAGE_H_ | 13 #ifndef REMOTING_BASE_RUNNING_AVERAGE_H_ |
14 #define REMOTING_BASE_RUNNING_AVERAGE_H_ | 14 #define REMOTING_BASE_RUNNING_AVERAGE_H_ |
15 | 15 |
16 #include <deque> | 16 #include <deque> |
17 | 17 |
18 #include "base/basictypes.h" | 18 #include "base/basictypes.h" |
19 #include "base/synchronization/lock.h" | 19 #include "base/synchronization/lock.h" |
20 #include "base/time.h" | |
21 | 20 |
22 namespace remoting { | 21 namespace remoting { |
23 | 22 |
24 class RunningAverage { | 23 class RunningAverage { |
25 public: | 24 public: |
26 // Construct a running average counter for a specific window size. The | 25 // Construct a running average counter for a specific window size. The |
27 // |windows_size| most recent values are kept and the average is reported. | 26 // |windows_size| most recent values are kept and the average is reported. |
28 RunningAverage(int window_size); | 27 explicit RunningAverage(int window_size); |
29 | 28 |
30 virtual ~RunningAverage(); | 29 virtual ~RunningAverage(); |
31 | 30 |
32 // Record the provided data point. | 31 // Record the provided data point. |
33 void Record(int64 value); | 32 void Record(int64 value); |
34 | 33 |
35 // Return the average of data points in the last window. | 34 // Return the average of data points in the last window. |
36 double Average(); | 35 double Average(); |
37 | 36 |
38 private: | 37 private: |
39 // Size of the window. This is of type size_t to avoid casting when comparing | 38 // Size of the window. This is of type size_t to avoid casting when comparing |
40 // with the size of |data_points_|. | 39 // with the size of |data_points_|. |
41 size_t window_size_; | 40 size_t window_size_; |
42 | 41 |
43 // Protects |data_points_| and |sum_|. | 42 // Protects |data_points_| and |sum_|. |
44 base::Lock lock_; | 43 base::Lock lock_; |
45 | 44 |
46 // Keep the values of all the data points. | 45 // Keep the values of all the data points. |
47 std::deque<int64> data_points_; | 46 std::deque<int64> data_points_; |
48 | 47 |
49 // Sum of values in |data_points_|. | 48 // Sum of values in |data_points_|. |
50 int64 sum_; | 49 int64 sum_; |
51 | 50 |
52 DISALLOW_COPY_AND_ASSIGN(RunningAverage); | 51 DISALLOW_COPY_AND_ASSIGN(RunningAverage); |
53 }; | 52 }; |
54 | 53 |
55 } // namespace remoting | 54 } // namespace remoting |
56 | 55 |
57 #endif // REMOTING_BASE_RUNNING_AVERAGE_H_ | 56 #endif // REMOTING_BASE_RUNNING_AVERAGE_H_ |
OLD | NEW |