blob: 780664441890d8b5ad2703e71fecc7d7a34b95fa [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 Pflanzer542002c2017-07-26 16:03:58 +010081 *_stream << R"(")" << info.name << R"(" : {)";
Moritz Pflanzer80fffae2017-07-05 11:02:37 +010082}
83
84void JSONPrinter::print_test_footer()
85{
86 *_stream << "}";
87}
88
89void JSONPrinter::print_measurements(const Profiler::MeasurementsMap &measurements)
90{
91 for(auto i_it = measurements.cbegin(), i_end = measurements.cend(); i_it != i_end;)
92 {
93 *_stream << R"(")" << i_it->first << R"(" : {)";
94
95 auto add_measurements = [](double a, const Instrument::Measurement & b)
96 {
97 return a + b.value;
98 };
99
100 auto cmp_measurements = [](const Instrument::Measurement & a, const Instrument::Measurement & b)
101 {
102 return a.value < b.value;
103 };
104
105 double sum_values = std::accumulate(i_it->second.cbegin(), i_it->second.cend(), 0., add_measurements);
106 int num_values = i_it->second.size();
107 const auto minmax_values = std::minmax_element(i_it->second.begin(), i_it->second.end(), cmp_measurements);
108
109 if(num_values > 2)
110 {
111 sum_values -= minmax_values.first->value + minmax_values.second->value;
112 num_values -= 2;
113 }
114
115 auto measurement_to_string = [](const Instrument::Measurement & measurement)
116 {
117 return support::cpp11::to_string(measurement.value);
118 };
119
120 *_stream << R"("avg" : )" << (sum_values / num_values) << ",";
121 *_stream << R"("min" : )" << minmax_values.first->value << ",";
122 *_stream << R"("max" : )" << minmax_values.second->value << ",";
123 *_stream << R"("raw" : [)" << join(i_it->second.begin(), i_it->second.end(), ",", measurement_to_string) << "],";
124 *_stream << R"("unit" : ")" << minmax_values.first->unit << R"(")";
125 *_stream << "}";
126
127 if(++i_it != i_end)
128 {
129 *_stream << ",";
130 }
131 }
132}
133} // namespace framework
134} // namespace test
135} // namespace arm_compute