blob: d49d7cacdd5344f09ff38284e70bb9e32d885476 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
Tim Psiaki63046262015-09-14 10:38:08 -07002 * Copyright 2015 The WebRTC Project Authors. All rights reserved.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef WEBRTC_BASE_RATETRACKER_H_
12#define WEBRTC_BASE_RATETRACKER_H_
13
14#include <stdlib.h>
15#include "webrtc/base/basictypes.h"
16
17namespace rtc {
18
Tim Psiaki63046262015-09-14 10:38:08 -070019// Computes units per second over a given interval by tracking the units over
20// each bucket of a given size and calculating the instantaneous rate assuming
21// that over each bucket the rate was constant.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022class RateTracker {
23 public:
Peter Boström0c4e06b2015-10-07 12:23:21 +020024 RateTracker(uint32_t bucket_milliseconds, size_t bucket_count);
Tim Psiaki63046262015-09-14 10:38:08 -070025 virtual ~RateTracker();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000026
Tim Psiaki63046262015-09-14 10:38:08 -070027 // Computes the average rate over the most recent interval_milliseconds,
28 // or if the first sample was added within this period, computes the rate
29 // since the first sample was added.
Peter Boström0c4e06b2015-10-07 12:23:21 +020030 double ComputeRateForInterval(uint32_t interval_milliseconds) const;
Tim Psiaki63046262015-09-14 10:38:08 -070031
32 // Computes the average rate over the rate tracker's recording interval
33 // of bucket_milliseconds * bucket_count.
34 double ComputeRate() const {
Peter Boström0c4e06b2015-10-07 12:23:21 +020035 return ComputeRateForInterval(bucket_milliseconds_ *
36 static_cast<uint32_t>(bucket_count_));
Tim Psiaki63046262015-09-14 10:38:08 -070037 }
38
39 // Computes the average rate since the first sample was added to the
40 // rate tracker.
41 double ComputeTotalRate() const;
42
43 // The total number of samples added.
44 size_t TotalSampleCount() const;
45
46 // Reads the current time in order to determine the appropriate bucket for
47 // these samples, and increments the count for that bucket by sample_count.
48 void AddSamples(size_t sample_count);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000049
50 protected:
51 // overrideable for tests
Peter Boström0c4e06b2015-10-07 12:23:21 +020052 virtual uint32_t Time() const;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000053
54 private:
Tim Psiaki63046262015-09-14 10:38:08 -070055 void EnsureInitialized();
56 size_t NextBucketIndex(size_t bucket_index) const;
57
Peter Boström0c4e06b2015-10-07 12:23:21 +020058 const uint32_t bucket_milliseconds_;
Tim Psiaki63046262015-09-14 10:38:08 -070059 const size_t bucket_count_;
60 size_t* sample_buckets_;
61 size_t total_sample_count_;
62 size_t current_bucket_;
Peter Boström0c4e06b2015-10-07 12:23:21 +020063 uint32_t bucket_start_time_milliseconds_;
64 uint32_t initialization_time_milliseconds_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000065};
66
67} // namespace rtc
68
69#endif // WEBRTC_BASE_RATETRACKER_H_