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