Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(408)

Side by Side Diff: remoting/base/rate_counter.h

Issue 12803008: Add unit tests for sub-components of CaptureScheduler. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | remoting/base/rate_counter.cc » ('j') | remoting/base/rate_counter.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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. 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. 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. 7 // This can be used for measuring bandwidth, bitrate, etc.
8 8
9 // This class is thread-safe. 9 // This class is thread-safe.
10 10
11 #ifndef REMOTING_BASE_RATE_COUNTER_H_ 11 #ifndef REMOTING_BASE_RATE_COUNTER_H_
12 #define REMOTING_BASE_RATE_COUNTER_H_ 12 #define REMOTING_BASE_RATE_COUNTER_H_
13 13
14 #include <queue> 14 #include <queue>
15 #include <utility> 15 #include <utility>
16 16
17 #include "base/basictypes.h" 17 #include "base/basictypes.h"
18 #include "base/synchronization/lock.h" 18 #include "base/synchronization/lock.h"
19 #include "base/time.h" 19 #include "base/time.h"
20 20
21 namespace remoting { 21 namespace remoting {
22 22
23 class RateCounter { 23 class RateCounter {
24 public: 24 public:
25 // Construct a counter for a specific time window. 25 // Constructs a rate counter over |time_window|.
26 RateCounter(base::TimeDelta time_window); 26 RateCounter(base::TimeDelta time_window);
alexeypa (please no reviews) 2013/03/18 19:39:39 nit: explicit
Wez 2013/03/19 23:23:14 Done.
27 ~RateCounter();
27 28
28 virtual ~RateCounter(); 29 // Records a data point.
29
30 // Record the data point.
31 void Record(int64 value); 30 void Record(int64 value);
32 31
33 // Report the rate recorded. At the beginning of recording the numbers before 32 // Returns the rate-per-second of recorded values over the time window.
34 // |time_window| is reached the reported rate will not be accurate. 33 // Rates reported before |time_window| has elapsed are not accurate.
35 double Rate(); 34 double Rate();
36 35
36 // Overrides the current time for testing.
37 void SetCurrentTimeForTest(base::Time current_time);
alexeypa (please no reviews) 2013/03/18 19:39:39 It might be better to pass |current_time| explicit
Wez 2013/03/19 23:23:14 That would require the caller to pass base::Time::
38
37 private: 39 private:
38 // Helper function to evict old data points. 40 // Removes data points more than |time_window| older than |current_time|.
39 void Evict(base::Time current_time); 41 void EvictOldDataPoints(base::Time current_time);
40 42
41 // A data point consists of a timestamp and a data value. 43 // Returns the current time specified for test, if set, or base::Time::Now().
44 base::Time CurrentTime() const;
45
46 // Type used to store data points with timestamps.
42 typedef std::pair<base::Time, int64> DataPoint; 47 typedef std::pair<base::Time, int64> DataPoint;
alexeypa (please no reviews) 2013/03/18 19:39:39 nit: I think this can be moved down, closer to |da
Wez 2013/03/19 23:23:14 According to the style guide, it actually needs to
43 48
44 // Duration of the time window. 49 // Time window over which to calculate the rate.
45 base::TimeDelta time_window_; 50 base::TimeDelta time_window_;
alexeypa (please no reviews) 2013/03/18 19:39:39 nit: |time_window_| can be const.
Wez 2013/03/19 23:23:14 Done.
46 51
47 // Protects |data_points_| and |sum_|. 52 // Protects |data_points_| and |sum_|.
alexeypa (please no reviews) 2013/03/18 19:39:39 nit: It also protects |time_window_|.
Wez 2013/03/19 23:23:14 Nope. |time_window| is set once, at init time (se
48 base::Lock lock_; 53 base::Lock lock_;
alexeypa (please no reviews) 2013/03/18 19:39:39 nit: How about inheriting this class from base::No
Wez 2013/03/19 23:23:14 I wanted to avoid too much refactoring in this CL,
49 54
50 // Keep the values of all the data points in a queue. 55 // Queue containing data points in the order in which they were recorded.
51 std::queue<DataPoint> data_points_; 56 std::queue<DataPoint> data_points_;
52 57
53 // Sum of values in |data_points_|. 58 // Sum of values in |data_points_|.
54 int64 sum_; 59 int64 sum_;
55 60
61 // If set, used to calculate the running average, in place of Now().
62 base::Time current_time_for_test_;
63
56 DISALLOW_COPY_AND_ASSIGN(RateCounter); 64 DISALLOW_COPY_AND_ASSIGN(RateCounter);
57 }; 65 };
58 66
59 } // namespace remoting 67 } // namespace remoting
60 68
61 #endif // REMOTING_BASE_RATE_COUNTER_H_ 69 #endif // REMOTING_BASE_RATE_COUNTER_H_
OLDNEW
« no previous file with comments | « no previous file | remoting/base/rate_counter.cc » ('j') | remoting/base/rate_counter.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698