OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 // RunningAverage defined in this file is used to generate statistics for | |
6 // bandwidth, latency and other performance metrics for remoting. Usually | |
7 // these data come 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 // TimedRunningAverage keeps track of running average in a timed faction. | |
simonmorris
2011/03/25 09:24:59
"Fashion" instead of "faction"?
| |
12 // It essentially reports the rate of RunningAverage divided by the duration | |
13 // of the window. | |
14 | |
15 // All classes defined are thread-safe. | |
16 | |
17 #ifndef REMOTING_BASE_RUNNING_AVERAGE_H_ | |
18 #define REMOTING_BASE_RUNNING_AVERAGE_H_ | |
19 | |
20 #include <deque> | |
21 | |
22 #include "base/basictypes.h" | |
23 #include "base/synchronization/lock.h" | |
24 #include "base/time.h" | |
25 | |
26 namespace remoting { | |
27 | |
28 class RunningAverage { | |
29 public: | |
30 // Construct a running average counter for a specific window size. The most | |
simonmorris
2011/03/25 09:24:59
"|windows_size| most"
| |
31 // |windows_size| recent values are kept and the average is reported. | |
32 RunningAverage(int window_size); | |
33 | |
34 virtual ~RunningAverage(); | |
35 | |
36 // Record the provided data point. | |
37 void Record(int64 value); | |
38 | |
39 // Return the average of data points in the last window. | |
40 int64 Average(); | |
41 | |
42 private: | |
43 // Size of the window. This is of type size_t to avoid casting when comparing | |
44 // with the size of |data_points_|. | |
45 size_t window_size_; | |
46 | |
47 // Protects |data_points_| and |sum_|. | |
48 base::Lock lock_; | |
49 | |
50 // Keep the values of all the data points. | |
51 std::deque<int64> data_points_; | |
52 | |
53 // Sum of values in |data_points_|. | |
54 int64 sum_; | |
55 | |
56 DISALLOW_COPY_AND_ASSIGN(RunningAverage); | |
57 }; | |
58 | |
59 class TimedRunningAverage { | |
60 public: | |
61 // Construct with a window size. | |
62 TimedRunningAverage(int window_size); | |
simonmorris
2011/03/25 09:24:59
Is it better for the window to be a length of time
| |
63 | |
64 virtual ~TimedRunningAverage(); | |
65 | |
66 // Record the value and the current time. | |
67 void Record(int64 value); | |
68 | |
69 // Report the rate per second recorded. | |
70 double Rate(); | |
71 | |
72 private: | |
73 // Protects |time_|. | |
74 base::Lock lock_; | |
75 | |
76 // Timestamps when Record() is called. | |
77 std::deque<base::Time> time_; | |
78 | |
79 // Counter to measure the running average. | |
80 RunningAverage counter_; | |
81 | |
82 // Window size for the rate. | |
83 size_t window_size_; | |
84 | |
85 DISALLOW_COPY_AND_ASSIGN(TimedRunningAverage); | |
86 }; | |
87 | |
88 } // namespace remoting | |
89 | |
90 #endif // REMOTING_BASE_RUNNING_AVERAGE_H_ | |
OLD | NEW |