Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +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 "Framework.h" |
| 25 | |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 26 | #include "support/ToolchainSupport.h" |
| 27 | |
Moritz Pflanzer | 47752c9 | 2017-07-18 13:38:47 +0100 | [diff] [blame] | 28 | #ifdef ARM_COMPUTE_CL |
| 29 | #include "arm_compute/core/CL/OpenCL.h" |
Anthony Barbier | bf95922 | 2017-07-19 17:01:42 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/CL/CLScheduler.h" |
Moritz Pflanzer | 47752c9 | 2017-07-18 13:38:47 +0100 | [diff] [blame] | 31 | #endif /* ARM_COMPUTE_CL */ |
| 32 | |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 33 | #include <chrono> |
| 34 | #include <iostream> |
| 35 | #include <sstream> |
| 36 | #include <type_traits> |
| 37 | |
| 38 | namespace arm_compute |
| 39 | { |
| 40 | namespace test |
| 41 | { |
| 42 | namespace framework |
| 43 | { |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 44 | Framework::Framework() |
| 45 | { |
| 46 | _available_instruments.emplace(InstrumentType::WALL_CLOCK_TIMER, Instrument::make_instrument<WallClockTimer>); |
| 47 | #ifdef PMU_ENABLED |
Moritz Pflanzer | 09e4f98 | 2017-08-30 12:47:06 +0100 | [diff] [blame] | 48 | _available_instruments.emplace(InstrumentType::PMU, Instrument::make_instrument<PMUCounter>); |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 49 | #endif /* PMU_ENABLED */ |
| 50 | } |
| 51 | |
| 52 | std::set<InstrumentType> Framework::available_instruments() const |
| 53 | { |
| 54 | std::set<InstrumentType> types; |
| 55 | |
| 56 | for(const auto &instrument : _available_instruments) |
| 57 | { |
| 58 | types.emplace(instrument.first); |
| 59 | } |
| 60 | |
| 61 | return types; |
| 62 | } |
| 63 | |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 64 | std::map<TestResult::Status, int> Framework::count_test_results() const |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 65 | { |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 66 | std::map<TestResult::Status, int> counts; |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 67 | |
| 68 | for(const auto &test : _test_results) |
| 69 | { |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 70 | ++counts[test.second.status]; |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 71 | } |
| 72 | |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 73 | return counts; |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | Framework &Framework::get() |
| 77 | { |
| 78 | static Framework instance; |
| 79 | return instance; |
| 80 | } |
| 81 | |
Moritz Pflanzer | ec2de0f | 2017-07-27 14:43:46 +0100 | [diff] [blame] | 82 | void Framework::init(const std::vector<InstrumentType> &instruments, int num_iterations, DatasetMode mode, const std::string &name_filter, const std::string &id_filter, LogLevel log_level) |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 83 | { |
Moritz Pflanzer | ec2de0f | 2017-07-27 14:43:46 +0100 | [diff] [blame] | 84 | _test_filter = TestFilter(mode, name_filter, id_filter); |
| 85 | _num_iterations = num_iterations; |
| 86 | _log_level = log_level; |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 87 | |
Moritz Pflanzer | 09e4f98 | 2017-08-30 12:47:06 +0100 | [diff] [blame] | 88 | _instruments = std::set<InstrumentType>(instruments.begin(), instruments.end()); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | std::string Framework::current_suite_name() const |
| 92 | { |
| 93 | return join(_test_suite_name.cbegin(), _test_suite_name.cend(), "/"); |
| 94 | } |
| 95 | |
| 96 | void Framework::push_suite(std::string name) |
| 97 | { |
| 98 | _test_suite_name.emplace_back(std::move(name)); |
| 99 | } |
| 100 | |
| 101 | void Framework::pop_suite() |
| 102 | { |
| 103 | _test_suite_name.pop_back(); |
| 104 | } |
| 105 | |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 106 | void Framework::add_test_info(std::string info) |
| 107 | { |
| 108 | _test_info.emplace_back(std::move(info)); |
| 109 | } |
| 110 | |
| 111 | void Framework::clear_test_info() |
| 112 | { |
| 113 | _test_info.clear(); |
| 114 | } |
| 115 | |
| 116 | bool Framework::has_test_info() const |
| 117 | { |
| 118 | return !_test_info.empty(); |
| 119 | } |
| 120 | |
| 121 | void Framework::print_test_info(std::ostream &os) const |
| 122 | { |
Moritz Pflanzer | 8df3faf | 2017-07-28 13:57:53 +0100 | [diff] [blame] | 123 | if(!_test_info.empty()) |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 124 | { |
Moritz Pflanzer | 8df3faf | 2017-07-28 13:57:53 +0100 | [diff] [blame] | 125 | os << "CONTEXT:\n"; |
| 126 | |
| 127 | for(const auto &str : _test_info) |
| 128 | { |
| 129 | os << " " << str << "\n"; |
| 130 | } |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 134 | void Framework::log_test_start(const TestInfo &info) |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 135 | { |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 136 | if(_printer != nullptr && _log_level >= LogLevel::TESTS) |
Moritz Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 137 | { |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 138 | _printer->print_test_header(info); |
Moritz Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 139 | } |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 140 | } |
| 141 | |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 142 | void Framework::log_test_skipped(const TestInfo &info) |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 143 | { |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 144 | static_cast<void>(info); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 145 | } |
| 146 | |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 147 | void Framework::log_test_end(const TestInfo &info) |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 148 | { |
Moritz Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 149 | if(_printer != nullptr) |
| 150 | { |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 151 | if(_log_level >= LogLevel::MEASUREMENTS) |
| 152 | { |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 153 | _printer->print_measurements(_test_results.at(info).measurements); |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | if(_log_level >= LogLevel::TESTS) |
| 157 | { |
| 158 | _printer->print_test_footer(); |
| 159 | } |
Moritz Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 160 | } |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Moritz Pflanzer | 24a8246 | 2017-08-04 11:34:44 +0100 | [diff] [blame] | 163 | void Framework::log_failed_expectation(const TestError &error) |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 164 | { |
Moritz Pflanzer | 24a8246 | 2017-08-04 11:34:44 +0100 | [diff] [blame] | 165 | if(_log_level >= error.level() && _printer != nullptr) |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 166 | { |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 167 | constexpr bool expected_error = true; |
| 168 | _printer->print_error(error, expected_error); |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 169 | } |
Moritz Pflanzer | e1103a8 | 2017-07-18 12:20:45 +0100 | [diff] [blame] | 170 | |
| 171 | if(_current_test_result != nullptr) |
| 172 | { |
| 173 | _current_test_result->status = TestResult::Status::FAILED; |
| 174 | } |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 175 | } |
| 176 | |
steniu01 | 172c58d | 2017-08-31 13:49:08 +0100 | [diff] [blame] | 177 | void Framework::log_info(const std::string &info) |
| 178 | { |
| 179 | if(_log_level >= LogLevel::DEBUG && _printer != nullptr) |
| 180 | { |
| 181 | _printer->print_info(info); |
| 182 | } |
| 183 | } |
| 184 | |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 185 | int Framework::num_iterations() const |
| 186 | { |
| 187 | return _num_iterations; |
| 188 | } |
| 189 | |
| 190 | void Framework::set_num_iterations(int num_iterations) |
| 191 | { |
| 192 | _num_iterations = num_iterations; |
| 193 | } |
| 194 | |
| 195 | void Framework::set_throw_errors(bool throw_errors) |
| 196 | { |
| 197 | _throw_errors = throw_errors; |
| 198 | } |
| 199 | |
| 200 | bool Framework::throw_errors() const |
| 201 | { |
| 202 | return _throw_errors; |
| 203 | } |
| 204 | |
Moritz Pflanzer | fa81165 | 2017-07-26 17:00:37 +0100 | [diff] [blame] | 205 | void Framework::set_stop_on_error(bool stop_on_error) |
| 206 | { |
| 207 | _stop_on_error = stop_on_error; |
| 208 | } |
| 209 | |
| 210 | bool Framework::stop_on_error() const |
| 211 | { |
| 212 | return _stop_on_error; |
| 213 | } |
| 214 | |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 215 | void Framework::run_test(const TestInfo &info, TestCaseFactory &test_factory) |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 216 | { |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 217 | if(test_factory.status() == TestCaseFactory::Status::DISABLED) |
| 218 | { |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 219 | log_test_skipped(info); |
| 220 | set_test_result(info, TestResult(TestResult::Status::DISABLED)); |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 221 | return; |
| 222 | } |
| 223 | |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 224 | log_test_start(info); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 225 | |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 226 | Profiler profiler = get_profiler(); |
Moritz Pflanzer | e33eb64 | 2017-07-31 14:48:45 +0100 | [diff] [blame] | 227 | TestResult result(TestResult::Status::NOT_RUN); |
Moritz Pflanzer | e1103a8 | 2017-07-18 12:20:45 +0100 | [diff] [blame] | 228 | |
| 229 | _current_test_result = &result; |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 230 | |
Moritz Pflanzer | 24a8246 | 2017-08-04 11:34:44 +0100 | [diff] [blame] | 231 | if(_log_level >= LogLevel::ERRORS && _printer != nullptr) |
| 232 | { |
| 233 | _printer->print_errors_header(); |
| 234 | } |
| 235 | |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 236 | const bool is_expected_failure = test_factory.status() == TestCaseFactory::Status::EXPECTED_FAILURE; |
| 237 | |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 238 | try |
| 239 | { |
| 240 | std::unique_ptr<TestCase> test_case = test_factory.make(); |
| 241 | |
| 242 | try |
| 243 | { |
| 244 | test_case->do_setup(); |
| 245 | |
| 246 | for(int i = 0; i < _num_iterations; ++i) |
| 247 | { |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 248 | profiler.start(); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 249 | test_case->do_run(); |
Anthony Barbier | bf95922 | 2017-07-19 17:01:42 +0100 | [diff] [blame] | 250 | #ifdef ARM_COMPUTE_CL |
| 251 | if(opencl_is_available()) |
| 252 | { |
| 253 | CLScheduler::get().sync(); |
| 254 | } |
| 255 | #endif /* ARM_COMPUTE_CL */ |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 256 | profiler.stop(); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | test_case->do_teardown(); |
Moritz Pflanzer | e33eb64 | 2017-07-31 14:48:45 +0100 | [diff] [blame] | 260 | |
| 261 | // Change status to success if no error has happend |
| 262 | if(result.status == TestResult::Status::NOT_RUN) |
| 263 | { |
| 264 | result.status = TestResult::Status::SUCCESS; |
| 265 | } |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 266 | } |
| 267 | catch(const TestError &error) |
| 268 | { |
Moritz Pflanzer | 24a8246 | 2017-08-04 11:34:44 +0100 | [diff] [blame] | 269 | if(_log_level >= error.level() && _printer != nullptr) |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 270 | { |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 271 | _printer->print_error(error, is_expected_failure); |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 272 | } |
| 273 | |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 274 | result.status = TestResult::Status::FAILED; |
| 275 | |
| 276 | if(_throw_errors) |
| 277 | { |
| 278 | throw; |
| 279 | } |
| 280 | } |
Moritz Pflanzer | 47752c9 | 2017-07-18 13:38:47 +0100 | [diff] [blame] | 281 | #ifdef ARM_COMPUTE_CL |
| 282 | catch(const ::cl::Error &error) |
| 283 | { |
Moritz Pflanzer | 24a8246 | 2017-08-04 11:34:44 +0100 | [diff] [blame] | 284 | if(_log_level >= LogLevel::ERRORS && _printer != nullptr) |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 285 | { |
Moritz Pflanzer | 24a8246 | 2017-08-04 11:34:44 +0100 | [diff] [blame] | 286 | std::stringstream stream; |
| 287 | stream << "Error code: " << error.err(); |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 288 | TestError test_error(error.what(), LogLevel::ERRORS, stream.str()); |
| 289 | _printer->print_error(test_error, is_expected_failure); |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 290 | } |
| 291 | |
Moritz Pflanzer | 47752c9 | 2017-07-18 13:38:47 +0100 | [diff] [blame] | 292 | result.status = TestResult::Status::FAILED; |
| 293 | |
| 294 | if(_throw_errors) |
| 295 | { |
| 296 | throw; |
| 297 | } |
| 298 | } |
| 299 | #endif /* ARM_COMPUTE_CL */ |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 300 | catch(const std::exception &error) |
| 301 | { |
Moritz Pflanzer | 24a8246 | 2017-08-04 11:34:44 +0100 | [diff] [blame] | 302 | if(_log_level >= LogLevel::ERRORS && _printer != nullptr) |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 303 | { |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 304 | _printer->print_error(error, is_expected_failure); |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 305 | } |
| 306 | |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 307 | result.status = TestResult::Status::CRASHED; |
| 308 | |
| 309 | if(_throw_errors) |
| 310 | { |
| 311 | throw; |
| 312 | } |
| 313 | } |
| 314 | catch(...) |
| 315 | { |
Moritz Pflanzer | 24a8246 | 2017-08-04 11:34:44 +0100 | [diff] [blame] | 316 | if(_log_level >= LogLevel::ERRORS && _printer != nullptr) |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 317 | { |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 318 | _printer->print_error(TestError("Received unknown exception"), is_expected_failure); |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 319 | } |
| 320 | |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 321 | result.status = TestResult::Status::CRASHED; |
| 322 | |
| 323 | if(_throw_errors) |
| 324 | { |
| 325 | throw; |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | catch(const std::exception &error) |
| 330 | { |
Moritz Pflanzer | 24a8246 | 2017-08-04 11:34:44 +0100 | [diff] [blame] | 331 | if(_log_level >= LogLevel::ERRORS && _printer != nullptr) |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 332 | { |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 333 | _printer->print_error(error, is_expected_failure); |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 334 | } |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 335 | |
Moritz Pflanzer | e33eb64 | 2017-07-31 14:48:45 +0100 | [diff] [blame] | 336 | result.status = TestResult::Status::CRASHED; |
| 337 | |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 338 | if(_throw_errors) |
| 339 | { |
| 340 | throw; |
| 341 | } |
| 342 | } |
| 343 | catch(...) |
| 344 | { |
Moritz Pflanzer | 24a8246 | 2017-08-04 11:34:44 +0100 | [diff] [blame] | 345 | if(_log_level >= LogLevel::ERRORS && _printer != nullptr) |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 346 | { |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 347 | _printer->print_error(TestError("Received unknown exception"), is_expected_failure); |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 348 | } |
| 349 | |
Moritz Pflanzer | e1103a8 | 2017-07-18 12:20:45 +0100 | [diff] [blame] | 350 | result.status = TestResult::Status::CRASHED; |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 351 | |
| 352 | if(_throw_errors) |
| 353 | { |
| 354 | throw; |
| 355 | } |
| 356 | } |
| 357 | |
Moritz Pflanzer | 24a8246 | 2017-08-04 11:34:44 +0100 | [diff] [blame] | 358 | if(_log_level >= LogLevel::ERRORS && _printer != nullptr) |
| 359 | { |
| 360 | _printer->print_errors_footer(); |
| 361 | } |
| 362 | |
Moritz Pflanzer | e1103a8 | 2017-07-18 12:20:45 +0100 | [diff] [blame] | 363 | _current_test_result = nullptr; |
| 364 | |
Moritz Pflanzer | fa81165 | 2017-07-26 17:00:37 +0100 | [diff] [blame] | 365 | if(result.status == TestResult::Status::FAILED) |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 366 | { |
Moritz Pflanzer | fa81165 | 2017-07-26 17:00:37 +0100 | [diff] [blame] | 367 | if(info.status == TestCaseFactory::Status::EXPECTED_FAILURE) |
| 368 | { |
| 369 | result.status = TestResult::Status::EXPECTED_FAILURE; |
| 370 | } |
Moritz Pflanzer | e33eb64 | 2017-07-31 14:48:45 +0100 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | if(result.status == TestResult::Status::FAILED || result.status == TestResult::Status::CRASHED) |
| 374 | { |
| 375 | if(_stop_on_error) |
Moritz Pflanzer | fa81165 | 2017-07-26 17:00:37 +0100 | [diff] [blame] | 376 | { |
| 377 | throw std::runtime_error("Abort on first error."); |
| 378 | } |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 379 | } |
| 380 | |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 381 | result.measurements = profiler.measurements(); |
| 382 | |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 383 | set_test_result(info, result); |
| 384 | log_test_end(info); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | bool Framework::run() |
| 388 | { |
| 389 | // Clear old test results |
| 390 | _test_results.clear(); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 391 | |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 392 | if(_printer != nullptr && _log_level >= LogLevel::TESTS) |
Moritz Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 393 | { |
| 394 | _printer->print_run_header(); |
| 395 | } |
| 396 | |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 397 | const std::chrono::time_point<std::chrono::high_resolution_clock> start = std::chrono::high_resolution_clock::now(); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 398 | |
| 399 | int id = 0; |
| 400 | |
| 401 | for(auto &test_factory : _test_factories) |
| 402 | { |
| 403 | const std::string test_case_name = test_factory->name(); |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 404 | const TestInfo test_info{ id, test_case_name, test_factory->mode(), test_factory->status() }; |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 405 | |
Moritz Pflanzer | ec2de0f | 2017-07-27 14:43:46 +0100 | [diff] [blame] | 406 | if(_test_filter.is_selected(test_info)) |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 407 | { |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 408 | run_test(test_info, *test_factory); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | ++id; |
| 412 | } |
| 413 | |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 414 | const std::chrono::time_point<std::chrono::high_resolution_clock> end = std::chrono::high_resolution_clock::now(); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 415 | |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 416 | if(_printer != nullptr && _log_level >= LogLevel::TESTS) |
Moritz Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 417 | { |
| 418 | _printer->print_run_footer(); |
| 419 | } |
| 420 | |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 421 | auto runtime = std::chrono::duration_cast<std::chrono::seconds>(end - start); |
| 422 | std::map<TestResult::Status, int> results = count_test_results(); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 423 | |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 424 | if(_log_level > LogLevel::NONE) |
| 425 | { |
Moritz Pflanzer | 8df3faf | 2017-07-28 13:57:53 +0100 | [diff] [blame] | 426 | std::cout << "Executed " << _test_results.size() << " test(s) (" |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 427 | << results[TestResult::Status::SUCCESS] << " passed, " |
| 428 | << results[TestResult::Status::EXPECTED_FAILURE] << " expected failures, " |
| 429 | << results[TestResult::Status::FAILED] << " failed, " |
| 430 | << results[TestResult::Status::CRASHED] << " crashed, " |
| 431 | << results[TestResult::Status::DISABLED] << " disabled) in " << runtime.count() << " second(s)\n"; |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 432 | } |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 433 | |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 434 | int num_successful_tests = results[TestResult::Status::SUCCESS] + results[TestResult::Status::EXPECTED_FAILURE] + results[TestResult::Status::DISABLED]; |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 435 | |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 436 | return (static_cast<unsigned int>(num_successful_tests) == _test_results.size()); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 437 | } |
| 438 | |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 439 | void Framework::set_test_result(TestInfo info, TestResult result) |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 440 | { |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 441 | _test_results.emplace(std::move(info), std::move(result)); |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 442 | } |
| 443 | |
Moritz Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 444 | void Framework::print_test_results(Printer &printer) const |
| 445 | { |
| 446 | printer.print_run_header(); |
| 447 | |
| 448 | for(const auto &test : _test_results) |
| 449 | { |
| 450 | printer.print_test_header(test.first); |
| 451 | printer.print_measurements(test.second.measurements); |
| 452 | printer.print_test_footer(); |
| 453 | } |
| 454 | |
| 455 | printer.print_run_footer(); |
| 456 | } |
| 457 | |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 458 | Profiler Framework::get_profiler() const |
| 459 | { |
| 460 | Profiler profiler; |
| 461 | |
Moritz Pflanzer | 09e4f98 | 2017-08-30 12:47:06 +0100 | [diff] [blame] | 462 | const bool all_instruments = std::any_of( |
| 463 | _instruments.begin(), |
| 464 | _instruments.end(), |
| 465 | [](InstrumentType type) -> bool { return type == InstrumentType::ALL; }); |
| 466 | |
| 467 | auto is_selected = [&](InstrumentType instrument) -> bool |
| 468 | { |
| 469 | return std::find_if(_instruments.begin(), _instruments.end(), [&](InstrumentType type) -> bool { |
| 470 | const auto group = static_cast<InstrumentType>(static_cast<uint64_t>(type) & 0xFF00); |
| 471 | return group == instrument; |
| 472 | }) |
| 473 | != _instruments.end(); |
| 474 | }; |
| 475 | |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 476 | for(const auto &instrument : _available_instruments) |
| 477 | { |
Moritz Pflanzer | 09e4f98 | 2017-08-30 12:47:06 +0100 | [diff] [blame] | 478 | if(all_instruments || is_selected(instrument.first)) |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 479 | { |
| 480 | profiler.add(instrument.second()); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | return profiler; |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 485 | } |
| 486 | |
Moritz Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 487 | void Framework::set_printer(Printer *printer) |
| 488 | { |
| 489 | _printer = printer; |
| 490 | } |
| 491 | |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 492 | std::vector<TestInfo> Framework::test_infos() const |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 493 | { |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 494 | std::vector<TestInfo> ids; |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 495 | |
| 496 | int id = 0; |
| 497 | |
| 498 | for(const auto &factory : _test_factories) |
| 499 | { |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 500 | TestInfo test_info{ id, factory->name(), factory->mode(), factory->status() }; |
| 501 | |
Moritz Pflanzer | ec2de0f | 2017-07-27 14:43:46 +0100 | [diff] [blame] | 502 | if(_test_filter.is_selected(test_info)) |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 503 | { |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 504 | ids.emplace_back(std::move(test_info)); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | ++id; |
| 508 | } |
| 509 | |
| 510 | return ids; |
| 511 | } |
steniu01 | 172c58d | 2017-08-31 13:49:08 +0100 | [diff] [blame] | 512 | |
| 513 | LogLevel Framework::log_level() const |
| 514 | { |
| 515 | return _log_level; |
| 516 | } |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 517 | } // namespace framework |
| 518 | } // namespace test |
| 519 | } // namespace arm_compute |