| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/running_average.h" | 5 #include "remoting/base/running_samples.h" |
| 6 |
| 7 #include <algorithm> |
| 6 | 8 |
| 7 #include "base/logging.h" | 9 #include "base/logging.h" |
| 8 | 10 |
| 9 namespace remoting { | 11 namespace remoting { |
| 10 | 12 |
| 11 RunningAverage::RunningAverage(int window_size) | 13 RunningSamples::RunningSamples(int window_size) |
| 12 : window_size_(window_size), | 14 : window_size_(window_size) { |
| 13 sum_(0) { | |
| 14 DCHECK_GT(window_size, 0); | 15 DCHECK_GT(window_size, 0); |
| 15 } | 16 } |
| 16 | 17 |
| 17 RunningAverage::~RunningAverage() {} | 18 RunningSamples::~RunningSamples() {} |
| 18 | 19 |
| 19 void RunningAverage::Record(int64_t value) { | 20 void RunningSamples::Record(int64_t value) { |
| 20 DCHECK(thread_checker_.CalledOnValidThread()); | 21 DCHECK(thread_checker_.CalledOnValidThread()); |
| 21 | 22 |
| 22 data_points_.push_back(value); | 23 data_points_.push_back(value); |
| 23 sum_ += value; | 24 sum_ += value; |
| 24 | 25 |
| 25 if (data_points_.size() > window_size_) { | 26 if (data_points_.size() > window_size_) { |
| 26 sum_ -= data_points_[0]; | 27 sum_ -= data_points_[0]; |
| 27 data_points_.pop_front(); | 28 data_points_.pop_front(); |
| 28 } | 29 } |
| 29 } | 30 } |
| 30 | 31 |
| 31 double RunningAverage::Average() { | 32 double RunningSamples::Average() const { |
| 32 DCHECK(thread_checker_.CalledOnValidThread()); | 33 DCHECK(thread_checker_.CalledOnValidThread()); |
| 33 | 34 |
| 34 if (data_points_.empty()) | 35 if (data_points_.empty()) |
| 35 return 0; | 36 return 0; |
| 36 return static_cast<double>(sum_) / data_points_.size(); | 37 return static_cast<double>(sum_) / data_points_.size(); |
| 37 } | 38 } |
| 38 | 39 |
| 40 int64_t RunningSamples::Max() const { |
| 41 DCHECK(thread_checker_.CalledOnValidThread()); |
| 42 |
| 43 if (data_points_.empty()) |
| 44 return 0; |
| 45 |
| 46 return *std::max_element(data_points_.begin(), data_points_.end()); |
| 47 } |
| 48 |
| 39 } // namespace remoting | 49 } // namespace remoting |
| OLD | NEW |