blob: bf8fce78444b3e1ac1f3f6069c6a238048182509 [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 Pflanzera09de0c2017-09-01 20:41:12 +010026#include "tests/framework/Framework.h"
Moritz Pflanzer542002c2017-07-26 16:03:58 +010027
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
steniu01172c58d2017-08-31 13:49:08 +0100116void JSONPrinter::print_info(const std::string &info)
117{
118 std::istringstream iss(info);
119 for(std::string line; !std::getline(iss, line).fail();)
120 {
121 print_separator(_first_error);
122 *_stream << R"(")" << line << R"(")";
123 }
124}
125
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100126void JSONPrinter::print_measurements(const Profiler::MeasurementsMap &measurements)
127{
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100128 print_separator(_first_test_entry);
129
130 *_stream << R"("measurements" : {)";
131
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100132 for(auto i_it = measurements.cbegin(), i_end = measurements.cend(); i_it != i_end;)
133 {
134 *_stream << R"(")" << i_it->first << R"(" : {)";
135
136 auto add_measurements = [](double a, const Instrument::Measurement & b)
137 {
138 return a + b.value;
139 };
140
141 auto cmp_measurements = [](const Instrument::Measurement & a, const Instrument::Measurement & b)
142 {
143 return a.value < b.value;
144 };
145
146 double sum_values = std::accumulate(i_it->second.cbegin(), i_it->second.cend(), 0., add_measurements);
147 int num_values = i_it->second.size();
148 const auto minmax_values = std::minmax_element(i_it->second.begin(), i_it->second.end(), cmp_measurements);
149
150 if(num_values > 2)
151 {
152 sum_values -= minmax_values.first->value + minmax_values.second->value;
153 num_values -= 2;
154 }
155
156 auto measurement_to_string = [](const Instrument::Measurement & measurement)
157 {
158 return support::cpp11::to_string(measurement.value);
159 };
160
161 *_stream << R"("avg" : )" << (sum_values / num_values) << ",";
162 *_stream << R"("min" : )" << minmax_values.first->value << ",";
163 *_stream << R"("max" : )" << minmax_values.second->value << ",";
164 *_stream << R"("raw" : [)" << join(i_it->second.begin(), i_it->second.end(), ",", measurement_to_string) << "],";
165 *_stream << R"("unit" : ")" << minmax_values.first->unit << R"(")";
166 *_stream << "}";
167
168 if(++i_it != i_end)
169 {
170 *_stream << ",";
171 }
172 }
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100173
174 *_stream << "}";
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100175}
176} // namespace framework
177} // namespace test
178} // namespace arm_compute