1// Copyright 2013 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#ifndef CONTENT_BROWSER_DOWNLOAD_RATE_ESTIMATOR_H_
6#define CONTENT_BROWSER_DOWNLOAD_RATE_ESTIMATOR_H_
7
8#include <string>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/time/time.h"
13#include "content/common/content_export.h"
14
15namespace content {
16
17// RateEstimator generates rate estimates based on recent activity.
18//
19// Internally it uses a fixed-size ring buffer, and develops estimates
20// based on a small sliding window of activity.
21class CONTENT_EXPORT RateEstimator {
22 public:
23  RateEstimator();
24  RateEstimator(base::TimeDelta bucket_time,
25                size_t num_buckets,
26                base::TimeTicks now);
27  ~RateEstimator();
28
29  // Increment the counter by |count|. The first variant uses the current time,
30  // the second variant provides the time that |count| is observed.
31  void Increment(uint32 count);
32  void Increment(uint32 count, base::TimeTicks now);
33
34  // Get a rate estimate, in terms of counts/second. The first variant uses the
35  // current time, the second variant provides the time.
36  uint64 GetCountPerSecond() const;
37  uint64 GetCountPerSecond(base::TimeTicks now) const;
38
39 private:
40  void ClearOldBuckets(base::TimeTicks now);
41  void ResetBuckets(base::TimeTicks now);
42
43  std::vector<uint32> history_;
44  base::TimeDelta bucket_time_;
45  size_t oldest_index_;
46  size_t bucket_count_;
47  base::TimeTicks oldest_time_;
48};
49
50}  // namespace content
51
52#endif  // CONTENT_BROWSER_DOWNLOAD_RATE_ESTIMATOR_H_
53