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

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

Issue 2561173004: Cleanup RateCounter (Closed)
Patch Set: Created 4 years 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
« no previous file with comments | « remoting/base/rate_counter.h ('k') | remoting/base/rate_counter_unittest.cc » ('j') | no next file with comments »
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 #include "remoting/base/rate_counter.h" 5 #include "remoting/base/rate_counter.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 8
9 namespace remoting { 9 namespace remoting {
10 10
11 RateCounter::RateCounter(base::TimeDelta time_window) 11 RateCounter::RateCounter(base::TimeDelta time_window)
12 : time_window_(time_window), 12 : time_window_(time_window), sum_(0) {
13 sum_(0) { 13 DCHECK_GT(time_window, base::TimeDelta());
14 DCHECK_GT(time_window.InMilliseconds(), 0);
15 } 14 }
16 15
17 RateCounter::~RateCounter() { 16 RateCounter::~RateCounter() {}
18 }
19 17
20 void RateCounter::Record(int64_t value) { 18 void RateCounter::Record(int64_t value) {
21 DCHECK(CalledOnValidThread()); 19 DCHECK(CalledOnValidThread());
22 20
23 base::Time current_time = CurrentTime(); 21 base::TimeTicks now = tick_clock_->NowTicks();
24 EvictOldDataPoints(current_time); 22 EvictOldDataPoints(now);
25 sum_ += value; 23 sum_ += value;
26 data_points_.push(std::make_pair(current_time, value)); 24 data_points_.push(std::make_pair(now, value));
27 } 25 }
28 26
29 double RateCounter::Rate() { 27 double RateCounter::Rate() {
30 DCHECK(CalledOnValidThread()); 28 DCHECK(CalledOnValidThread());
31 29
32 EvictOldDataPoints(CurrentTime()); 30 EvictOldDataPoints(tick_clock_->NowTicks());
33 return sum_ / time_window_.InSecondsF(); 31 return sum_ / time_window_.InSecondsF();
34 } 32 }
35 33
36 void RateCounter::SetCurrentTimeForTest(base::Time current_time) { 34 void RateCounter::EvictOldDataPoints(base::TimeTicks now) {
37 DCHECK(CalledOnValidThread());
38 DCHECK(current_time >= current_time_for_test_);
39
40 current_time_for_test_ = current_time;
41 }
42
43 void RateCounter::EvictOldDataPoints(base::Time current_time) {
44 // Remove data points outside of the window. 35 // Remove data points outside of the window.
45 base::Time window_start = current_time - time_window_; 36 base::TimeTicks window_start = now - time_window_;
46 37
47 while (!data_points_.empty()) { 38 while (!data_points_.empty()) {
48 if (data_points_.front().first > window_start) 39 if (data_points_.front().first > window_start)
49 break; 40 break;
50 41
51 sum_ -= data_points_.front().second; 42 sum_ -= data_points_.front().second;
52 data_points_.pop(); 43 data_points_.pop();
53 } 44 }
54 } 45 }
55 46
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
62 } // namespace remoting 47 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/base/rate_counter.h ('k') | remoting/base/rate_counter_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698