Moritz Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 1 | /* |
| 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 Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 26 | #include "framework/Framework.h" |
| 27 | |
Moritz Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 28 | #include <algorithm> |
| 29 | |
| 30 | namespace arm_compute |
| 31 | { |
| 32 | namespace test |
| 33 | { |
| 34 | namespace framework |
| 35 | { |
| 36 | void JSONPrinter::print_separator(bool &flag) |
| 37 | { |
| 38 | if(flag) |
| 39 | { |
| 40 | flag = false; |
| 41 | } |
| 42 | else |
| 43 | { |
| 44 | *_stream << ","; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | void 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 | |
| 55 | void JSONPrinter::print_global_header() |
| 56 | { |
| 57 | *_stream << "{"; |
| 58 | } |
| 59 | |
| 60 | void JSONPrinter::print_global_footer() |
| 61 | { |
| 62 | *_stream << "}\n"; |
| 63 | } |
| 64 | |
| 65 | void JSONPrinter::print_run_header() |
| 66 | { |
| 67 | print_separator(_first_entry); |
| 68 | |
| 69 | *_stream << R"("tests" : {)"; |
| 70 | } |
| 71 | |
| 72 | void JSONPrinter::print_run_footer() |
| 73 | { |
| 74 | *_stream << "}"; |
| 75 | } |
| 76 | |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 77 | void JSONPrinter::print_test_header(const TestInfo &info) |
Moritz Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 78 | { |
| 79 | print_separator(_first_test); |
| 80 | |
Moritz Pflanzer | 24a8246 | 2017-08-04 11:34:44 +0100 | [diff] [blame] | 81 | _first_test_entry = true; |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 82 | *_stream << R"(")" << info.name << R"(" : {)"; |
Moritz Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void JSONPrinter::print_test_footer() |
| 86 | { |
| 87 | *_stream << "}"; |
| 88 | } |
| 89 | |
Moritz Pflanzer | 24a8246 | 2017-08-04 11:34:44 +0100 | [diff] [blame] | 90 | void JSONPrinter::print_errors_header() |
| 91 | { |
| 92 | print_separator(_first_test_entry); |
| 93 | |
| 94 | _first_error = true; |
| 95 | *_stream << R"("errors" : [)"; |
| 96 | } |
| 97 | |
| 98 | void JSONPrinter::print_errors_footer() |
| 99 | { |
| 100 | *_stream << "]"; |
| 101 | } |
| 102 | |
| 103 | void JSONPrinter::print_error(const std::exception &error) |
| 104 | { |
| 105 | print_separator(_first_error); |
| 106 | |
| 107 | *_stream << R"(")" << error.what() << R"(")"; |
| 108 | } |
| 109 | |
Moritz Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 110 | void JSONPrinter::print_measurements(const Profiler::MeasurementsMap &measurements) |
| 111 | { |
Moritz Pflanzer | 24a8246 | 2017-08-04 11:34:44 +0100 | [diff] [blame] | 112 | print_separator(_first_test_entry); |
| 113 | |
| 114 | *_stream << R"("measurements" : {)"; |
| 115 | |
Moritz Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 116 | for(auto i_it = measurements.cbegin(), i_end = measurements.cend(); i_it != i_end;) |
| 117 | { |
| 118 | *_stream << R"(")" << i_it->first << R"(" : {)"; |
| 119 | |
| 120 | auto add_measurements = [](double a, const Instrument::Measurement & b) |
| 121 | { |
| 122 | return a + b.value; |
| 123 | }; |
| 124 | |
| 125 | auto cmp_measurements = [](const Instrument::Measurement & a, const Instrument::Measurement & b) |
| 126 | { |
| 127 | return a.value < b.value; |
| 128 | }; |
| 129 | |
| 130 | double sum_values = std::accumulate(i_it->second.cbegin(), i_it->second.cend(), 0., add_measurements); |
| 131 | int num_values = i_it->second.size(); |
| 132 | const auto minmax_values = std::minmax_element(i_it->second.begin(), i_it->second.end(), cmp_measurements); |
| 133 | |
| 134 | if(num_values > 2) |
| 135 | { |
| 136 | sum_values -= minmax_values.first->value + minmax_values.second->value; |
| 137 | num_values -= 2; |
| 138 | } |
| 139 | |
| 140 | auto measurement_to_string = [](const Instrument::Measurement & measurement) |
| 141 | { |
| 142 | return support::cpp11::to_string(measurement.value); |
| 143 | }; |
| 144 | |
| 145 | *_stream << R"("avg" : )" << (sum_values / num_values) << ","; |
| 146 | *_stream << R"("min" : )" << minmax_values.first->value << ","; |
| 147 | *_stream << R"("max" : )" << minmax_values.second->value << ","; |
| 148 | *_stream << R"("raw" : [)" << join(i_it->second.begin(), i_it->second.end(), ",", measurement_to_string) << "],"; |
| 149 | *_stream << R"("unit" : ")" << minmax_values.first->unit << R"(")"; |
| 150 | *_stream << "}"; |
| 151 | |
| 152 | if(++i_it != i_end) |
| 153 | { |
| 154 | *_stream << ","; |
| 155 | } |
| 156 | } |
Moritz Pflanzer | 24a8246 | 2017-08-04 11:34:44 +0100 | [diff] [blame] | 157 | |
| 158 | *_stream << "}"; |
Moritz Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 159 | } |
| 160 | } // namespace framework |
| 161 | } // namespace test |
| 162 | } // namespace arm_compute |