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 "JSONPrinter.h"
25
26 #include "../Framework.h"
27 #include "../instruments/Measurement.h"
28
29 #include <algorithm>
30
31 namespace arm_compute
32 {
33 namespace test
34 {
35 namespace framework
36 {
print_separator(bool & flag)37 void JSONPrinter::print_separator(bool &flag)
38 {
39 if(flag)
40 {
41 flag = false;
42 }
43 else
44 {
45 *_stream << ",";
46 }
47 }
48
49 template <typename T>
print_strings(T && first,T && last)50 void JSONPrinter::print_strings(T &&first, T &&last)
51 {
52 bool first_entry = true;
53 std::stringstream log;
54
55 while(first != last)
56 {
57 print_separator(first_entry);
58
59 *_stream << R"(")";
60
61 log.str(*first);
62
63 for(std::string line; !std::getline(log, line).fail();)
64 {
65 *_stream << line << "; ";
66 }
67
68 *_stream << R"(")";
69
70 ++first;
71 }
72 }
73
print_entry(const std::string & name,const std::string & value)74 void JSONPrinter::print_entry(const std::string &name, const std::string &value)
75 {
76 print_separator(_first_entry);
77
78 *_stream << R"(")" << name << R"(" : ")" << value << R"(")";
79 }
80
print_global_header()81 void JSONPrinter::print_global_header()
82 {
83 *_stream << "{";
84 }
85
print_global_footer()86 void JSONPrinter::print_global_footer()
87 {
88 *_stream << "}\n";
89 }
90
print_run_header()91 void JSONPrinter::print_run_header()
92 {
93 print_separator(_first_entry);
94
95 *_stream << R"("tests" : {)";
96 }
97
print_run_footer()98 void JSONPrinter::print_run_footer()
99 {
100 *_stream << "}";
101 }
102
print_test_header(const TestInfo & info)103 void JSONPrinter::print_test_header(const TestInfo &info)
104 {
105 print_separator(_first_test);
106
107 _first_test_entry = true;
108 *_stream << R"(")" << info.name << R"(" : {)";
109 }
110
print_test_footer()111 void JSONPrinter::print_test_footer()
112 {
113 *_stream << "}";
114 }
115
print_list_tests(const std::vector<TestInfo> & infos)116 void JSONPrinter::print_list_tests(const std::vector<TestInfo> &infos)
117 {
118 *_stream << R"(, "list_tests" : {)";
119 bool first = true;
120 for(auto const &info : infos)
121 {
122 if(!first)
123 {
124 *_stream << ",";
125 }
126 *_stream << R"(")" << info.id << R"(" : { "name": ")" << info.name << R"(", "mode": ")" << info.mode << R"(", "status" : ")" << info.status << R"(" })";
127 first = false;
128 }
129 *_stream << "}";
130 }
print_errors_header()131 void JSONPrinter::print_errors_header()
132 {
133 _errors.clear();
134 _expected_errors.clear();
135 _infos.clear();
136 }
137
print_errors_footer()138 void JSONPrinter::print_errors_footer()
139 {
140 print_separator(_first_test_entry);
141
142 *_stream << R"("errors" : [)";
143 print_strings(_errors.begin(), _errors.end());
144 *_stream << "]";
145
146 *_stream << R"(, "expected_errors" : [)";
147 print_strings(_expected_errors.begin(), _expected_errors.end());
148 *_stream << "]";
149
150 *_stream << R"(, "infos" : [)";
151 print_strings(_infos.begin(), _infos.end());
152 *_stream << "]";
153 }
154
print_error(const std::exception & error,bool expected)155 void JSONPrinter::print_error(const std::exception &error, bool expected)
156 {
157 if(expected)
158 {
159 _expected_errors.emplace_back(error.what());
160 }
161 else
162 {
163 _errors.emplace_back(error.what());
164 }
165 }
166
print_info(const std::string & info)167 void JSONPrinter::print_info(const std::string &info)
168 {
169 _infos.push_back(info);
170 }
171
print_profiler_header(const std::string & header_data)172 void JSONPrinter::print_profiler_header(const std::string &header_data)
173 {
174 if(header_data.size() > 0)
175 {
176 print_separator(_first_test_entry);
177 }
178 *_stream << header_data;
179 }
180
print_measurements(const Profiler::MeasurementsMap & measurements)181 void JSONPrinter::print_measurements(const Profiler::MeasurementsMap &measurements)
182 {
183 print_separator(_first_test_entry);
184 *_stream << R"("measurements" : {)";
185
186 for(auto i_it = measurements.cbegin(), i_end = measurements.cend(); i_it != i_end;)
187 {
188 *_stream << R"(")" << i_it->first << R"(" : {)";
189
190 auto measurement_to_string = [](const Measurement & measurement)
191 {
192 if(measurement.raw_data().size() == 1)
193 {
194 return measurement.raw_data().front();
195 }
196 else
197 {
198 std::stringstream str;
199 str << R"([")";
200 str << join(measurement.raw_data().begin(), measurement.raw_data().end(), R"(",")");
201 str << R"("])";
202 return str.str();
203 }
204 };
205 *_stream << R"("raw" : [)" << join(i_it->second.begin(), i_it->second.end(), ",", measurement_to_string) << "],";
206 *_stream << R"("unit" : ")" << i_it->second.begin()->unit() << R"(")";
207 *_stream << "}";
208
209 if(++i_it != i_end)
210 {
211 *_stream << ",";
212 }
213 }
214
215 *_stream << "}";
216 }
217 } // namespace framework
218 } // namespace test
219 } // namespace arm_compute
220