api_call_statistics.cc (3291B)
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 11 #include "modules/audio_processing/test/api_call_statistics.h" 12 13 #include <algorithm> 14 #include <cstdint> 15 #include <fstream> 16 #include <iostream> 17 #include <limits> 18 #include <memory> 19 #include <string> 20 21 #include "absl/strings/string_view.h" 22 #include "rtc_base/time_utils.h" 23 24 namespace webrtc { 25 namespace test { 26 27 void ApiCallStatistics::Add(int64_t duration_nanos, CallType call_type) { 28 calls_.push_back(CallData(duration_nanos, call_type)); 29 } 30 31 void ApiCallStatistics::PrintReport() const { 32 int64_t min_render = std::numeric_limits<int64_t>::max(); 33 int64_t min_capture = std::numeric_limits<int64_t>::max(); 34 int64_t max_render = 0; 35 int64_t max_capture = 0; 36 int64_t sum_render = 0; 37 int64_t sum_capture = 0; 38 int64_t num_render = 0; 39 int64_t num_capture = 0; 40 int64_t avg_render = 0; 41 int64_t avg_capture = 0; 42 43 for (auto v : calls_) { 44 if (v.call_type == CallType::kRender) { 45 ++num_render; 46 min_render = std::min(min_render, v.duration_nanos); 47 max_render = std::max(max_render, v.duration_nanos); 48 sum_render += v.duration_nanos; 49 } else { 50 ++num_capture; 51 min_capture = std::min(min_capture, v.duration_nanos); 52 max_capture = std::max(max_capture, v.duration_nanos); 53 sum_capture += v.duration_nanos; 54 } 55 } 56 min_render /= kNumNanosecsPerMicrosec; 57 max_render /= kNumNanosecsPerMicrosec; 58 sum_render /= kNumNanosecsPerMicrosec; 59 min_capture /= kNumNanosecsPerMicrosec; 60 max_capture /= kNumNanosecsPerMicrosec; 61 sum_capture /= kNumNanosecsPerMicrosec; 62 avg_render = num_render > 0 ? sum_render / num_render : 0; 63 avg_capture = num_capture > 0 ? sum_capture / num_capture : 0; 64 65 std::cout << std::endl 66 << "Total time: " << (sum_capture + sum_render) * 1e-6 << " s" 67 << std::endl 68 << " Render API calls:" << std::endl 69 << " min: " << min_render << " us" << std::endl 70 << " max: " << max_render << " us" << std::endl 71 << " avg: " << avg_render << " us" << std::endl 72 << " Capture API calls:" << std::endl 73 << " min: " << min_capture << " us" << std::endl 74 << " max: " << max_capture << " us" << std::endl 75 << " avg: " << avg_capture << " us" << std::endl; 76 } 77 78 void ApiCallStatistics::WriteReportToFile(absl::string_view filename) const { 79 std::unique_ptr<std::ofstream> out = 80 std::make_unique<std::ofstream>(std::string(filename)); 81 for (auto v : calls_) { 82 if (v.call_type == CallType::kRender) { 83 *out << "render, "; 84 } else { 85 *out << "capture, "; 86 } 87 *out << (v.duration_nanos / kNumNanosecsPerMicrosec) << std::endl; 88 } 89 } 90 91 ApiCallStatistics::CallData::CallData(int64_t duration_nanos, 92 CallType call_type) 93 : duration_nanos(duration_nanos), call_type(call_type) {} 94 95 } // namespace test 96 } // namespace webrtc