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