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 #include "remoting/base/rate_counter.h" | 5 #include "remoting/base/rate_counter.h" |
6 | 6 |
7 #include "base/logging.h" | |
alexeypa (please no reviews)
2013/03/18 19:39:39
Is this header actually used?
Wez
2013/03/19 23:23:14
No; used it during development. It's needed for th
| |
8 | |
7 namespace remoting { | 9 namespace remoting { |
8 | 10 |
9 RateCounter::RateCounter(base::TimeDelta time_window) | 11 RateCounter::RateCounter(base::TimeDelta time_window) |
10 : time_window_(time_window), | 12 : time_window_(time_window), |
11 sum_(0) { | 13 sum_(0) { |
12 } | 14 } |
alexeypa (please no reviews)
2013/03/18 19:39:39
nit: Add CHECK(time_window_.InMilliseconds() > 0)
Wez
2013/03/19 23:23:14
Done.
| |
13 | 15 |
14 RateCounter::~RateCounter() { | 16 RateCounter::~RateCounter() { |
15 } | 17 } |
16 | 18 |
17 void RateCounter::Record(int64 value) { | 19 void RateCounter::Record(int64 value) { |
18 base::Time current_time = base::Time::Now(); | 20 base::Time current_time = CurrentTime(); |
19 Evict(current_time); | |
20 | 21 |
21 base::AutoLock auto_lock(lock_); | 22 base::AutoLock auto_lock(lock_); |
23 EvictOldDataPoints(current_time); | |
22 sum_ += value; | 24 sum_ += value; |
23 data_points_.push(std::make_pair(current_time, value)); | 25 data_points_.push(std::make_pair(current_time, value)); |
24 } | 26 } |
25 | 27 |
26 double RateCounter::Rate() { | 28 double RateCounter::Rate() { |
27 Evict(base::Time::Now()); | 29 base::Time current_time = CurrentTime(); |
alexeypa (please no reviews)
2013/03/18 19:39:39
Should CurrentTime() be called under the lock?
Wez
2013/03/19 23:23:14
No, I don't think so. This way it captures the ti
| |
28 | 30 |
29 base::AutoLock auto_lock(lock_); | 31 base::AutoLock auto_lock(lock_); |
32 EvictOldDataPoints(current_time); | |
30 return static_cast<double>(base::Time::kMillisecondsPerSecond) * sum_ / | 33 return static_cast<double>(base::Time::kMillisecondsPerSecond) * sum_ / |
alexeypa (please no reviews)
2013/03/18 19:39:39
nit: Since it is converting to double anyway it wi
Wez
2013/03/19 23:23:14
Done.
| |
31 time_window_.InMilliseconds(); | 34 time_window_.InMilliseconds(); |
32 } | 35 } |
33 | 36 |
34 void RateCounter::Evict(base::Time current_time) { | 37 void RateCounter::SetCurrentTimeForTest(base::Time current_time) { |
35 base::AutoLock auto_lock(lock_); | 38 current_time_for_test_ = current_time; |
39 } | |
40 | |
41 void RateCounter::EvictOldDataPoints(base::Time current_time) { | |
42 lock_.AssertAcquired(); | |
36 | 43 |
37 // Remove data points outside of the window. | 44 // Remove data points outside of the window. |
38 base::Time window_start = current_time - time_window_; | 45 base::Time window_start = current_time - time_window_; |
39 | 46 |
40 while (!data_points_.empty()) { | 47 while (!data_points_.empty()) { |
41 if (data_points_.front().first > window_start) | 48 if (data_points_.front().first > window_start) |
42 break; | 49 break; |
43 | 50 |
44 sum_ -= data_points_.front().second; | 51 sum_ -= data_points_.front().second; |
45 data_points_.pop(); | 52 data_points_.pop(); |
46 } | 53 } |
47 } | 54 } |
48 | 55 |
56 base::Time RateCounter::CurrentTime() const { | |
57 if (current_time_for_test_ == base::Time()) | |
58 return base::Time::Now(); | |
59 return current_time_for_test_; | |
60 } | |
61 | |
49 } // namespace remoting | 62 } // namespace remoting |
OLD | NEW |