blob: 0995ff3594bff53eba21984d149b0b4892dd9bd3 [file] [log] [blame]
Moritz Pflanzer80fffae2017-07-05 11:02:37 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2019 Arm Limited.
Moritz Pflanzer80fffae2017-07-05 11:02:37 +01003 *
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
Moritz Pflanzer09e4f982017-08-30 12:47:06 +010026#include "../Framework.h"
27#include "../instruments/Measurement.h"
Moritz Pflanzer542002c2017-07-26 16:03:58 +010028
Moritz Pflanzer80fffae2017-07-05 11:02:37 +010029#include <algorithm>
30
31namespace arm_compute
32{
33namespace test
34{
35namespace framework
36{
37void JSONPrinter::print_separator(bool &flag)
38{
39 if(flag)
40 {
41 flag = false;
42 }
43 else
44 {
45 *_stream << ",";
46 }
47}
48
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +010049template <typename T>
50void 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
Moritz Pflanzer80fffae2017-07-05 11:02:37 +010074void 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
81void JSONPrinter::print_global_header()
82{
83 *_stream << "{";
84}
85
86void JSONPrinter::print_global_footer()
87{
88 *_stream << "}\n";
89}
90
91void JSONPrinter::print_run_header()
92{
93 print_separator(_first_entry);
94
95 *_stream << R"("tests" : {)";
96}
97
98void JSONPrinter::print_run_footer()
99{
100 *_stream << "}";
101}
102
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100103void JSONPrinter::print_test_header(const TestInfo &info)
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100104{
105 print_separator(_first_test);
106
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100107 _first_test_entry = true;
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100108 *_stream << R"(")" << info.name << R"(" : {)";
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100109}
110
111void JSONPrinter::print_test_footer()
112{
113 *_stream << "}";
114}
115
Anthony Barbierdbfb31c2017-11-24 11:24:45 +0000116void JSONPrinter::print_list_tests(const std::vector<TestInfo> &infos)
117{
118 *_stream << R"(, "list_tests" : {)";
119 bool first = true;
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100120 for(auto const &info : infos)
Anthony Barbierdbfb31c2017-11-24 11:24:45 +0000121 {
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}
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100131void JSONPrinter::print_errors_header()
132{
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100133 _errors.clear();
134 _expected_errors.clear();
135 _infos.clear();
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100136}
137
138void JSONPrinter::print_errors_footer()
139{
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100140 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());
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100152 *_stream << "]";
153}
154
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100155void JSONPrinter::print_error(const std::exception &error, bool expected)
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100156{
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100157 if(expected)
Anthony Barbierd4ab78a2017-08-09 14:27:09 +0100158 {
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100159 _expected_errors.emplace_back(error.what());
160 }
161 else
162 {
163 _errors.emplace_back(error.what());
Anthony Barbierd4ab78a2017-08-09 14:27:09 +0100164 }
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100165}
166
steniu01172c58d2017-08-31 13:49:08 +0100167void JSONPrinter::print_info(const std::string &info)
168{
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100169 _infos.push_back(info);
steniu01172c58d2017-08-31 13:49:08 +0100170}
171
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100172void JSONPrinter::print_measurements(const Profiler::MeasurementsMap &measurements)
173{
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100174 print_separator(_first_test_entry);
175
176 *_stream << R"("measurements" : {)";
177
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100178 for(auto i_it = measurements.cbegin(), i_end = measurements.cend(); i_it != i_end;)
179 {
180 *_stream << R"(")" << i_it->first << R"(" : {)";
181
Moritz Pflanzer09e4f982017-08-30 12:47:06 +0100182 auto measurement_to_string = [](const Measurement & measurement)
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100183 {
Anthony Barbierab14c152017-11-09 10:29:59 +0000184 if(measurement.raw_data().size() == 1)
185 {
186 return measurement.raw_data().front();
187 }
188 else
189 {
190 std::stringstream str;
Anthony Barbierd6073752017-11-10 12:01:18 +0000191 str << R"([")";
192 str << join(measurement.raw_data().begin(), measurement.raw_data().end(), R"(",")");
193 str << R"("])";
Anthony Barbierab14c152017-11-09 10:29:59 +0000194 return str.str();
195 }
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100196 };
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100197 *_stream << R"("raw" : [)" << join(i_it->second.begin(), i_it->second.end(), ",", measurement_to_string) << "],";
Anthony Barbierc9afce52018-01-26 16:07:50 +0000198 *_stream << R"("unit" : ")" << i_it->second.begin()->unit() << R"(")";
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100199 *_stream << "}";
200
201 if(++i_it != i_end)
202 {
203 *_stream << ",";
204 }
205 }
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100206
207 *_stream << "}";
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100208}
209} // namespace framework
210} // namespace test
211} // namespace arm_compute