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" |
| 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) { |
| 14 DCHECK_GT(time_window.InMilliseconds(), 0); |
12 } | 15 } |
13 | 16 |
14 RateCounter::~RateCounter() { | 17 RateCounter::~RateCounter() { |
15 } | 18 } |
16 | 19 |
17 void RateCounter::Record(int64 value) { | 20 void RateCounter::Record(int64 value) { |
18 base::Time current_time = base::Time::Now(); | 21 DCHECK(CalledOnValidThread()); |
19 Evict(current_time); | |
20 | 22 |
21 base::AutoLock auto_lock(lock_); | 23 base::Time current_time = CurrentTime(); |
| 24 EvictOldDataPoints(current_time); |
22 sum_ += value; | 25 sum_ += value; |
23 data_points_.push(std::make_pair(current_time, value)); | 26 data_points_.push(std::make_pair(current_time, value)); |
24 } | 27 } |
25 | 28 |
26 double RateCounter::Rate() { | 29 double RateCounter::Rate() { |
27 Evict(base::Time::Now()); | 30 DCHECK(CalledOnValidThread()); |
28 | 31 |
29 base::AutoLock auto_lock(lock_); | 32 EvictOldDataPoints(CurrentTime()); |
30 return static_cast<double>(base::Time::kMillisecondsPerSecond) * sum_ / | 33 return sum_ / time_window_.InSecondsF(); |
31 time_window_.InMilliseconds(); | |
32 } | 34 } |
33 | 35 |
34 void RateCounter::Evict(base::Time current_time) { | 36 void RateCounter::SetCurrentTimeForTest(base::Time current_time) { |
35 base::AutoLock auto_lock(lock_); | 37 DCHECK(CalledOnValidThread()); |
| 38 DCHECK(current_time >= current_time_for_test_); |
36 | 39 |
| 40 current_time_for_test_ = current_time; |
| 41 } |
| 42 |
| 43 void RateCounter::EvictOldDataPoints(base::Time current_time) { |
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 |