blob: 1e9c68ccb8f1021c5f0954b84d7a89f3316aacf8 [file] [log] [blame]
ivoc3cfb3ef2016-11-24 04:17:28 -08001/*
2 * Copyright (c) 2016 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
11#include "webrtc/modules/audio_processing/test/performance_timer.h"
12
13#include <math.h>
14
15#include <numeric>
16
Edward Lemurc20978e2017-07-06 19:44:34 +020017#include "webrtc/rtc_base/checks.h"
ivoc3cfb3ef2016-11-24 04:17:28 -080018
19namespace webrtc {
20namespace test {
21
22PerformanceTimer::PerformanceTimer(int num_frames_to_process)
23 : clock_(webrtc::Clock::GetRealTimeClock()) {
24 timestamps_us_.reserve(num_frames_to_process);
25}
26
27PerformanceTimer::~PerformanceTimer() = default;
28
29void PerformanceTimer::StartTimer() {
30 start_timestamp_us_ = rtc::Optional<int64_t>(clock_->TimeInMicroseconds());
31}
32
33void PerformanceTimer::StopTimer() {
34 RTC_DCHECK(start_timestamp_us_);
35 timestamps_us_.push_back(clock_->TimeInMicroseconds() - *start_timestamp_us_);
36}
37
38double PerformanceTimer::GetDurationAverage() const {
ivoc1973ba62017-03-20 03:03:16 -070039 return GetDurationAverage(0);
ivoc3cfb3ef2016-11-24 04:17:28 -080040}
41
42double PerformanceTimer::GetDurationStandardDeviation() const {
ivoc1973ba62017-03-20 03:03:16 -070043 return GetDurationStandardDeviation(0);
44}
45
46double PerformanceTimer::GetDurationAverage(
47 size_t number_of_warmup_samples) const {
48 RTC_DCHECK_GT(timestamps_us_.size(), number_of_warmup_samples);
49 const size_t number_of_samples =
50 timestamps_us_.size() - number_of_warmup_samples;
51 return static_cast<double>(
52 std::accumulate(timestamps_us_.begin() + number_of_warmup_samples,
53 timestamps_us_.end(), static_cast<int64_t>(0))) /
54 number_of_samples;
55}
56
57double PerformanceTimer::GetDurationStandardDeviation(
58 size_t number_of_warmup_samples) const {
59 RTC_DCHECK_GT(timestamps_us_.size(), number_of_warmup_samples);
60 const size_t number_of_samples =
61 timestamps_us_.size() - number_of_warmup_samples;
62 RTC_DCHECK_GT(number_of_samples, 0);
63 double average_duration = GetDurationAverage(number_of_warmup_samples);
ivoc3cfb3ef2016-11-24 04:17:28 -080064
65 double variance = std::accumulate(
ivoc1973ba62017-03-20 03:03:16 -070066 timestamps_us_.begin() + number_of_warmup_samples, timestamps_us_.end(),
67 0.0, [average_duration](const double& a, const int64_t& b) {
ivoc3cfb3ef2016-11-24 04:17:28 -080068 return a + (b - average_duration) * (b - average_duration);
69 });
70
ivoc1973ba62017-03-20 03:03:16 -070071 return sqrt(variance / number_of_samples);
ivoc3cfb3ef2016-11-24 04:17:28 -080072}
73
74} // namespace test
75} // namespace webrtc