Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "remoting/base/rate_counter.h" | |
| 6 | |
| 7 namespace remoting { | |
| 8 | |
| 9 RateCounter::RateCounter(base::TimeDelta time_window) | |
| 10 : time_window_(time_window) { | |
|
simonmorris
2011/04/04 10:27:34
Initialise sum_.
Alpha Left Google
2011/04/05 00:16:06
Done.
| |
| 11 } | |
| 12 | |
| 13 RateCounter::~RateCounter() { | |
| 14 } | |
| 15 | |
| 16 void RateCounter::Record(int64 value) { | |
|
simonmorris
2011/04/04 10:27:34
It might be worth adding a Record(int64, base::Tim
Alpha Left Google
2011/04/05 00:16:06
How is this related to unit testing? I mean this i
| |
| 17 base::Time current_time = base::Time::Now(); | |
| 18 Evict(current_time); | |
| 19 | |
| 20 base::AutoLock auto_lock(lock_); | |
| 21 sum_ += value; | |
| 22 data_points_.push(std::make_pair(current_time, value)); | |
| 23 } | |
| 24 | |
| 25 double RateCounter::Rate() { | |
| 26 Evict(base::Time::Now()); | |
| 27 | |
| 28 base::AutoLock auto_lock(lock_); | |
| 29 return 1.0 * base::Time::kMillisecondsPerSecond * sum_ / | |
|
simonmorris
2011/04/04 10:27:34
Clearer to explicitly cast to a double.
Alpha Left Google
2011/04/05 00:16:06
Done.
| |
| 30 time_window_.InMilliseconds(); | |
|
simonmorris
2011/04/04 10:27:34
What if the first data point was taken more recent
Alpha Left Google
2011/04/05 00:16:06
Done.
| |
| 31 } | |
| 32 | |
| 33 void RateCounter::Evict(base::Time current_time) { | |
| 34 base::AutoLock auto_lock(lock_); | |
| 35 | |
| 36 // Remove data points outside of the window. | |
| 37 base::Time window_start = current_time - time_window_; | |
| 38 | |
| 39 while (!data_points_.empty()) { | |
| 40 if (window_start < data_points_.front().first) | |
|
simonmorris
2011/04/04 10:27:34
Clearer to reverse the order, so the non-const is
Alpha Left Google
2011/04/05 00:16:06
Done.
| |
| 41 break; | |
| 42 | |
| 43 sum_ -= data_points_.front().second; | |
| 44 data_points_.pop(); | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 } // namespace remoting | |
| OLD | NEW |