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