summaryrefslogtreecommitdiffstats
path: root/remoting/base/rate_counter.h
blob: c427b9603f8c6041dcd40ad8bc1efcd2f23f08b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// 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.

// RateCounter is defined to measure average rate over a given time window.
// Rate is reported as the sum of values recorded divided by the time window.
// This can be used for measuring bandwidth, bitrate, etc.

// This class is thread-safe.

#ifndef REMOTING_BASE_RATE_COUNTER_H_
#define REMOTING_BASE_RATE_COUNTER_H_

#include <queue>
#include <utility>

#include "base/basictypes.h"
#include "base/synchronization/lock.h"
#include "base/time.h"

namespace remoting {

class RateCounter {
 public:
  // Construct a counter for a specific time window.
  RateCounter(base::TimeDelta time_window);

  virtual ~RateCounter();

  // Record the data point.
  void Record(int64 value);

  // Report the rate recorded. At the beginning of recording the numbers before
  // |time_window| is reached the reported rate will not be accurate.
  double Rate();

 private:
  // Helper function to evict old data points.
  void Evict(base::Time current_time);

  // A data point consists of a timestamp and a data value.
  typedef std::pair<base::Time, int64> DataPoint;

  // Duration of the time window.
  base::TimeDelta time_window_;

  // Protects |data_points_| and |sum_|.
  base::Lock lock_;

  // Keep the values of all the data points in a queue.
  std::queue<DataPoint> data_points_;

  // Sum of values in |data_points_|.
  int64 sum_;

  DISALLOW_COPY_AND_ASSIGN(RateCounter);
};

}  // namespace remoting

#endif  // REMOTING_BASE_RATE_COUNTER_H_