blob: d1fb28d50171af79c74e7c881458c4ce4d447f0e [file] [log] [blame]
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +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 "Framework.h"
25
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010026#include "support/ToolchainSupport.h"
27
Moritz Pflanzer47752c92017-07-18 13:38:47 +010028#ifdef ARM_COMPUTE_CL
29#include "arm_compute/core/CL/OpenCL.h"
Anthony Barbierbf959222017-07-19 17:01:42 +010030#include "arm_compute/runtime/CL/CLScheduler.h"
Moritz Pflanzer47752c92017-07-18 13:38:47 +010031#endif /* ARM_COMPUTE_CL */
32
Anthony Barbier7068f992017-10-26 15:23:08 +010033#ifdef ARM_COMPUTE_GC
34#include "arm_compute/core/GLES_COMPUTE/OpenGLES.h"
35#include "arm_compute/runtime/GLES_COMPUTE/GCScheduler.h"
36#endif /* ARM_COMPUTE_GC */
37
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010038#include <chrono>
39#include <iostream>
40#include <sstream>
41#include <type_traits>
42
43namespace arm_compute
44{
45namespace test
46{
47namespace framework
48{
Moritz Pflanzera4f711b2017-07-05 11:02:23 +010049Framework::Framework()
50{
Giorgio Arenace58a9f2017-10-31 17:59:17 +000051 _available_instruments.emplace(std::pair<InstrumentType, ScaleFactor>(InstrumentType::WALL_CLOCK_TIMER, ScaleFactor::NONE), Instrument::make_instrument<WallClockTimer, ScaleFactor::NONE>);
52 _available_instruments.emplace(std::pair<InstrumentType, ScaleFactor>(InstrumentType::WALL_CLOCK_TIMER, ScaleFactor::TIME_MS), Instrument::make_instrument<WallClockTimer, ScaleFactor::TIME_MS>);
53 _available_instruments.emplace(std::pair<InstrumentType, ScaleFactor>(InstrumentType::WALL_CLOCK_TIMER, ScaleFactor::TIME_S), Instrument::make_instrument<WallClockTimer, ScaleFactor::TIME_S>);
Moritz Pflanzera4f711b2017-07-05 11:02:23 +010054#ifdef PMU_ENABLED
Giorgio Arenace58a9f2017-10-31 17:59:17 +000055 _available_instruments.emplace(std::pair<InstrumentType, ScaleFactor>(InstrumentType::PMU, ScaleFactor::NONE), Instrument::make_instrument<PMUCounter, ScaleFactor::NONE>);
56 _available_instruments.emplace(std::pair<InstrumentType, ScaleFactor>(InstrumentType::PMU, ScaleFactor::SCALE_1K), Instrument::make_instrument<PMUCounter, ScaleFactor::SCALE_1K>);
57 _available_instruments.emplace(std::pair<InstrumentType, ScaleFactor>(InstrumentType::PMU, ScaleFactor::SCALE_1M), Instrument::make_instrument<PMUCounter, ScaleFactor::SCALE_1M>);
Moritz Pflanzera4f711b2017-07-05 11:02:23 +010058#endif /* PMU_ENABLED */
Moritz Pflanzer45634b42017-08-30 12:48:18 +010059#ifdef MALI_ENABLED
Giorgio Arenace58a9f2017-10-31 17:59:17 +000060 _available_instruments.emplace(std::pair<InstrumentType, ScaleFactor>(InstrumentType::MALI, ScaleFactor::NONE), Instrument::make_instrument<MaliCounter, ScaleFactor::NONE>);
61 _available_instruments.emplace(std::pair<InstrumentType, ScaleFactor>(InstrumentType::MALI, ScaleFactor::SCALE_1K), Instrument::make_instrument<MaliCounter, ScaleFactor::SCALE_1K>);
62 _available_instruments.emplace(std::pair<InstrumentType, ScaleFactor>(InstrumentType::MALI, ScaleFactor::SCALE_1M), Instrument::make_instrument<MaliCounter, ScaleFactor::SCALE_1M>);
Moritz Pflanzer45634b42017-08-30 12:48:18 +010063#endif /* MALI_ENABLED */
Anthony Barbiere8895f82017-11-23 16:58:52 +000064#ifdef ARM_COMPUTE_CL
Giorgio Arenace58a9f2017-10-31 17:59:17 +000065 _available_instruments.emplace(std::pair<InstrumentType, ScaleFactor>(InstrumentType::OPENCL_TIMER, ScaleFactor::NONE), Instrument::make_instrument<OpenCLTimer, ScaleFactor::NONE>);
66 _available_instruments.emplace(std::pair<InstrumentType, ScaleFactor>(InstrumentType::OPENCL_TIMER, ScaleFactor::TIME_US), Instrument::make_instrument<OpenCLTimer, ScaleFactor::TIME_US>);
67 _available_instruments.emplace(std::pair<InstrumentType, ScaleFactor>(InstrumentType::OPENCL_TIMER, ScaleFactor::TIME_MS), Instrument::make_instrument<OpenCLTimer, ScaleFactor::TIME_MS>);
68 _available_instruments.emplace(std::pair<InstrumentType, ScaleFactor>(InstrumentType::OPENCL_TIMER, ScaleFactor::TIME_S), Instrument::make_instrument<OpenCLTimer, ScaleFactor::TIME_S>);
Anthony Barbiere8895f82017-11-23 16:58:52 +000069#endif /* ARM_COMPUTE_CL */
Moritz Pflanzera4f711b2017-07-05 11:02:23 +010070}
71
Giorgio Arenace58a9f2017-10-31 17:59:17 +000072std::set<InstrumentsDescription> Framework::available_instruments() const
Moritz Pflanzera4f711b2017-07-05 11:02:23 +010073{
Giorgio Arenace58a9f2017-10-31 17:59:17 +000074 std::set<InstrumentsDescription> types;
Moritz Pflanzera4f711b2017-07-05 11:02:23 +010075
76 for(const auto &instrument : _available_instruments)
77 {
78 types.emplace(instrument.first);
79 }
80
81 return types;
82}
83
Moritz Pflanzerbf234e02017-07-24 15:04:14 +010084std::map<TestResult::Status, int> Framework::count_test_results() const
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010085{
Moritz Pflanzerbf234e02017-07-24 15:04:14 +010086 std::map<TestResult::Status, int> counts;
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010087
88 for(const auto &test : _test_results)
89 {
Moritz Pflanzerbf234e02017-07-24 15:04:14 +010090 ++counts[test.second.status];
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010091 }
92
Moritz Pflanzerbf234e02017-07-24 15:04:14 +010093 return counts;
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010094}
95
96Framework &Framework::get()
97{
98 static Framework instance;
99 return instance;
100}
101
Giorgio Arenace58a9f2017-10-31 17:59:17 +0000102void Framework::init(const std::vector<framework::InstrumentsDescription> &instruments, int num_iterations, DatasetMode mode, const std::string &name_filter, const std::string &id_filter,
103 LogLevel log_level)
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100104{
Moritz Pflanzerec2de0f2017-07-27 14:43:46 +0100105 _test_filter = TestFilter(mode, name_filter, id_filter);
106 _num_iterations = num_iterations;
107 _log_level = log_level;
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100108
Giorgio Arenace58a9f2017-10-31 17:59:17 +0000109 _instruments = std::set<framework::InstrumentsDescription>(instruments.begin(), instruments.end());
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100110}
111
112std::string Framework::current_suite_name() const
113{
114 return join(_test_suite_name.cbegin(), _test_suite_name.cend(), "/");
115}
116
117void Framework::push_suite(std::string name)
118{
119 _test_suite_name.emplace_back(std::move(name));
120}
121
122void Framework::pop_suite()
123{
124 _test_suite_name.pop_back();
125}
126
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100127void Framework::add_test_info(std::string info)
128{
129 _test_info.emplace_back(std::move(info));
130}
131
132void Framework::clear_test_info()
133{
134 _test_info.clear();
135}
136
137bool Framework::has_test_info() const
138{
139 return !_test_info.empty();
140}
141
142void Framework::print_test_info(std::ostream &os) const
143{
Moritz Pflanzer8df3faf2017-07-28 13:57:53 +0100144 if(!_test_info.empty())
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100145 {
Moritz Pflanzer8df3faf2017-07-28 13:57:53 +0100146 os << "CONTEXT:\n";
147
148 for(const auto &str : _test_info)
149 {
150 os << " " << str << "\n";
151 }
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100152 }
153}
154
Giorgio Arena2d099932017-10-25 15:47:08 +0100155template <typename F>
156void Framework::func_on_all_printers(F &&func)
157{
158 std::for_each(std::begin(_printers), std::end(_printers), func);
159}
160
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100161void Framework::log_test_start(const TestInfo &info)
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100162{
Giorgio Arena2d099932017-10-25 15:47:08 +0100163 if(_log_level >= LogLevel::TESTS)
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100164 {
Giorgio Arena2d099932017-10-25 15:47:08 +0100165 func_on_all_printers([&](Printer * p)
166 {
167 p->print_test_header(info);
168 });
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100169 }
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100170}
171
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100172void Framework::log_test_skipped(const TestInfo &info)
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100173{
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100174 static_cast<void>(info);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100175}
176
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100177void Framework::log_test_end(const TestInfo &info)
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100178{
Giorgio Arena2d099932017-10-25 15:47:08 +0100179 if(_log_level >= LogLevel::MEASUREMENTS)
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100180 {
Giorgio Arena2d099932017-10-25 15:47:08 +0100181 func_on_all_printers([&](Printer * p)
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100182 {
Giorgio Arena2d099932017-10-25 15:47:08 +0100183 p->print_measurements(_test_results.at(info).measurements);
184 });
185 }
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100186
Giorgio Arena2d099932017-10-25 15:47:08 +0100187 if(_log_level >= LogLevel::TESTS)
188 {
189 func_on_all_printers([](Printer * p)
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100190 {
Giorgio Arena2d099932017-10-25 15:47:08 +0100191 p->print_test_footer();
192 });
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100193 }
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100194}
195
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100196void Framework::log_failed_expectation(const TestError &error)
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100197{
Moritz Pflanzer0ce08442017-09-16 11:11:27 +0100198 ARM_COMPUTE_ERROR_ON(_current_test_info == nullptr);
199 ARM_COMPUTE_ERROR_ON(_current_test_result == nullptr);
200
201 const bool is_expected_failure = _current_test_info->status == TestCaseFactory::Status::EXPECTED_FAILURE;
202
Giorgio Arena2d099932017-10-25 15:47:08 +0100203 if(_log_level >= error.level())
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100204 {
Giorgio Arena2d099932017-10-25 15:47:08 +0100205 func_on_all_printers([&](Printer * p)
206 {
207 p->print_error(error, is_expected_failure);
208 });
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100209 }
Moritz Pflanzere1103a82017-07-18 12:20:45 +0100210
Moritz Pflanzer0ce08442017-09-16 11:11:27 +0100211 _current_test_result->status = TestResult::Status::FAILED;
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100212}
213
steniu01172c58d2017-08-31 13:49:08 +0100214void Framework::log_info(const std::string &info)
215{
Giorgio Arena2d099932017-10-25 15:47:08 +0100216 if(_log_level >= LogLevel::DEBUG)
steniu01172c58d2017-08-31 13:49:08 +0100217 {
Giorgio Arena2d099932017-10-25 15:47:08 +0100218 func_on_all_printers([&](Printer * p)
219 {
220 p->print_info(info);
221 });
steniu01172c58d2017-08-31 13:49:08 +0100222 }
223}
224
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100225int Framework::num_iterations() const
226{
227 return _num_iterations;
228}
229
230void Framework::set_num_iterations(int num_iterations)
231{
232 _num_iterations = num_iterations;
233}
234
235void Framework::set_throw_errors(bool throw_errors)
236{
237 _throw_errors = throw_errors;
238}
239
240bool Framework::throw_errors() const
241{
242 return _throw_errors;
243}
244
Moritz Pflanzerfa811652017-07-26 17:00:37 +0100245void Framework::set_stop_on_error(bool stop_on_error)
246{
247 _stop_on_error = stop_on_error;
248}
249
250bool Framework::stop_on_error() const
251{
252 return _stop_on_error;
253}
254
Anthony Barbierf6705ec2017-09-28 12:01:10 +0100255void Framework::set_error_on_missing_assets(bool error_on_missing_assets)
256{
257 _error_on_missing_assets = error_on_missing_assets;
258}
259
260bool Framework::error_on_missing_assets() const
261{
262 return _error_on_missing_assets;
263}
264
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100265void Framework::run_test(const TestInfo &info, TestCaseFactory &test_factory)
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100266{
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100267 if(test_factory.status() == TestCaseFactory::Status::DISABLED)
268 {
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100269 log_test_skipped(info);
270 set_test_result(info, TestResult(TestResult::Status::DISABLED));
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100271 return;
272 }
273
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100274 log_test_start(info);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100275
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100276 Profiler profiler = get_profiler();
Moritz Pflanzere33eb642017-07-31 14:48:45 +0100277 TestResult result(TestResult::Status::NOT_RUN);
Moritz Pflanzere1103a82017-07-18 12:20:45 +0100278
Moritz Pflanzer0ce08442017-09-16 11:11:27 +0100279 _current_test_info = &info;
Moritz Pflanzere1103a82017-07-18 12:20:45 +0100280 _current_test_result = &result;
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100281
Giorgio Arena2d099932017-10-25 15:47:08 +0100282 if(_log_level >= LogLevel::ERRORS)
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100283 {
Giorgio Arena2d099932017-10-25 15:47:08 +0100284 func_on_all_printers([](Printer * p)
285 {
286 p->print_errors_header();
287 });
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100288 }
289
Moritz Pflanzer0ce08442017-09-16 11:11:27 +0100290 const bool is_expected_failure = info.status == TestCaseFactory::Status::EXPECTED_FAILURE;
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100291
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100292 try
293 {
294 std::unique_ptr<TestCase> test_case = test_factory.make();
295
296 try
297 {
298 test_case->do_setup();
299
300 for(int i = 0; i < _num_iterations; ++i)
301 {
Anthony Barbier61941452017-11-21 17:49:07 +0000302 //Start the profiler if:
303 //- there is only one iteration
304 //- it's not the first iteration of a multi-iterations run.
305 //
306 //Reason: if the CLTuner is enabled then the first run will be really messy
307 //as each kernel will be executed several times, messing up the instruments like OpenCL timers.
308 if(_num_iterations == 1 || i != 0)
Anthony Barbier2f8e0772017-11-15 13:06:46 +0000309 {
310 profiler.start();
311 }
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100312 test_case->do_run();
Anthony Barbierbf959222017-07-19 17:01:42 +0100313#ifdef ARM_COMPUTE_CL
314 if(opencl_is_available())
315 {
316 CLScheduler::get().sync();
317 }
318#endif /* ARM_COMPUTE_CL */
Anthony Barbier7068f992017-10-26 15:23:08 +0100319#ifdef ARM_COMPUTE_GC
320 if(opengles31_is_available())
321 {
322 GCScheduler::get().sync();
323 }
324#endif /* ARM_COMPUTE_GC */
Anthony Barbier61941452017-11-21 17:49:07 +0000325 if(_num_iterations == 1 || i != 0)
Anthony Barbier2f8e0772017-11-15 13:06:46 +0000326 {
327 profiler.stop();
328 }
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100329 }
330
331 test_case->do_teardown();
Moritz Pflanzere33eb642017-07-31 14:48:45 +0100332
333 // Change status to success if no error has happend
334 if(result.status == TestResult::Status::NOT_RUN)
335 {
336 result.status = TestResult::Status::SUCCESS;
337 }
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100338 }
Anthony Barbierf6705ec2017-09-28 12:01:10 +0100339 catch(const FileNotFound &error)
340 {
341 if(_error_on_missing_assets)
342 {
Giorgio Arena2d099932017-10-25 15:47:08 +0100343 if(_log_level >= LogLevel::ERRORS)
Anthony Barbierf6705ec2017-09-28 12:01:10 +0100344 {
345 TestError test_error(error.what(), LogLevel::ERRORS);
Giorgio Arena2d099932017-10-25 15:47:08 +0100346 func_on_all_printers([&](Printer * p)
347 {
348 p->print_error(test_error, is_expected_failure);
349 });
Anthony Barbierf6705ec2017-09-28 12:01:10 +0100350 }
351
352 result.status = TestResult::Status::FAILED;
353
354 if(_throw_errors)
355 {
356 throw;
357 }
358 }
359 else
360 {
Giorgio Arena2d099932017-10-25 15:47:08 +0100361 if(_log_level >= LogLevel::DEBUG)
Anthony Barbierf6705ec2017-09-28 12:01:10 +0100362 {
Giorgio Arena2d099932017-10-25 15:47:08 +0100363 func_on_all_printers([&](Printer * p)
364 {
365 p->print_info(error.what());
366 });
Anthony Barbierf6705ec2017-09-28 12:01:10 +0100367 }
368
369 result.status = TestResult::Status::NOT_RUN;
370 }
371 }
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100372 catch(const TestError &error)
373 {
Giorgio Arena2d099932017-10-25 15:47:08 +0100374 if(_log_level >= error.level())
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100375 {
Giorgio Arena2d099932017-10-25 15:47:08 +0100376 func_on_all_printers([&](Printer * p)
377 {
378 p->print_error(error, is_expected_failure);
379 });
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100380 }
381
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100382 result.status = TestResult::Status::FAILED;
383
384 if(_throw_errors)
385 {
386 throw;
387 }
388 }
Moritz Pflanzer47752c92017-07-18 13:38:47 +0100389#ifdef ARM_COMPUTE_CL
390 catch(const ::cl::Error &error)
391 {
Giorgio Arena2d099932017-10-25 15:47:08 +0100392 if(_log_level >= LogLevel::ERRORS)
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100393 {
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100394 std::stringstream stream;
395 stream << "Error code: " << error.err();
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100396 TestError test_error(error.what(), LogLevel::ERRORS, stream.str());
Giorgio Arena2d099932017-10-25 15:47:08 +0100397 func_on_all_printers([&](Printer * p)
398 {
399 p->print_error(test_error, is_expected_failure);
400 });
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100401 }
402
Moritz Pflanzer47752c92017-07-18 13:38:47 +0100403 result.status = TestResult::Status::FAILED;
404
405 if(_throw_errors)
406 {
407 throw;
408 }
409 }
410#endif /* ARM_COMPUTE_CL */
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100411 catch(const std::exception &error)
412 {
Giorgio Arena2d099932017-10-25 15:47:08 +0100413 if(_log_level >= LogLevel::ERRORS)
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100414 {
Giorgio Arena2d099932017-10-25 15:47:08 +0100415 func_on_all_printers([&](Printer * p)
416 {
417 p->print_error(error, is_expected_failure);
418 });
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100419 }
420
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100421 result.status = TestResult::Status::CRASHED;
422
423 if(_throw_errors)
424 {
425 throw;
426 }
427 }
428 catch(...)
429 {
Giorgio Arena2d099932017-10-25 15:47:08 +0100430 if(_log_level >= LogLevel::ERRORS)
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100431 {
Giorgio Arena2d099932017-10-25 15:47:08 +0100432 func_on_all_printers([&](Printer * p)
433 {
434 p->print_error(TestError("Received unknown exception"), is_expected_failure);
435 });
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100436 }
437
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100438 result.status = TestResult::Status::CRASHED;
439
440 if(_throw_errors)
441 {
442 throw;
443 }
444 }
445 }
446 catch(const std::exception &error)
447 {
Giorgio Arena2d099932017-10-25 15:47:08 +0100448 if(_log_level >= LogLevel::ERRORS)
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100449 {
Giorgio Arena2d099932017-10-25 15:47:08 +0100450 func_on_all_printers([&](Printer * p)
451 {
452 p->print_error(error, is_expected_failure);
453 });
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100454 }
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100455
Moritz Pflanzere33eb642017-07-31 14:48:45 +0100456 result.status = TestResult::Status::CRASHED;
457
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100458 if(_throw_errors)
459 {
460 throw;
461 }
462 }
463 catch(...)
464 {
Giorgio Arena2d099932017-10-25 15:47:08 +0100465 if(_log_level >= LogLevel::ERRORS)
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100466 {
Giorgio Arena2d099932017-10-25 15:47:08 +0100467 func_on_all_printers([&](Printer * p)
468 {
469 p->print_error(TestError("Received unknown exception"), is_expected_failure);
470 });
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100471 }
472
Moritz Pflanzere1103a82017-07-18 12:20:45 +0100473 result.status = TestResult::Status::CRASHED;
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100474
475 if(_throw_errors)
476 {
477 throw;
478 }
479 }
480
Giorgio Arena2d099932017-10-25 15:47:08 +0100481 if(_log_level >= LogLevel::ERRORS)
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100482 {
Giorgio Arena2d099932017-10-25 15:47:08 +0100483 func_on_all_printers([](Printer * p)
484 {
485 p->print_errors_footer();
486 });
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100487 }
488
Moritz Pflanzer0ce08442017-09-16 11:11:27 +0100489 _current_test_info = nullptr;
Moritz Pflanzere1103a82017-07-18 12:20:45 +0100490 _current_test_result = nullptr;
491
Moritz Pflanzerfa811652017-07-26 17:00:37 +0100492 if(result.status == TestResult::Status::FAILED)
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100493 {
Moritz Pflanzerfa811652017-07-26 17:00:37 +0100494 if(info.status == TestCaseFactory::Status::EXPECTED_FAILURE)
495 {
496 result.status = TestResult::Status::EXPECTED_FAILURE;
497 }
Moritz Pflanzere33eb642017-07-31 14:48:45 +0100498 }
499
500 if(result.status == TestResult::Status::FAILED || result.status == TestResult::Status::CRASHED)
501 {
502 if(_stop_on_error)
Moritz Pflanzerfa811652017-07-26 17:00:37 +0100503 {
504 throw std::runtime_error("Abort on first error.");
505 }
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100506 }
507
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100508 result.measurements = profiler.measurements();
509
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100510 set_test_result(info, result);
511 log_test_end(info);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100512}
513
514bool Framework::run()
515{
516 // Clear old test results
517 _test_results.clear();
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100518
Giorgio Arena2d099932017-10-25 15:47:08 +0100519 if(_log_level >= LogLevel::TESTS)
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100520 {
Giorgio Arena2d099932017-10-25 15:47:08 +0100521 func_on_all_printers([](Printer * p)
522 {
523 p->print_run_header();
524 });
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100525 }
526
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100527 const std::chrono::time_point<std::chrono::high_resolution_clock> start = std::chrono::high_resolution_clock::now();
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100528
529 int id = 0;
530
531 for(auto &test_factory : _test_factories)
532 {
533 const std::string test_case_name = test_factory->name();
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100534 const TestInfo test_info{ id, test_case_name, test_factory->mode(), test_factory->status() };
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100535
Moritz Pflanzerec2de0f2017-07-27 14:43:46 +0100536 if(_test_filter.is_selected(test_info))
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100537 {
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100538 run_test(test_info, *test_factory);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100539 }
540
541 ++id;
542 }
543
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100544 const std::chrono::time_point<std::chrono::high_resolution_clock> end = std::chrono::high_resolution_clock::now();
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100545
Giorgio Arena2d099932017-10-25 15:47:08 +0100546 if(_log_level >= LogLevel::TESTS)
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100547 {
Giorgio Arena2d099932017-10-25 15:47:08 +0100548 func_on_all_printers([](Printer * p)
549 {
550 p->print_run_footer();
551 });
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100552 }
553
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100554 auto runtime = std::chrono::duration_cast<std::chrono::seconds>(end - start);
555 std::map<TestResult::Status, int> results = count_test_results();
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100556
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100557 if(_log_level > LogLevel::NONE)
558 {
Moritz Pflanzer8df3faf2017-07-28 13:57:53 +0100559 std::cout << "Executed " << _test_results.size() << " test(s) ("
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100560 << results[TestResult::Status::SUCCESS] << " passed, "
561 << results[TestResult::Status::EXPECTED_FAILURE] << " expected failures, "
562 << results[TestResult::Status::FAILED] << " failed, "
563 << results[TestResult::Status::CRASHED] << " crashed, "
564 << results[TestResult::Status::DISABLED] << " disabled) in " << runtime.count() << " second(s)\n";
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100565 }
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100566
steniu013e05e4e2017-08-25 17:18:01 +0100567 int num_successful_tests = results[TestResult::Status::SUCCESS] + results[TestResult::Status::EXPECTED_FAILURE] + results[TestResult::Status::DISABLED];
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100568
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100569 return (static_cast<unsigned int>(num_successful_tests) == _test_results.size());
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100570}
571
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100572void Framework::set_test_result(TestInfo info, TestResult result)
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100573{
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100574 _test_results.emplace(std::move(info), std::move(result));
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100575}
576
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100577void Framework::print_test_results(Printer &printer) const
578{
579 printer.print_run_header();
580
581 for(const auto &test : _test_results)
582 {
583 printer.print_test_header(test.first);
584 printer.print_measurements(test.second.measurements);
585 printer.print_test_footer();
586 }
587
588 printer.print_run_footer();
589}
590
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100591Profiler Framework::get_profiler() const
592{
593 Profiler profiler;
594
Moritz Pflanzer09e4f982017-08-30 12:47:06 +0100595 const bool all_instruments = std::any_of(
596 _instruments.begin(),
597 _instruments.end(),
Giorgio Arenace58a9f2017-10-31 17:59:17 +0000598 [](InstrumentsDescription type) -> bool { return type.first == InstrumentType::ALL; });
Moritz Pflanzer09e4f982017-08-30 12:47:06 +0100599
Giorgio Arenace58a9f2017-10-31 17:59:17 +0000600 auto is_selected = [&](InstrumentsDescription instrument) -> bool
Moritz Pflanzer09e4f982017-08-30 12:47:06 +0100601 {
Giorgio Arenace58a9f2017-10-31 17:59:17 +0000602 return std::find_if(_instruments.begin(), _instruments.end(), [&](InstrumentsDescription type) -> bool {
603 const auto group = static_cast<InstrumentType>(static_cast<uint64_t>(type.first) & 0xFF00);
604 return (group == instrument.first) && (instrument.second == type.second);
Moritz Pflanzer09e4f982017-08-30 12:47:06 +0100605 })
606 != _instruments.end();
607 };
608
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100609 for(const auto &instrument : _available_instruments)
610 {
Moritz Pflanzer09e4f982017-08-30 12:47:06 +0100611 if(all_instruments || is_selected(instrument.first))
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100612 {
613 profiler.add(instrument.second());
614 }
615 }
616
617 return profiler;
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100618}
619
Giorgio Arena2d099932017-10-25 15:47:08 +0100620void Framework::add_printer(Printer *printer)
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100621{
Giorgio Arena2d099932017-10-25 15:47:08 +0100622 _printers.push_back(printer);
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100623}
624
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100625std::vector<TestInfo> Framework::test_infos() const
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100626{
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100627 std::vector<TestInfo> ids;
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100628
629 int id = 0;
630
631 for(const auto &factory : _test_factories)
632 {
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100633 TestInfo test_info{ id, factory->name(), factory->mode(), factory->status() };
634
Moritz Pflanzerec2de0f2017-07-27 14:43:46 +0100635 if(_test_filter.is_selected(test_info))
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100636 {
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100637 ids.emplace_back(std::move(test_info));
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100638 }
639
640 ++id;
641 }
642
643 return ids;
644}
steniu01172c58d2017-08-31 13:49:08 +0100645
646LogLevel Framework::log_level() const
647{
648 return _log_level;
649}
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100650} // namespace framework
651} // namespace test
652} // namespace arm_compute