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 "modules/audio_processing/test/performance_timer.h" 12 13 #include <math.h> 14 15 #include <numeric> 16 17 #include "rtc_base/checks.h" 18 19 namespace webrtc { 20 namespace test { 21 PerformanceTimer(int num_frames_to_process)22PerformanceTimer::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 StartTimer()29void PerformanceTimer::StartTimer() { 30 start_timestamp_us_ = clock_->TimeInMicroseconds(); 31 } 32 StopTimer()33void PerformanceTimer::StopTimer() { 34 RTC_DCHECK(start_timestamp_us_); 35 timestamps_us_.push_back(clock_->TimeInMicroseconds() - *start_timestamp_us_); 36 } 37 GetDurationAverage() const38double PerformanceTimer::GetDurationAverage() const { 39 return GetDurationAverage(0); 40 } 41 GetDurationStandardDeviation() const42double PerformanceTimer::GetDurationStandardDeviation() const { 43 return GetDurationStandardDeviation(0); 44 } 45 GetDurationAverage(size_t number_of_warmup_samples) const46double 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 GetDurationStandardDeviation(size_t number_of_warmup_samples) const57double 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); 64 65 double variance = std::accumulate( 66 timestamps_us_.begin() + number_of_warmup_samples, timestamps_us_.end(), 67 0.0, [average_duration](const double& a, const int64_t& b) { 68 return a + (b - average_duration) * (b - average_duration); 69 }); 70 71 return sqrt(variance / number_of_samples); 72 } 73 74 } // namespace test 75 } // namespace webrtc 76