xref: /aosp_15_r20/external/ComputeLibrary/tests/framework/printers/PrettyPrinter.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2017-2019,2021 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "PrettyPrinter.h"
25 
26 #include "../Framework.h"
27 #include "../instruments/InstrumentsStats.h"
28 #include "../instruments/Measurement.h"
29 
30 #include <algorithm>
31 
32 namespace arm_compute
33 {
34 namespace test
35 {
36 namespace framework
37 {
begin_color(const std::string & color) const38 std::string PrettyPrinter::begin_color(const std::string &color) const
39 {
40     if(!_color_output)
41     {
42         return "";
43     }
44 
45     return "\033[0;3" + color + "m";
46 }
47 
end_color() const48 std::string PrettyPrinter::end_color() const
49 {
50     if(!_color_output)
51     {
52         return "";
53     }
54 
55     return "\033[m";
56 }
57 
set_color_output(bool color_output)58 void PrettyPrinter::set_color_output(bool color_output)
59 {
60     _color_output = color_output;
61 }
62 
print_entry(const std::string & name,const std::string & value)63 void PrettyPrinter::print_entry(const std::string &name, const std::string &value)
64 {
65     *_stream << begin_color("4") << name << " = " << value << end_color() << "\n";
66 }
67 
print_global_header()68 void PrettyPrinter::print_global_header()
69 {
70 }
71 
print_global_footer()72 void PrettyPrinter::print_global_footer()
73 {
74 }
75 
print_run_header()76 void PrettyPrinter::print_run_header()
77 {
78 }
79 
print_run_footer()80 void PrettyPrinter::print_run_footer()
81 {
82 }
83 
print_test_header(const TestInfo & info)84 void PrettyPrinter::print_test_header(const TestInfo &info)
85 {
86     *_stream << begin_color("2") << "Running [" << info.id << "] '" << info.name << "'" << end_color() << "\n";
87 }
88 
print_test_footer()89 void PrettyPrinter::print_test_footer()
90 {
91 }
92 
print_errors_header()93 void PrettyPrinter::print_errors_header()
94 {
95 }
96 
print_errors_footer()97 void PrettyPrinter::print_errors_footer()
98 {
99 }
100 
print_info(const std::string & info)101 void PrettyPrinter::print_info(const std::string &info)
102 {
103     *_stream << begin_color("1") << "INFO: " << info << end_color() << "\n";
104 }
105 
print_error(const std::exception & error,bool expected)106 void PrettyPrinter::print_error(const std::exception &error, bool expected)
107 {
108     std::string prefix = expected ? "EXPECTED ERROR: " : "ERROR: ";
109     *_stream << begin_color("1") << prefix << error.what() << end_color() << "\n";
110 }
111 
print_list_tests(const std::vector<TestInfo> & infos)112 void PrettyPrinter::print_list_tests(const std::vector<TestInfo> &infos)
113 {
114     for(auto const &info : infos)
115     {
116         *_stream << "[" << info.id << ", " << info.mode << ", " << info.status << "] " << info.name << "\n";
117     }
118 }
119 
print_profiler_header(const std::string & header_data)120 void PrettyPrinter::print_profiler_header(const std::string &header_data)
121 {
122     ARM_COMPUTE_UNUSED(header_data);
123 }
124 
print_measurements(const Profiler::MeasurementsMap & measurements)125 void PrettyPrinter::print_measurements(const Profiler::MeasurementsMap &measurements)
126 {
127     for(const auto &instrument : measurements)
128     {
129         *_stream << begin_color("3") << "  " << instrument.first << ":";
130 
131         InstrumentsStats stats(instrument.second);
132 
133         *_stream << "    ";
134         *_stream << "AVG=" << stats.mean() << " " << stats.max().unit();
135         if(instrument.second.size() > 1)
136         {
137             *_stream << ", STDDEV=" << arithmetic_to_string(stats.relative_standard_deviation(), 2) << " %";
138             *_stream << ", MIN=" << stats.min();
139             *_stream << ", MAX=" << stats.max();
140             *_stream << ", MEDIAN=" << stats.median().value() << " " << stats.median().unit();
141         }
142         *_stream << end_color() << "\n";
143     }
144 }
145 } // namespace framework
146 } // namespace test
147 } // namespace arm_compute
148