blob: 3ff66cd10c8c13a7ecadc9e75aab710b1123d60f [file] [log] [blame]
Sebastian Jansson9a2ca0a2019-04-15 13:18:19 +02001/*
2 * Copyright 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 "test/scenario/performance_stats.h"
11
12#include <algorithm>
13
14namespace webrtc {
15namespace test {
16void EventRateCounter::AddEvent(Timestamp event_time) {
17 if (first_time_.IsInfinite()) {
18 first_time_ = event_time;
19 } else {
20 RTC_DCHECK(event_time >= last_time_);
21 interval_.AddSample(event_time - last_time_);
22 }
23 last_time_ = event_time;
24 event_count_++;
25}
26
27void EventRateCounter::AddEvents(EventRateCounter other) {
28 first_time_ = std::min(first_time_, other.first_time_);
29 last_time_ = std::max(last_time_, other.last_time_);
30 event_count_ += other.event_count_;
31 interval_.AddSamples(other.interval_);
32}
33
34bool EventRateCounter::IsEmpty() const {
35 return first_time_ == last_time_;
36}
37
38double EventRateCounter::Rate() const {
39 if (event_count_ == 0)
40 return 0;
41 if (event_count_ == 1)
42 return NAN;
43 return (event_count_ - 1) / (last_time_ - first_time_).seconds<double>();
44}
45
46double SampleStats<double>::Max() {
47 if (IsEmpty())
48 return INFINITY;
49 return GetMax();
50}
51
52double SampleStats<double>::Mean() {
53 if (IsEmpty())
54 return 0;
55 return GetAverage();
56}
57
58double SampleStats<double>::Median() {
59 return Quantile(0.5);
60}
61
62double SampleStats<double>::Quantile(double quantile) {
63 if (IsEmpty())
64 return 0;
65 return GetPercentile(quantile);
66}
67
68double SampleStats<double>::Min() {
69 if (IsEmpty())
70 return -INFINITY;
71 return GetMin();
72}
73
74double SampleStats<double>::Variance() {
75 if (IsEmpty())
76 return 0;
77 return GetVariance();
78}
79
80double SampleStats<double>::StandardDeviation() {
81 return sqrt(Variance());
82}
83
84void SampleStats<TimeDelta>::AddSample(TimeDelta delta) {
85 RTC_DCHECK(delta.IsFinite());
86 stats_.AddSample(delta.seconds<double>());
87}
88
89void SampleStats<TimeDelta>::AddSampleMs(double delta_ms) {
90 AddSample(TimeDelta::ms(delta_ms));
91}
92void SampleStats<TimeDelta>::AddSamples(const SampleStats<TimeDelta>& other) {
93 stats_.AddSamples(other.stats_);
94}
95
96TimeDelta SampleStats<TimeDelta>::Max() {
97 return TimeDelta::seconds(stats_.Max());
98}
99
100TimeDelta SampleStats<TimeDelta>::Mean() {
101 return TimeDelta::seconds(stats_.Mean());
102}
103
104TimeDelta SampleStats<TimeDelta>::Median() {
105 return Quantile(0.5);
106}
107
108TimeDelta SampleStats<TimeDelta>::Quantile(double quantile) {
109 return TimeDelta::seconds(stats_.Quantile(quantile));
110}
111
112TimeDelta SampleStats<TimeDelta>::Min() {
113 return TimeDelta::seconds(stats_.Min());
114}
115
116TimeDelta SampleStats<TimeDelta>::Variance() {
117 return TimeDelta::seconds(stats_.Variance());
118}
119
120TimeDelta SampleStats<TimeDelta>::StandardDeviation() {
121 return TimeDelta::seconds(stats_.StandardDeviation());
122}
123
124void SampleStats<DataRate>::AddSample(DataRate sample) {
125 stats_.AddSample(sample.bps<double>());
126}
127
128void SampleStats<DataRate>::AddSampleBps(double rate_bps) {
129 stats_.AddSample(rate_bps);
130}
131
132void SampleStats<DataRate>::AddSamples(const SampleStats<DataRate>& other) {
133 stats_.AddSamples(other.stats_);
134}
135
136DataRate SampleStats<DataRate>::Max() {
137 return DataRate::bps(stats_.Max());
138}
139
140DataRate SampleStats<DataRate>::Mean() {
141 return DataRate::bps(stats_.Mean());
142}
143
144DataRate SampleStats<DataRate>::Median() {
145 return Quantile(0.5);
146}
147
148DataRate SampleStats<DataRate>::Quantile(double quantile) {
149 return DataRate::bps(stats_.Quantile(quantile));
150}
151
152DataRate SampleStats<DataRate>::Min() {
153 return DataRate::bps(stats_.Min());
154}
155
156DataRate SampleStats<DataRate>::Variance() {
157 return DataRate::bps(stats_.Variance());
158}
159
160DataRate SampleStats<DataRate>::StandardDeviation() {
161 return DataRate::bps(stats_.StandardDeviation());
162}
163
164void VideoFramesStats::AddFrameInfo(const VideoFrameBuffer& frame,
165 Timestamp at_time) {
166 ++count;
167 RTC_DCHECK(at_time.IsFinite());
168 pixels.AddSample(frame.width() * frame.height());
169 resolution.AddSample(std::max(frame.width(), frame.height()));
170 frames.AddEvent(at_time);
171}
172
173void VideoFramesStats::AddStats(const VideoFramesStats& other) {
174 count += other.count;
175 pixels.AddSamples(other.pixels);
176 resolution.AddSamples(other.resolution);
177 frames.AddEvents(other.frames);
178}
179
180void VideoQualityStats::AddStats(const VideoQualityStats& other) {
181 capture.AddStats(other.capture);
182 render.AddStats(other.render);
183 lost_count += other.lost_count;
184 freeze_count += other.freeze_count;
185 end_to_end_delay.AddSamples(other.end_to_end_delay);
186 psnr.AddSamples(other.psnr);
187 skipped_between_rendered.AddSamples(other.skipped_between_rendered);
188 freeze_duration.AddSamples(other.freeze_duration);
189 time_between_freezes.AddSamples(other.time_between_freezes);
190}
191
192} // namespace test
193} // namespace webrtc