| 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 #ifndef REMOTING_BASE_RATE_COUNTER_H_ | 5 #ifndef REMOTING_BASE_RATE_COUNTER_H_ | 
| 6 #define REMOTING_BASE_RATE_COUNTER_H_ | 6 #define REMOTING_BASE_RATE_COUNTER_H_ | 
| 7 | 7 | 
| 8 #include <stdint.h> | 8 #include <stdint.h> | 
| 9 | 9 | 
| 10 #include <queue> | 10 #include <queue> | 
| 11 #include <utility> | 11 #include <utility> | 
| 12 | 12 | 
| 13 #include "base/macros.h" | 13 #include "base/macros.h" | 
| 14 #include "base/threading/non_thread_safe.h" | 14 #include "base/threading/non_thread_safe.h" | 
|  | 15 #include "base/time/default_tick_clock.h" | 
|  | 16 #include "base/time/tick_clock.h" | 
| 15 #include "base/time/time.h" | 17 #include "base/time/time.h" | 
| 16 | 18 | 
| 17 namespace remoting { | 19 namespace remoting { | 
| 18 | 20 | 
| 19 // Measures average rate per second of a sequence of point rate samples | 21 // Measures average rate per second of a sequence of point rate samples | 
| 20 // over a specified time window. This can be used to measure bandwidth, frame | 22 // over a specified time window. This can be used to measure bandwidth, frame | 
| 21 // rates, etc. | 23 // rates, etc. | 
| 22 class RateCounter : public base::NonThreadSafe { | 24 class RateCounter : public base::NonThreadSafe { | 
| 23  public: | 25  public: | 
| 24   // Constructs a rate counter over the specified |time_window|. | 26   // Constructs a rate counter over the specified |time_window|. | 
| 25   explicit RateCounter(base::TimeDelta time_window); | 27   explicit RateCounter(base::TimeDelta time_window); | 
| 26   virtual ~RateCounter(); | 28   virtual ~RateCounter(); | 
| 27 | 29 | 
| 28   // Records a point event count to include in the rate. | 30   // Records a point event count to include in the rate. | 
| 29   void Record(int64_t value); | 31   void Record(int64_t value); | 
| 30 | 32 | 
| 31   // Returns the rate-per-second of values recorded over the time window. | 33   // Returns the rate-per-second of values recorded over the time window. | 
| 32   // Note that rates reported before |time_window| has elapsed are not accurate. | 34   // Note that rates reported before |time_window| has elapsed are not accurate. | 
| 33   double Rate(); | 35   double Rate(); | 
| 34 | 36 | 
| 35   // Overrides the current time for testing. | 37   void set_tick_clock_for_tests(base::TickClock* tick_clock) { | 
| 36   void SetCurrentTimeForTest(base::Time current_time); | 38     tick_clock_ = tick_clock; | 
|  | 39   } | 
| 37 | 40 | 
| 38  private: | 41  private: | 
| 39   // Type used to store data points with timestamps. | 42   // Type used to store data points with timestamps. | 
| 40   typedef std::pair<base::Time, int64_t> DataPoint; | 43   typedef std::pair<base::TimeTicks, int64_t> DataPoint; | 
| 41 | 44 | 
| 42   // Removes data points more than |time_window| older than |current_time|. | 45   // Removes data points more than |time_window| older than |current_time|. | 
| 43   void EvictOldDataPoints(base::Time current_time); | 46   void EvictOldDataPoints(base::TimeTicks current_time); | 
| 44 |  | 
| 45   // Returns the current time specified for test, if set, or base::Time::Now(). |  | 
| 46   base::Time CurrentTime() const; |  | 
| 47 | 47 | 
| 48   // Time window over which to calculate the rate. | 48   // Time window over which to calculate the rate. | 
| 49   const base::TimeDelta time_window_; | 49   const base::TimeDelta time_window_; | 
| 50 | 50 | 
| 51   // Queue containing data points in the order in which they were recorded. | 51   // Queue containing data points in the order in which they were recorded. | 
| 52   std::queue<DataPoint> data_points_; | 52   std::queue<DataPoint> data_points_; | 
| 53 | 53 | 
| 54   // Sum of values in |data_points_|. | 54   // Sum of values in |data_points_|. | 
| 55   int64_t sum_; | 55   int64_t sum_; | 
| 56 | 56 | 
| 57   // If set, used to calculate the running average, in place of Now(). | 57   base::DefaultTickClock default_tick_clock_; | 
| 58   base::Time current_time_for_test_; | 58   base::TickClock* tick_clock_ = &default_tick_clock_; | 
| 59 | 59 | 
| 60   DISALLOW_COPY_AND_ASSIGN(RateCounter); | 60   DISALLOW_COPY_AND_ASSIGN(RateCounter); | 
| 61 }; | 61 }; | 
| 62 | 62 | 
| 63 }  // namespace remoting | 63 }  // namespace remoting | 
| 64 | 64 | 
| 65 #endif  // REMOTING_BASE_RATE_COUNTER_H_ | 65 #endif  // REMOTING_BASE_RATE_COUNTER_H_ | 
| OLD | NEW | 
|---|