blob: 099734e72f1cd247a522264e7bcb243dde014d90 [file] [log] [blame]
Moritz Pflanzer80fffae2017-07-05 11:02:37 +01001/*
2 * Copyright (c) 2017 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
Moritz Pflanzer542002c2017-07-26 16:03:58 +010026#include "framework/Framework.h"
27
Moritz Pflanzer80fffae2017-07-05 11:02:37 +010028#include <algorithm>
29
30namespace arm_compute
31{
32namespace test
33{
34namespace framework
35{
36void JSONPrinter::print_separator(bool &flag)
37{
38 if(flag)
39 {
40 flag = false;
41 }
42 else
43 {
44 *_stream << ",";
45 }
46}
47
48void JSONPrinter::print_entry(const std::string &name, const std::string &value)
49{
50 print_separator(_first_entry);
51
52 *_stream << R"(")" << name << R"(" : ")" << value << R"(")";
53}
54
55void JSONPrinter::print_global_header()
56{
57 *_stream << "{";
58}
59
60void JSONPrinter::print_global_footer()
61{
62 *_stream << "}\n";
63}
64
65void JSONPrinter::print_run_header()
66{
67 print_separator(_first_entry);
68
69 *_stream << R"("tests" : {)";
70}
71
72void JSONPrinter::print_run_footer()
73{
74 *_stream << "}";
75}
76
Moritz Pflanzer542002c2017-07-26 16:03:58 +010077void JSONPrinter::print_test_header(const TestInfo &info)
Moritz Pflanzer80fffae2017-07-05 11:02:37 +010078{
79 print_separator(_first_test);
80
Moritz Pflanzer24a82462017-08-04 11:34:44 +010081 _first_test_entry = true;
Moritz Pflanzer542002c2017-07-26 16:03:58 +010082 *_stream << R"(")" << info.name << R"(" : {)";
Moritz Pflanzer80fffae2017-07-05 11:02:37 +010083}
84
85void JSONPrinter::print_test_footer()
86{
87 *_stream << "}";
88}
89
Moritz Pflanzer24a82462017-08-04 11:34:44 +010090void JSONPrinter::print_errors_header()
91{
92 print_separator(_first_test_entry);
93
94 _first_error = true;
95 *_stream << R"("errors" : [)";
96}
97
98void JSONPrinter::print_errors_footer()
99{
100 *_stream << "]";
101}
102
103void JSONPrinter::print_error(const std::exception &error)
104{
Anthony Barbierd4ab78a2017-08-09 14:27:09 +0100105 std::stringstream error_log;
106 error_log.str(error.what());
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100107
Moritz Pflanzer369e0142017-08-10 14:26:34 +0100108 for(std::string line; !std::getline(error_log, line).fail();)
Anthony Barbierd4ab78a2017-08-09 14:27:09 +0100109 {
110 print_separator(_first_error);
111
112 *_stream << R"(")" << line << R"(")";
113 }
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100114}
115
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100116void JSONPrinter::print_measurements(const Profiler::MeasurementsMap &measurements)
117{
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100118 print_separator(_first_test_entry);
119
120 *_stream << R"("measurements" : {)";
121
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100122 for(auto i_it = measurements.cbegin(), i_end = measurements.cend(); i_it != i_end;)
123 {
124 *_stream << R"(")" << i_it->first << R"(" : {)";
125
126 auto add_measurements = [](double a, const Instrument::Measurement & b)
127 {
128 return a + b.value;
129 };
130
131 auto cmp_measurements = [](const Instrument::Measurement & a, const Instrument::Measurement & b)
132 {
133 return a.value < b.value;
134 };
135
136 double sum_values = std::accumulate(i_it->second.cbegin(), i_it->second.cend(), 0., add_measurements);
137 int num_values = i_it->second.size();
138 const auto minmax_values = std::minmax_element(i_it->second.begin(), i_it->second.end(), cmp_measurements);
139
140 if(num_values > 2)
141 {
142 sum_values -= minmax_values.first->value + minmax_values.second->value;
143 num_values -= 2;
144 }
145
146 auto measurement_to_string = [](const Instrument::Measurement & measurement)
147 {
148 return support::cpp11::to_string(measurement.value);
149 };
150
151 *_stream << R"("avg" : )" << (sum_values / num_values) << ",";
152 *_stream << R"("min" : )" << minmax_values.first->value << ",";
153 *_stream << R"("max" : )" << minmax_values.second->value << ",";
154 *_stream << R"("raw" : [)" << join(i_it->second.begin(), i_it->second.end(), ",", measurement_to_string) << "],";
155 *_stream << R"("unit" : ")" << minmax_values.first->unit << R"(")";
156 *_stream << "}";
157
158 if(++i_it != i_end)
159 {
160 *_stream << ",";
161 }
162 }
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100163
164 *_stream << "}";
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100165}
166} // namespace framework
167} // namespace test
168} // namespace arm_compute