| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "remoting/base/running_samples.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 | |
| 11 namespace remoting { | |
| 12 | |
| 13 RunningSamples::RunningSamples(int window_size) | |
| 14 : window_size_(window_size) { | |
| 15 DCHECK_GT(window_size, 0); | |
| 16 } | |
| 17 | |
| 18 RunningSamples::~RunningSamples() {} | |
| 19 | |
| 20 void RunningSamples::Record(int64_t value) { | |
| 21 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 22 | |
| 23 data_points_.push_back(value); | |
| 24 sum_ += value; | |
| 25 | |
| 26 if (data_points_.size() > window_size_) { | |
| 27 sum_ -= data_points_[0]; | |
| 28 data_points_.pop_front(); | |
| 29 } | |
| 30 } | |
| 31 | |
| 32 double RunningSamples::Average() const { | |
| 33 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 34 | |
| 35 if (data_points_.empty()) | |
| 36 return 0; | |
| 37 return static_cast<double>(sum_) / data_points_.size(); | |
| 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 | |
| 49 } // namespace remoting | |
| OLD | NEW |