Compute Library
 22.08
JSONPrinter.cpp
Go to the documentation of this file.
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 {
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>
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 
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 
82 {
83  *_stream << "{";
84 }
85 
87 {
88  *_stream << "}\n";
89 }
90 
92 {
93  print_separator(_first_entry);
94 
95  *_stream << R"("tests" : {)";
96 }
97 
99 {
100  *_stream << "}";
101 }
102 
104 {
105  print_separator(_first_test);
106 
107  _first_test_entry = true;
108  *_stream << R"(")" << info.name << R"(" : {)";
109 }
110 
112 {
113  *_stream << "}";
114 }
115 
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 }
132 {
133  _errors.clear();
134  _expected_errors.clear();
135  _infos.clear();
136 }
137 
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 
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 
167 void JSONPrinter::print_info(const std::string &info)
168 {
169  _infos.push_back(info);
170 }
171 
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 
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
void print_run_footer() override
Print footer after running all tests.
Definition: JSONPrinter.cpp:98
std::string join(T first, T last, const std::string &separator)
Helper function to concatenate multiple strings.
Definition: Utils.h:93
void print_measurements(const Profiler::MeasurementsMap &measurements) override
Print measurements for a test.
void print_info(const std::string &info) override
Print test log info.
void print_errors_header() override
Print header before errors.
Generic measurement that stores values as either double or long long int.
Definition: Measurement.h:41
void print_global_footer() override
Print global footer.
Definition: JSONPrinter.cpp:86
Copyright (c) 2017-2022 Arm Limited.
void print_entry(const std::string &name, const std::string &value) override
Print an entry consisting of a (name, value) pair.
Definition: JSONPrinter.cpp:74
std::string name
Test name.
Definition: Framework.h:81
void print_test_footer() override
Print footer after a test.
void print_global_header() override
Print global header.
Definition: JSONPrinter.cpp:81
Information about a test case.
Definition: Framework.h:78
std::map< std::string, std::vector< Measurement > > MeasurementsMap
Mapping from instrument ids to their measurements.
Definition: Profiler.h:49
const char * name
void print_list_tests(const std::vector< TestInfo > &infos) override
Print the list of all the tests.
void print_errors_footer() override
Print footer after errors.
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
void print_profiler_header(const std::string &header_data) override
Print header data.
void print_run_header() override
Print header before running all tests.
Definition: JSONPrinter.cpp:91
void print_error(const std::exception &error, bool expected) override
Print test error.
void print_test_header(const TestInfo &info) override
Print header before a test.