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