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 // RateCounter is defined to measure average rate over a given time window. | |
6 // Rate is reported as the sum of values recorded divided by the time window. | |
7 // This can be used for measuring bandwidth, bitrate, etc. | |
8 | |
9 // This class is thread-safe. | |
10 | |
11 #ifndef REMOTING_BASE_RATE_COUNTER_H_ | 5 #ifndef REMOTING_BASE_RATE_COUNTER_H_ |
12 #define REMOTING_BASE_RATE_COUNTER_H_ | 6 #define REMOTING_BASE_RATE_COUNTER_H_ |
13 | 7 |
14 #include <queue> | 8 #include <queue> |
15 #include <utility> | 9 #include <utility> |
16 | 10 |
17 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
18 #include "base/synchronization/lock.h" | 12 #include "base/threading/non_thread_safe.h" |
19 #include "base/time.h" | 13 #include "base/time.h" |
20 | 14 |
21 namespace remoting { | 15 namespace remoting { |
22 | 16 |
23 class RateCounter { | 17 // Measures average rate per second of a sequence of point rate samples |
| 18 // over a specified time window. This can be used to measure bandwidth, frame |
| 19 // rates, etc. |
| 20 class RateCounter : public base::NonThreadSafe { |
24 public: | 21 public: |
25 // Construct a counter for a specific time window. | 22 // Constructs a rate counter over the specified |time_window|. |
26 RateCounter(base::TimeDelta time_window); | 23 explicit RateCounter(base::TimeDelta time_window); |
27 | |
28 virtual ~RateCounter(); | 24 virtual ~RateCounter(); |
29 | 25 |
30 // Record the data point. | 26 // Records a point event count to include in the rate. |
31 void Record(int64 value); | 27 void Record(int64 value); |
32 | 28 |
33 // Report the rate recorded. At the beginning of recording the numbers before | 29 // Returns the rate-per-second of values recorded over the time window. |
34 // |time_window| is reached the reported rate will not be accurate. | 30 // Note that rates reported before |time_window| has elapsed are not accurate. |
35 double Rate(); | 31 double Rate(); |
36 | 32 |
| 33 // Overrides the current time for testing. |
| 34 void SetCurrentTimeForTest(base::Time current_time); |
| 35 |
37 private: | 36 private: |
38 // Helper function to evict old data points. | 37 // Type used to store data points with timestamps. |
39 void Evict(base::Time current_time); | |
40 | |
41 // A data point consists of a timestamp and a data value. | |
42 typedef std::pair<base::Time, int64> DataPoint; | 38 typedef std::pair<base::Time, int64> DataPoint; |
43 | 39 |
44 // Duration of the time window. | 40 // Removes data points more than |time_window| older than |current_time|. |
45 base::TimeDelta time_window_; | 41 void EvictOldDataPoints(base::Time current_time); |
46 | 42 |
47 // Protects |data_points_| and |sum_|. | 43 // Returns the current time specified for test, if set, or base::Time::Now(). |
48 base::Lock lock_; | 44 base::Time CurrentTime() const; |
49 | 45 |
50 // Keep the values of all the data points in a queue. | 46 // Time window over which to calculate the rate. |
| 47 const base::TimeDelta time_window_; |
| 48 |
| 49 // Queue containing data points in the order in which they were recorded. |
51 std::queue<DataPoint> data_points_; | 50 std::queue<DataPoint> data_points_; |
52 | 51 |
53 // Sum of values in |data_points_|. | 52 // Sum of values in |data_points_|. |
54 int64 sum_; | 53 int64 sum_; |
55 | 54 |
| 55 // If set, used to calculate the running average, in place of Now(). |
| 56 base::Time current_time_for_test_; |
| 57 |
56 DISALLOW_COPY_AND_ASSIGN(RateCounter); | 58 DISALLOW_COPY_AND_ASSIGN(RateCounter); |
57 }; | 59 }; |
58 | 60 |
59 } // namespace remoting | 61 } // namespace remoting |
60 | 62 |
61 #endif // REMOTING_BASE_RATE_COUNTER_H_ | 63 #endif // REMOTING_BASE_RATE_COUNTER_H_ |
OLD | NEW |