1 /* 2 * Copyright (c) 2019 The WebRTC project authors. All Rights Reserved. 3 * 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 #include "rtc_base/numerics/sample_stats.h" 11 12 namespace webrtc { 13 Max()14double SampleStats<double>::Max() { 15 if (IsEmpty()) 16 return INFINITY; 17 return GetMax(); 18 } 19 Mean()20double SampleStats<double>::Mean() { 21 if (IsEmpty()) 22 return 0; 23 return GetAverage(); 24 } 25 Median()26double SampleStats<double>::Median() { 27 return Quantile(0.5); 28 } 29 Quantile(double quantile)30double SampleStats<double>::Quantile(double quantile) { 31 if (IsEmpty()) 32 return 0; 33 return GetPercentile(quantile); 34 } 35 Min()36double SampleStats<double>::Min() { 37 if (IsEmpty()) 38 return -INFINITY; 39 return GetMin(); 40 } 41 Variance()42double SampleStats<double>::Variance() { 43 if (IsEmpty()) 44 return 0; 45 return GetVariance(); 46 } 47 StandardDeviation()48double SampleStats<double>::StandardDeviation() { 49 return sqrt(Variance()); 50 } 51 Count()52int SampleStats<double>::Count() { 53 return static_cast<int>(GetSamples().size()); 54 } 55 AddSample(TimeDelta delta)56void SampleStats<TimeDelta>::AddSample(TimeDelta delta) { 57 RTC_DCHECK(delta.IsFinite()); 58 stats_.AddSample(delta.seconds<double>()); 59 } 60 AddSampleMs(double delta_ms)61void SampleStats<TimeDelta>::AddSampleMs(double delta_ms) { 62 AddSample(TimeDelta::Millis(delta_ms)); 63 } AddSamples(const SampleStats<TimeDelta> & other)64void SampleStats<TimeDelta>::AddSamples(const SampleStats<TimeDelta>& other) { 65 stats_.AddSamples(other.stats_); 66 } 67 IsEmpty()68bool SampleStats<TimeDelta>::IsEmpty() { 69 return stats_.IsEmpty(); 70 } 71 Max()72TimeDelta SampleStats<TimeDelta>::Max() { 73 return TimeDelta::Seconds(stats_.Max()); 74 } 75 Mean()76TimeDelta SampleStats<TimeDelta>::Mean() { 77 return TimeDelta::Seconds(stats_.Mean()); 78 } 79 Median()80TimeDelta SampleStats<TimeDelta>::Median() { 81 return Quantile(0.5); 82 } 83 Quantile(double quantile)84TimeDelta SampleStats<TimeDelta>::Quantile(double quantile) { 85 return TimeDelta::Seconds(stats_.Quantile(quantile)); 86 } 87 Min()88TimeDelta SampleStats<TimeDelta>::Min() { 89 return TimeDelta::Seconds(stats_.Min()); 90 } 91 Variance()92TimeDelta SampleStats<TimeDelta>::Variance() { 93 return TimeDelta::Seconds(stats_.Variance()); 94 } 95 StandardDeviation()96TimeDelta SampleStats<TimeDelta>::StandardDeviation() { 97 return TimeDelta::Seconds(stats_.StandardDeviation()); 98 } 99 Count()100int SampleStats<TimeDelta>::Count() { 101 return stats_.Count(); 102 } 103 AddSample(DataRate sample)104void SampleStats<DataRate>::AddSample(DataRate sample) { 105 stats_.AddSample(sample.bps<double>()); 106 } 107 AddSampleBps(double rate_bps)108void SampleStats<DataRate>::AddSampleBps(double rate_bps) { 109 stats_.AddSample(rate_bps); 110 } 111 AddSamples(const SampleStats<DataRate> & other)112void SampleStats<DataRate>::AddSamples(const SampleStats<DataRate>& other) { 113 stats_.AddSamples(other.stats_); 114 } 115 IsEmpty()116bool SampleStats<DataRate>::IsEmpty() { 117 return stats_.IsEmpty(); 118 } 119 Max()120DataRate SampleStats<DataRate>::Max() { 121 return DataRate::BitsPerSec(stats_.Max()); 122 } 123 Mean()124DataRate SampleStats<DataRate>::Mean() { 125 return DataRate::BitsPerSec(stats_.Mean()); 126 } 127 Median()128DataRate SampleStats<DataRate>::Median() { 129 return Quantile(0.5); 130 } 131 Quantile(double quantile)132DataRate SampleStats<DataRate>::Quantile(double quantile) { 133 return DataRate::BitsPerSec(stats_.Quantile(quantile)); 134 } 135 Min()136DataRate SampleStats<DataRate>::Min() { 137 return DataRate::BitsPerSec(stats_.Min()); 138 } 139 Variance()140DataRate SampleStats<DataRate>::Variance() { 141 return DataRate::BitsPerSec(stats_.Variance()); 142 } 143 StandardDeviation()144DataRate SampleStats<DataRate>::StandardDeviation() { 145 return DataRate::BitsPerSec(stats_.StandardDeviation()); 146 } 147 Count()148int SampleStats<DataRate>::Count() { 149 return stats_.Count(); 150 } 151 152 } // namespace webrtc 153