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 | |
| 26 | #include "Exceptions.h" |
| 27 | #include "support/ToolchainSupport.h" |
| 28 | |
Moritz Pflanzer | 47752c9 | 2017-07-18 13:38:47 +0100 | [diff] [blame] | 29 | #ifdef ARM_COMPUTE_CL |
| 30 | #include "arm_compute/core/CL/OpenCL.h" |
| 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 |
| 48 | _available_instruments.emplace(InstrumentType::PMU_CYCLE_COUNTER, Instrument::make_instrument<CycleCounter>); |
| 49 | _available_instruments.emplace(InstrumentType::PMU_INSTRUCTION_COUNTER, Instrument::make_instrument<InstructionCounter>); |
| 50 | #endif /* PMU_ENABLED */ |
| 51 | } |
| 52 | |
| 53 | std::set<InstrumentType> Framework::available_instruments() const |
| 54 | { |
| 55 | std::set<InstrumentType> types; |
| 56 | |
| 57 | for(const auto &instrument : _available_instruments) |
| 58 | { |
| 59 | types.emplace(instrument.first); |
| 60 | } |
| 61 | |
| 62 | return types; |
| 63 | } |
| 64 | |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 65 | std::tuple<int, int, int> Framework::count_test_results() const |
| 66 | { |
| 67 | int passed = 0; |
| 68 | int failed = 0; |
| 69 | int crashed = 0; |
| 70 | |
| 71 | for(const auto &test : _test_results) |
| 72 | { |
| 73 | switch(test.second.status) |
| 74 | { |
| 75 | case TestResult::Status::SUCCESS: |
| 76 | ++passed; |
| 77 | break; |
| 78 | case TestResult::Status::FAILED: |
| 79 | ++failed; |
| 80 | break; |
| 81 | case TestResult::Status::CRASHED: |
| 82 | ++crashed; |
| 83 | break; |
| 84 | default: |
| 85 | // Do nothing |
| 86 | break; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return std::make_tuple(passed, failed, crashed); |
| 91 | } |
| 92 | |
| 93 | Framework &Framework::get() |
| 94 | { |
| 95 | static Framework instance; |
| 96 | return instance; |
| 97 | } |
| 98 | |
Moritz Pflanzer | d03b00a | 2017-07-17 13:50:12 +0100 | [diff] [blame] | 99 | void Framework::init(const std::vector<InstrumentType> &instruments, int num_iterations, DatasetMode mode, const std::string &name_filter, const std::string &id_filter) |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 100 | { |
| 101 | _test_name_filter = std::regex{ name_filter }; |
| 102 | _test_id_filter = std::regex{ id_filter }; |
| 103 | _num_iterations = num_iterations; |
Moritz Pflanzer | d03b00a | 2017-07-17 13:50:12 +0100 | [diff] [blame] | 104 | _dataset_mode = mode; |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 105 | |
| 106 | _instruments = InstrumentType::NONE; |
| 107 | |
| 108 | for(const auto &instrument : instruments) |
| 109 | { |
| 110 | _instruments |= instrument; |
| 111 | } |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | std::string Framework::current_suite_name() const |
| 115 | { |
| 116 | return join(_test_suite_name.cbegin(), _test_suite_name.cend(), "/"); |
| 117 | } |
| 118 | |
| 119 | void Framework::push_suite(std::string name) |
| 120 | { |
| 121 | _test_suite_name.emplace_back(std::move(name)); |
| 122 | } |
| 123 | |
| 124 | void Framework::pop_suite() |
| 125 | { |
| 126 | _test_suite_name.pop_back(); |
| 127 | } |
| 128 | |
| 129 | void Framework::log_test_start(const std::string &test_name) |
| 130 | { |
Moritz Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 131 | if(_printer != nullptr) |
| 132 | { |
| 133 | _printer->print_test_header(test_name); |
| 134 | } |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void Framework::log_test_skipped(const std::string &test_name) |
| 138 | { |
| 139 | static_cast<void>(test_name); |
| 140 | } |
| 141 | |
| 142 | void Framework::log_test_end(const std::string &test_name) |
| 143 | { |
Moritz Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 144 | if(_printer != nullptr) |
| 145 | { |
| 146 | _printer->print_measurements(_test_results.at(test_name).measurements); |
| 147 | _printer->print_test_footer(); |
| 148 | } |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void Framework::log_failed_expectation(const std::string &msg) |
| 152 | { |
| 153 | std::cerr << "ERROR: " << msg << "\n"; |
Moritz Pflanzer | e1103a8 | 2017-07-18 12:20:45 +0100 | [diff] [blame] | 154 | |
| 155 | if(_current_test_result != nullptr) |
| 156 | { |
| 157 | _current_test_result->status = TestResult::Status::FAILED; |
| 158 | } |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | int Framework::num_iterations() const |
| 162 | { |
| 163 | return _num_iterations; |
| 164 | } |
| 165 | |
| 166 | void Framework::set_num_iterations(int num_iterations) |
| 167 | { |
| 168 | _num_iterations = num_iterations; |
| 169 | } |
| 170 | |
| 171 | void Framework::set_throw_errors(bool throw_errors) |
| 172 | { |
| 173 | _throw_errors = throw_errors; |
| 174 | } |
| 175 | |
| 176 | bool Framework::throw_errors() const |
| 177 | { |
| 178 | return _throw_errors; |
| 179 | } |
| 180 | |
| 181 | bool Framework::is_enabled(const TestId &id) const |
| 182 | { |
Moritz Pflanzer | d03b00a | 2017-07-17 13:50:12 +0100 | [diff] [blame] | 183 | int test_id = 0; |
| 184 | std::string name; |
| 185 | DatasetMode mode = DatasetMode::ALL; |
| 186 | std::tie(test_id, name, mode) = id; |
| 187 | |
| 188 | if((mode & _dataset_mode) == DatasetMode::DISABLED) |
| 189 | { |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | if(!std::regex_search(support::cpp11::to_string(test_id), _test_id_filter)) |
| 194 | { |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | if(!std::regex_search(name, _test_name_filter)) |
| 199 | { |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | return true; |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | void Framework::run_test(TestCaseFactory &test_factory) |
| 207 | { |
| 208 | const std::string test_case_name = test_factory.name(); |
| 209 | |
| 210 | log_test_start(test_case_name); |
| 211 | |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 212 | Profiler profiler = get_profiler(); |
Moritz Pflanzer | e1103a8 | 2017-07-18 12:20:45 +0100 | [diff] [blame] | 213 | TestResult result(TestResult::Status::SUCCESS); |
| 214 | |
| 215 | _current_test_result = &result; |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 216 | |
| 217 | try |
| 218 | { |
| 219 | std::unique_ptr<TestCase> test_case = test_factory.make(); |
| 220 | |
| 221 | try |
| 222 | { |
| 223 | test_case->do_setup(); |
| 224 | |
| 225 | for(int i = 0; i < _num_iterations; ++i) |
| 226 | { |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 227 | profiler.start(); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 228 | test_case->do_run(); |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 229 | profiler.stop(); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | test_case->do_teardown(); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 233 | } |
| 234 | catch(const TestError &error) |
| 235 | { |
| 236 | std::cerr << "FATAL ERROR: " << error.what() << "\n"; |
| 237 | result.status = TestResult::Status::FAILED; |
| 238 | |
| 239 | if(_throw_errors) |
| 240 | { |
| 241 | throw; |
| 242 | } |
| 243 | } |
Moritz Pflanzer | 47752c9 | 2017-07-18 13:38:47 +0100 | [diff] [blame] | 244 | #ifdef ARM_COMPUTE_CL |
| 245 | catch(const ::cl::Error &error) |
| 246 | { |
| 247 | std::cerr << "FATAL CL ERROR: " << error.what() << " with code " << error.err() << "\n"; |
| 248 | result.status = TestResult::Status::FAILED; |
| 249 | |
| 250 | if(_throw_errors) |
| 251 | { |
| 252 | throw; |
| 253 | } |
| 254 | } |
| 255 | #endif /* ARM_COMPUTE_CL */ |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 256 | catch(const std::exception &error) |
| 257 | { |
| 258 | std::cerr << "FATAL ERROR: Received unhandled error: '" << error.what() << "'\n"; |
| 259 | result.status = TestResult::Status::CRASHED; |
| 260 | |
| 261 | if(_throw_errors) |
| 262 | { |
| 263 | throw; |
| 264 | } |
| 265 | } |
| 266 | catch(...) |
| 267 | { |
| 268 | std::cerr << "FATAL ERROR: Received unhandled exception\n"; |
| 269 | result.status = TestResult::Status::CRASHED; |
| 270 | |
| 271 | if(_throw_errors) |
| 272 | { |
| 273 | throw; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | catch(const std::exception &error) |
| 278 | { |
| 279 | std::cerr << "FATAL ERROR: Received unhandled error during fixture creation: '" << error.what() << "'\n"; |
| 280 | |
| 281 | if(_throw_errors) |
| 282 | { |
| 283 | throw; |
| 284 | } |
| 285 | } |
| 286 | catch(...) |
| 287 | { |
| 288 | std::cerr << "FATAL ERROR: Received unhandled exception during fixture creation\n"; |
Moritz Pflanzer | e1103a8 | 2017-07-18 12:20:45 +0100 | [diff] [blame] | 289 | result.status = TestResult::Status::CRASHED; |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 290 | |
| 291 | if(_throw_errors) |
| 292 | { |
| 293 | throw; |
| 294 | } |
| 295 | } |
| 296 | |
Moritz Pflanzer | e1103a8 | 2017-07-18 12:20:45 +0100 | [diff] [blame] | 297 | _current_test_result = nullptr; |
| 298 | |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 299 | result.measurements = profiler.measurements(); |
| 300 | |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 301 | set_test_result(test_case_name, result); |
| 302 | log_test_end(test_case_name); |
| 303 | } |
| 304 | |
| 305 | bool Framework::run() |
| 306 | { |
| 307 | // Clear old test results |
| 308 | _test_results.clear(); |
| 309 | _runtime = std::chrono::seconds{ 0 }; |
| 310 | |
Moritz Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 311 | if(_printer != nullptr) |
| 312 | { |
| 313 | _printer->print_run_header(); |
| 314 | } |
| 315 | |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 316 | const auto start = std::chrono::high_resolution_clock::now(); |
| 317 | |
| 318 | int id = 0; |
| 319 | |
| 320 | for(auto &test_factory : _test_factories) |
| 321 | { |
| 322 | const std::string test_case_name = test_factory->name(); |
| 323 | |
Moritz Pflanzer | d03b00a | 2017-07-17 13:50:12 +0100 | [diff] [blame] | 324 | if(!is_enabled(TestId(id, test_case_name, test_factory->mode()))) |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 325 | { |
| 326 | log_test_skipped(test_case_name); |
| 327 | } |
| 328 | else |
| 329 | { |
| 330 | run_test(*test_factory); |
| 331 | } |
| 332 | |
| 333 | ++id; |
| 334 | } |
| 335 | |
| 336 | const auto end = std::chrono::high_resolution_clock::now(); |
| 337 | |
Moritz Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 338 | if(_printer != nullptr) |
| 339 | { |
| 340 | _printer->print_run_footer(); |
| 341 | } |
| 342 | |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 343 | _runtime = std::chrono::duration_cast<std::chrono::seconds>(end - start); |
| 344 | |
| 345 | int passed = 0; |
| 346 | int failed = 0; |
| 347 | int crashed = 0; |
| 348 | |
| 349 | std::tie(passed, failed, crashed) = count_test_results(); |
| 350 | |
| 351 | std::cout << "Executed " << _test_results.size() << " test(s) (" << passed << " passed, " << failed << " failed, " << crashed << " crashed) in " << _runtime.count() << " second(s)\n"; |
| 352 | |
| 353 | return (static_cast<unsigned int>(passed) == _test_results.size()); |
| 354 | } |
| 355 | |
| 356 | void Framework::set_test_result(std::string test_case_name, TestResult result) |
| 357 | { |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 358 | _test_results.emplace(std::move(test_case_name), std::move(result)); |
| 359 | } |
| 360 | |
Moritz Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 361 | void Framework::print_test_results(Printer &printer) const |
| 362 | { |
| 363 | printer.print_run_header(); |
| 364 | |
| 365 | for(const auto &test : _test_results) |
| 366 | { |
| 367 | printer.print_test_header(test.first); |
| 368 | printer.print_measurements(test.second.measurements); |
| 369 | printer.print_test_footer(); |
| 370 | } |
| 371 | |
| 372 | printer.print_run_footer(); |
| 373 | } |
| 374 | |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 375 | Profiler Framework::get_profiler() const |
| 376 | { |
| 377 | Profiler profiler; |
| 378 | |
| 379 | for(const auto &instrument : _available_instruments) |
| 380 | { |
| 381 | if((instrument.first & _instruments) != InstrumentType::NONE) |
| 382 | { |
| 383 | profiler.add(instrument.second()); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | return profiler; |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 388 | } |
| 389 | |
Moritz Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 390 | void Framework::set_printer(Printer *printer) |
| 391 | { |
| 392 | _printer = printer; |
| 393 | } |
| 394 | |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 395 | std::vector<Framework::TestId> Framework::test_ids() const |
| 396 | { |
| 397 | std::vector<TestId> ids; |
| 398 | |
| 399 | int id = 0; |
| 400 | |
| 401 | for(const auto &factory : _test_factories) |
| 402 | { |
Moritz Pflanzer | d03b00a | 2017-07-17 13:50:12 +0100 | [diff] [blame] | 403 | if(is_enabled(TestId(id, factory->name(), factory->mode()))) |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 404 | { |
Moritz Pflanzer | d03b00a | 2017-07-17 13:50:12 +0100 | [diff] [blame] | 405 | ids.emplace_back(id, factory->name(), factory->mode()); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | ++id; |
| 409 | } |
| 410 | |
| 411 | return ids; |
| 412 | } |
| 413 | } // namespace framework |
| 414 | } // namespace test |
| 415 | } // namespace arm_compute |