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/synchronization/lock.h" |
alexeypa (please no reviews)
2013/03/19 23:36:55
nit: lock.h is not needed any more.
Wez
2013/03/20 00:17:27
Done.
| |
12 #include "base/threading/non_thread_safe.h" | |
20 | 13 |
21 namespace remoting { | 14 namespace remoting { |
22 | 15 |
23 class RunningAverage { | 16 // Calculates the average of the most recent N recorded samples. |
17 // This is typically used to smooth out random variation in point samples | |
18 // over bandwidth, frame rate, etc. | |
19 class RunningAverage : public base::NonThreadSafe { | |
24 public: | 20 public: |
25 // Construct a running average counter for a specific window size. The | 21 // 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); | 22 explicit RunningAverage(int window_size); |
28 | |
29 virtual ~RunningAverage(); | 23 virtual ~RunningAverage(); |
30 | 24 |
31 // Record the provided data point. | 25 // Records a point sample. |
32 void Record(int64 value); | 26 void Record(int64 value); |
33 | 27 |
34 // Return the average of data points in the last window. | 28 // Returns the average over up to |window_size| of the most recent samples. |
35 double Average(); | 29 double Average() const; |
36 | 30 |
37 private: | 31 private: |
38 // Size of the window. This is of type size_t to avoid casting when comparing | 32 // Stores the desired window size, as size_t to avoid casting when comparing |
39 // with the size of |data_points_|. | 33 // with the size of |data_points_|. |
40 size_t window_size_; | 34 const size_t window_size_; |
41 | 35 |
42 // Protects |data_points_| and |sum_|. | 36 // 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_; | 37 std::deque<int64> data_points_; |
47 | 38 |
48 // Sum of values in |data_points_|. | 39 // Holds the sum of the samples in |data_points_|. |
49 int64 sum_; | 40 int64 sum_; |
50 | 41 |
51 DISALLOW_COPY_AND_ASSIGN(RunningAverage); | 42 DISALLOW_COPY_AND_ASSIGN(RunningAverage); |
52 }; | 43 }; |
53 | 44 |
54 } // namespace remoting | 45 } // namespace remoting |
55 | 46 |
56 #endif // REMOTING_BASE_RUNNING_AVERAGE_H_ | 47 #endif // REMOTING_BASE_RUNNING_AVERAGE_H_ |
OLD | NEW |