Remove deprecated extension notification and
[chromium-blink-merge.git] / remoting / base / rate_counter.h
blob083ec94083348535c807d3b2df7d8d256dc7e957
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.
5 #ifndef REMOTING_BASE_RATE_COUNTER_H_
6 #define REMOTING_BASE_RATE_COUNTER_H_
8 #include <queue>
9 #include <utility>
11 #include "base/basictypes.h"
12 #include "base/threading/non_thread_safe.h"
13 #include "base/time/time.h"
15 namespace remoting {
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 {
21 public:
22 // Constructs a rate counter over the specified |time_window|.
23 explicit RateCounter(base::TimeDelta time_window);
24 virtual ~RateCounter();
26 // Records a point event count to include in the rate.
27 void Record(int64 value);
29 // Returns the rate-per-second of values recorded over the time window.
30 // Note that rates reported before |time_window| has elapsed are not accurate.
31 double Rate();
33 // Overrides the current time for testing.
34 void SetCurrentTimeForTest(base::Time current_time);
36 private:
37 // Type used to store data points with timestamps.
38 typedef std::pair<base::Time, int64> DataPoint;
40 // Removes data points more than |time_window| older than |current_time|.
41 void EvictOldDataPoints(base::Time current_time);
43 // Returns the current time specified for test, if set, or base::Time::Now().
44 base::Time CurrentTime() const;
46 // Time window over which to calculate the rate.
47 const base::TimeDelta time_window_;
49 // Queue containing data points in the order in which they were recorded.
50 std::queue<DataPoint> data_points_;
52 // Sum of values in |data_points_|.
53 int64 sum_;
55 // If set, used to calculate the running average, in place of Now().
56 base::Time current_time_for_test_;
58 DISALLOW_COPY_AND_ASSIGN(RateCounter);
61 } // namespace remoting
63 #endif // REMOTING_BASE_RATE_COUNTER_H_