running_average.cc 870 Bytes
Newer Older
1 2 3 4 5 6
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/base/running_average.h"

7 8
#include "base/logging.h"

9 10 11 12 13
namespace remoting {

RunningAverage::RunningAverage(int window_size)
    : window_size_(window_size),
      sum_(0) {
14
  DCHECK_GT(window_size, 0);
15 16 17 18 19 20
}

RunningAverage::~RunningAverage() {
}

void RunningAverage::Record(int64 value) {
21
  base::AutoLock auto_lock(lock_);
22 23 24 25 26 27 28 29 30 31

  data_points_.push_back(value);
  sum_ += value;

  if (data_points_.size() > window_size_) {
    sum_ -= data_points_[0];
    data_points_.pop_front();
  }
}

32 33
double RunningAverage::Average() {
  base::AutoLock auto_lock(lock_);
34 35 36 37 38 39 40

  if (data_points_.empty())
    return 0;
  return static_cast<double>(sum_) / data_points_.size();
}

}  // namespace remoting