ivoc | 3cfb3ef | 2016-11-24 04:17:28 -0800 | [diff] [blame] | 1 | /* |
| 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 Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 17 | #include "webrtc/rtc_base/checks.h" |
ivoc | 3cfb3ef | 2016-11-24 04:17:28 -0800 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | namespace test { |
| 21 | |
| 22 | PerformanceTimer::PerformanceTimer(int num_frames_to_process) |
| 23 | : clock_(webrtc::Clock::GetRealTimeClock()) { |
| 24 | timestamps_us_.reserve(num_frames_to_process); |
| 25 | } |
| 26 | |
| 27 | PerformanceTimer::~PerformanceTimer() = default; |
| 28 | |
| 29 | void PerformanceTimer::StartTimer() { |
| 30 | start_timestamp_us_ = rtc::Optional<int64_t>(clock_->TimeInMicroseconds()); |
| 31 | } |
| 32 | |
| 33 | void PerformanceTimer::StopTimer() { |
| 34 | RTC_DCHECK(start_timestamp_us_); |
| 35 | timestamps_us_.push_back(clock_->TimeInMicroseconds() - *start_timestamp_us_); |
| 36 | } |
| 37 | |
| 38 | double PerformanceTimer::GetDurationAverage() const { |
ivoc | 1973ba6 | 2017-03-20 03:03:16 -0700 | [diff] [blame] | 39 | return GetDurationAverage(0); |
ivoc | 3cfb3ef | 2016-11-24 04:17:28 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | double PerformanceTimer::GetDurationStandardDeviation() const { |
ivoc | 1973ba6 | 2017-03-20 03:03:16 -0700 | [diff] [blame] | 43 | return GetDurationStandardDeviation(0); |
| 44 | } |
| 45 | |
| 46 | double 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 | |
| 57 | double 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); |
ivoc | 3cfb3ef | 2016-11-24 04:17:28 -0800 | [diff] [blame] | 64 | |
| 65 | double variance = std::accumulate( |
ivoc | 1973ba6 | 2017-03-20 03:03:16 -0700 | [diff] [blame] | 66 | timestamps_us_.begin() + number_of_warmup_samples, timestamps_us_.end(), |
| 67 | 0.0, [average_duration](const double& a, const int64_t& b) { |
ivoc | 3cfb3ef | 2016-11-24 04:17:28 -0800 | [diff] [blame] | 68 | return a + (b - average_duration) * (b - average_duration); |
| 69 | }); |
| 70 | |
ivoc | 1973ba6 | 2017-03-20 03:03:16 -0700 | [diff] [blame] | 71 | return sqrt(variance / number_of_samples); |
ivoc | 3cfb3ef | 2016-11-24 04:17:28 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | } // namespace test |
| 75 | } // namespace webrtc |