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 // 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_ |
| 12 #define REMOTING_BASE_RATE_COUNTER_H_ |
| 13 |
| 14 #include <queue> |
| 15 #include <utility> |
| 16 |
| 17 #include "base/basictypes.h" |
| 18 #include "base/synchronization/lock.h" |
| 19 #include "base/time.h" |
| 20 |
| 21 namespace remoting { |
| 22 |
| 23 class RateCounter { |
| 24 public: |
| 25 // Construct a counter for a specific time window. |
| 26 RateCounter(base::TimeDelta time_window); |
| 27 |
| 28 virtual ~RateCounter(); |
| 29 |
| 30 // Record the data point. |
| 31 void Record(int64 value); |
| 32 |
| 33 // Report the rate recorded. At the beginning of recording the numbers before |
| 34 // |time_window| is reached the reported rate will not be accurate. |
| 35 double Rate(); |
| 36 |
| 37 private: |
| 38 // Helper function to evict old data points. |
| 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; |
| 43 |
| 44 // Duration of the time window. |
| 45 base::TimeDelta time_window_; |
| 46 |
| 47 // Protects |data_points_| and |sum_|. |
| 48 base::Lock lock_; |
| 49 |
| 50 // Keep the values of all the data points in a queue. |
| 51 std::queue<DataPoint> data_points_; |
| 52 |
| 53 // Sum of values in |data_points_|. |
| 54 int64 sum_; |
| 55 |
| 56 DISALLOW_COPY_AND_ASSIGN(RateCounter); |
| 57 }; |
| 58 |
| 59 } // namespace remoting |
| 60 |
| 61 #endif // REMOTING_BASE_RATE_COUNTER_H_ |
OLD | NEW |