blob: ae19cae67cf48804804f32d520d2464e84bb58ff [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 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
49void JSONPrinter::print_entry(const std::string &name, const std::string &value)
50{
51 print_separator(_first_entry);
52
53 *_stream << R"(")" << name << R"(" : ")" << value << R"(")";
54}
55
56void JSONPrinter::print_global_header()
57{
58 *_stream << "{";
59}
60
61void JSONPrinter::print_global_footer()
62{
63 *_stream << "}\n";
64}
65
66void JSONPrinter::print_run_header()
67{
68 print_separator(_first_entry);
69
70 *_stream << R"("tests" : {)";
71}
72
73void JSONPrinter::print_run_footer()
74{
75 *_stream << "}";
76}
77
Moritz Pflanzer542002c2017-07-26 16:03:58 +010078void JSONPrinter::print_test_header(const TestInfo &info)
Moritz Pflanzer80fffae2017-07-05 11:02:37 +010079{
80 print_separator(_first_test);
81
Moritz Pflanzer24a82462017-08-04 11:34:44 +010082 _first_test_entry = true;
Moritz Pflanzer542002c2017-07-26 16:03:58 +010083 *_stream << R"(")" << info.name << R"(" : {)";
Moritz Pflanzer80fffae2017-07-05 11:02:37 +010084}
85
86void JSONPrinter::print_test_footer()
87{
88 *_stream << "}";
89}
90
Moritz Pflanzer24a82462017-08-04 11:34:44 +010091void JSONPrinter::print_errors_header()
92{
93 print_separator(_first_test_entry);
94
95 _first_error = true;
96 *_stream << R"("errors" : [)";
97}
98
99void JSONPrinter::print_errors_footer()
100{
101 *_stream << "]";
102}
103
104void JSONPrinter::print_error(const std::exception &error)
105{
Anthony Barbierd4ab78a2017-08-09 14:27:09 +0100106 std::stringstream error_log;
107 error_log.str(error.what());
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100108
Moritz Pflanzer369e0142017-08-10 14:26:34 +0100109 for(std::string line; !std::getline(error_log, line).fail();)
Anthony Barbierd4ab78a2017-08-09 14:27:09 +0100110 {
111 print_separator(_first_error);
112
113 *_stream << R"(")" << line << R"(")";
114 }
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100115}
116
steniu01172c58d2017-08-31 13:49:08 +0100117void JSONPrinter::print_info(const std::string &info)
118{
119 std::istringstream iss(info);
120 for(std::string line; !std::getline(iss, line).fail();)
121 {
122 print_separator(_first_error);
123 *_stream << R"(")" << line << R"(")";
124 }
125}
126
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100127void JSONPrinter::print_measurements(const Profiler::MeasurementsMap &measurements)
128{
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100129 print_separator(_first_test_entry);
130
131 *_stream << R"("measurements" : {)";
132
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100133 for(auto i_it = measurements.cbegin(), i_end = measurements.cend(); i_it != i_end;)
134 {
135 *_stream << R"(")" << i_it->first << R"(" : {)";
136
Moritz Pflanzer09e4f982017-08-30 12:47:06 +0100137 auto add_measurements = [](double a, const Measurement & b)
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100138 {
139 return a + b.value;
140 };
141
Moritz Pflanzer09e4f982017-08-30 12:47:06 +0100142 auto cmp_measurements = [](const Measurement & a, const Measurement & b)
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100143 {
144 return a.value < b.value;
145 };
146
147 double sum_values = std::accumulate(i_it->second.cbegin(), i_it->second.cend(), 0., add_measurements);
148 int num_values = i_it->second.size();
149 const auto minmax_values = std::minmax_element(i_it->second.begin(), i_it->second.end(), cmp_measurements);
150
151 if(num_values > 2)
152 {
153 sum_values -= minmax_values.first->value + minmax_values.second->value;
154 num_values -= 2;
155 }
156
Moritz Pflanzer09e4f982017-08-30 12:47:06 +0100157 auto measurement_to_string = [](const Measurement & measurement)
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100158 {
159 return support::cpp11::to_string(measurement.value);
160 };
161
162 *_stream << R"("avg" : )" << (sum_values / num_values) << ",";
163 *_stream << R"("min" : )" << minmax_values.first->value << ",";
164 *_stream << R"("max" : )" << minmax_values.second->value << ",";
165 *_stream << R"("raw" : [)" << join(i_it->second.begin(), i_it->second.end(), ",", measurement_to_string) << "],";
166 *_stream << R"("unit" : ")" << minmax_values.first->unit << R"(")";
167 *_stream << "}";
168
169 if(++i_it != i_end)
170 {
171 *_stream << ",";
172 }
173 }
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100174
175 *_stream << "}";
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100176}
177} // namespace framework
178} // namespace test
179} // namespace arm_compute