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 double Rate(); |
| 34 |
| 35 private: |
| 36 // Helper function to evict old data points. |
| 37 void Evict(base::Time current_time); |
| 38 |
| 39 // A data point consists of a timestamp and a data value. |
| 40 typedef std::pair<base::Time, int64> DataPoint; |
| 41 |
| 42 // Duration of the time window. |
| 43 base::TimeDelta time_window_; |
| 44 |
| 45 // Protects |data_points_| and |sum_|. |
| 46 base::Lock lock_; |
| 47 |
| 48 // Keep the values of all the data points in a queue. |
| 49 std::queue<DataPoint> data_points_; |
| 50 |
| 51 // Sum of values in |data_points_|. |
| 52 int64 sum_; |
| 53 |
| 54 DISALLOW_COPY_AND_ASSIGN(RateCounter); |
| 55 }; |
| 56 |
| 57 } // namespace remoting |
| 58 |
| 59 #endif // REMOTING_BASE_RATE_COUNTER_H_ |
OLD | NEW |