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 | |
| 26 | #include <algorithm> |
| 27 | |
| 28 | namespace arm_compute |
| 29 | { |
| 30 | namespace test |
| 31 | { |
| 32 | namespace framework |
| 33 | { |
| 34 | void JSONPrinter::print_separator(bool &flag) |
| 35 | { |
| 36 | if(flag) |
| 37 | { |
| 38 | flag = false; |
| 39 | } |
| 40 | else |
| 41 | { |
| 42 | *_stream << ","; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | void JSONPrinter::print_entry(const std::string &name, const std::string &value) |
| 47 | { |
| 48 | print_separator(_first_entry); |
| 49 | |
| 50 | *_stream << R"(")" << name << R"(" : ")" << value << R"(")"; |
| 51 | } |
| 52 | |
| 53 | void JSONPrinter::print_global_header() |
| 54 | { |
| 55 | *_stream << "{"; |
| 56 | } |
| 57 | |
| 58 | void JSONPrinter::print_global_footer() |
| 59 | { |
| 60 | *_stream << "}\n"; |
| 61 | } |
| 62 | |
| 63 | void JSONPrinter::print_run_header() |
| 64 | { |
| 65 | print_separator(_first_entry); |
| 66 | |
| 67 | *_stream << R"("tests" : {)"; |
| 68 | } |
| 69 | |
| 70 | void JSONPrinter::print_run_footer() |
| 71 | { |
| 72 | *_stream << "}"; |
| 73 | } |
| 74 | |
| 75 | void JSONPrinter::print_test_header(const std::string &name) |
| 76 | { |
| 77 | print_separator(_first_test); |
| 78 | |
| 79 | *_stream << R"(")" << name << R"(" : {)"; |
| 80 | } |
| 81 | |
| 82 | void JSONPrinter::print_test_footer() |
| 83 | { |
| 84 | *_stream << "}"; |
| 85 | } |
| 86 | |
| 87 | void JSONPrinter::print_measurements(const Profiler::MeasurementsMap &measurements) |
| 88 | { |
| 89 | for(auto i_it = measurements.cbegin(), i_end = measurements.cend(); i_it != i_end;) |
| 90 | { |
| 91 | *_stream << R"(")" << i_it->first << R"(" : {)"; |
| 92 | |
| 93 | auto add_measurements = [](double a, const Instrument::Measurement & b) |
| 94 | { |
| 95 | return a + b.value; |
| 96 | }; |
| 97 | |
| 98 | auto cmp_measurements = [](const Instrument::Measurement & a, const Instrument::Measurement & b) |
| 99 | { |
| 100 | return a.value < b.value; |
| 101 | }; |
| 102 | |
| 103 | double sum_values = std::accumulate(i_it->second.cbegin(), i_it->second.cend(), 0., add_measurements); |
| 104 | int num_values = i_it->second.size(); |
| 105 | const auto minmax_values = std::minmax_element(i_it->second.begin(), i_it->second.end(), cmp_measurements); |
| 106 | |
| 107 | if(num_values > 2) |
| 108 | { |
| 109 | sum_values -= minmax_values.first->value + minmax_values.second->value; |
| 110 | num_values -= 2; |
| 111 | } |
| 112 | |
| 113 | auto measurement_to_string = [](const Instrument::Measurement & measurement) |
| 114 | { |
| 115 | return support::cpp11::to_string(measurement.value); |
| 116 | }; |
| 117 | |
| 118 | *_stream << R"("avg" : )" << (sum_values / num_values) << ","; |
| 119 | *_stream << R"("min" : )" << minmax_values.first->value << ","; |
| 120 | *_stream << R"("max" : )" << minmax_values.second->value << ","; |
| 121 | *_stream << R"("raw" : [)" << join(i_it->second.begin(), i_it->second.end(), ",", measurement_to_string) << "],"; |
| 122 | *_stream << R"("unit" : ")" << minmax_values.first->unit << R"(")"; |
| 123 | *_stream << "}"; |
| 124 | |
| 125 | if(++i_it != i_end) |
| 126 | { |
| 127 | *_stream << ","; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | } // namespace framework |
| 132 | } // namespace test |
| 133 | } // namespace arm_compute |