Roll src/third_party/WebKit 06cb9e9:a978ee5 (svn 202558:202559)
[chromium-blink-merge.git] / remoting / base / running_average.cc
blob5e24c0de3a95672c1e4f8d4c5b67cf5b11db81ba
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.
5 #include "remoting/base/running_average.h"
7 #include "base/logging.h"
9 namespace remoting {
11 RunningAverage::RunningAverage(int window_size)
12 : window_size_(window_size),
13 sum_(0) {
14 DCHECK_GT(window_size, 0);
17 RunningAverage::~RunningAverage() {}
19 void RunningAverage::Record(int64_t value) {
20 DCHECK(thread_checker_.CalledOnValidThread());
22 data_points_.push_back(value);
23 sum_ += value;
25 if (data_points_.size() > window_size_) {
26 sum_ -= data_points_[0];
27 data_points_.pop_front();
31 double RunningAverage::Average() {
32 DCHECK(thread_checker_.CalledOnValidThread());
34 if (data_points_.empty())
35 return 0;
36 return static_cast<double>(sum_) / data_points_.size();
39 } // namespace remoting