Home | History | Annotate | Download | only in base
      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.
      4 
      5 #include "remoting/base/running_average.h"
      6 
      7 #include "base/logging.h"
      8 
      9 namespace remoting {
     10 
     11 RunningAverage::RunningAverage(int window_size)
     12     : window_size_(window_size),
     13       sum_(0) {
     14   DCHECK_GT(window_size, 0);
     15 }
     16 
     17 RunningAverage::~RunningAverage() {
     18 }
     19 
     20 void RunningAverage::Record(int64 value) {
     21   base::AutoLock auto_lock(lock_);
     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 RunningAverage::Average() {
     33   base::AutoLock auto_lock(lock_);
     34 
     35   if (data_points_.empty())
     36     return 0;
     37   return static_cast<double>(sum_) / data_points_.size();
     38 }
     39 
     40 }  // namespace remoting
     41