blob: 5a2b02fb21e3b84e5b1fb744ff927721435abe42 [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
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010033#include <chrono>
34#include <iostream>
35#include <sstream>
36#include <type_traits>
37
38namespace arm_compute
39{
40namespace test
41{
42namespace framework
43{
Moritz Pflanzera4f711b2017-07-05 11:02:23 +010044Framework::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
53std::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 Pflanzerbf234e02017-07-24 15:04:14 +010065std::map<TestResult::Status, int> Framework::count_test_results() const
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010066{
Moritz Pflanzerbf234e02017-07-24 15:04:14 +010067 std::map<TestResult::Status, int> counts;
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010068
69 for(const auto &test : _test_results)
70 {
Moritz Pflanzerbf234e02017-07-24 15:04:14 +010071 ++counts[test.second.status];
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010072 }
73
Moritz Pflanzerbf234e02017-07-24 15:04:14 +010074 return counts;
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010075}
76
77Framework &Framework::get()
78{
79 static Framework instance;
80 return instance;
81}
82
Moritz Pflanzerec2de0f2017-07-27 14:43:46 +010083void 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 Pflanzerfc95ed22017-07-05 11:07:07 +010084{
Moritz Pflanzerec2de0f2017-07-27 14:43:46 +010085 _test_filter = TestFilter(mode, name_filter, id_filter);
86 _num_iterations = num_iterations;
87 _log_level = log_level;
Moritz Pflanzera4f711b2017-07-05 11:02:23 +010088
89 _instruments = InstrumentType::NONE;
90
91 for(const auto &instrument : instruments)
92 {
93 _instruments |= instrument;
94 }
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010095}
96
97std::string Framework::current_suite_name() const
98{
99 return join(_test_suite_name.cbegin(), _test_suite_name.cend(), "/");
100}
101
102void Framework::push_suite(std::string name)
103{
104 _test_suite_name.emplace_back(std::move(name));
105}
106
107void Framework::pop_suite()
108{
109 _test_suite_name.pop_back();
110}
111
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100112void Framework::add_test_info(std::string info)
113{
114 _test_info.emplace_back(std::move(info));
115}
116
117void Framework::clear_test_info()
118{
119 _test_info.clear();
120}
121
122bool Framework::has_test_info() const
123{
124 return !_test_info.empty();
125}
126
127void Framework::print_test_info(std::ostream &os) const
128{
Moritz Pflanzer8df3faf2017-07-28 13:57:53 +0100129 if(!_test_info.empty())
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100130 {
Moritz Pflanzer8df3faf2017-07-28 13:57:53 +0100131 os << "CONTEXT:\n";
132
133 for(const auto &str : _test_info)
134 {
135 os << " " << str << "\n";
136 }
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100137 }
138}
139
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100140void Framework::log_test_start(const TestInfo &info)
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100141{
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100142 if(_printer != nullptr && _log_level >= LogLevel::TESTS)
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100143 {
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100144 _printer->print_test_header(info);
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100145 }
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100146}
147
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100148void Framework::log_test_skipped(const TestInfo &info)
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100149{
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100150 static_cast<void>(info);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100151}
152
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100153void Framework::log_test_end(const TestInfo &info)
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100154{
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100155 if(_printer != nullptr)
156 {
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100157 if(_log_level >= LogLevel::MEASUREMENTS)
158 {
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100159 _printer->print_measurements(_test_results.at(info).measurements);
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100160 }
161
162 if(_log_level >= LogLevel::TESTS)
163 {
164 _printer->print_test_footer();
165 }
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100166 }
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100167}
168
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100169void Framework::log_failed_expectation(const std::string &msg, LogLevel level)
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100170{
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100171 if(_log_level >= level)
172 {
173 std::cerr << "ERROR: " << msg << "\n";
174 }
Moritz Pflanzere1103a82017-07-18 12:20:45 +0100175
176 if(_current_test_result != nullptr)
177 {
178 _current_test_result->status = TestResult::Status::FAILED;
179 }
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100180}
181
182int Framework::num_iterations() const
183{
184 return _num_iterations;
185}
186
187void Framework::set_num_iterations(int num_iterations)
188{
189 _num_iterations = num_iterations;
190}
191
192void Framework::set_throw_errors(bool throw_errors)
193{
194 _throw_errors = throw_errors;
195}
196
197bool Framework::throw_errors() const
198{
199 return _throw_errors;
200}
201
Moritz Pflanzerfa811652017-07-26 17:00:37 +0100202void Framework::set_stop_on_error(bool stop_on_error)
203{
204 _stop_on_error = stop_on_error;
205}
206
207bool Framework::stop_on_error() const
208{
209 return _stop_on_error;
210}
211
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100212void Framework::run_test(const TestInfo &info, TestCaseFactory &test_factory)
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100213{
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100214 if(test_factory.status() == TestCaseFactory::Status::DISABLED)
215 {
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100216 log_test_skipped(info);
217 set_test_result(info, TestResult(TestResult::Status::DISABLED));
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100218 return;
219 }
220
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100221 log_test_start(info);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100222
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100223 Profiler profiler = get_profiler();
Moritz Pflanzere33eb642017-07-31 14:48:45 +0100224 TestResult result(TestResult::Status::NOT_RUN);
Moritz Pflanzere1103a82017-07-18 12:20:45 +0100225
226 _current_test_result = &result;
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100227
228 try
229 {
230 std::unique_ptr<TestCase> test_case = test_factory.make();
231
232 try
233 {
234 test_case->do_setup();
235
236 for(int i = 0; i < _num_iterations; ++i)
237 {
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100238 profiler.start();
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100239 test_case->do_run();
Anthony Barbierbf959222017-07-19 17:01:42 +0100240#ifdef ARM_COMPUTE_CL
241 if(opencl_is_available())
242 {
243 CLScheduler::get().sync();
244 }
245#endif /* ARM_COMPUTE_CL */
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100246 profiler.stop();
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100247 }
248
249 test_case->do_teardown();
Moritz Pflanzere33eb642017-07-31 14:48:45 +0100250
251 // Change status to success if no error has happend
252 if(result.status == TestResult::Status::NOT_RUN)
253 {
254 result.status = TestResult::Status::SUCCESS;
255 }
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100256 }
257 catch(const TestError &error)
258 {
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100259 if(_log_level >= error.level())
260 {
261 std::cerr << "FATAL ERROR: " << error.what() << "\n";
262 }
263
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100264 result.status = TestResult::Status::FAILED;
265
266 if(_throw_errors)
267 {
268 throw;
269 }
270 }
Moritz Pflanzer47752c92017-07-18 13:38:47 +0100271#ifdef ARM_COMPUTE_CL
272 catch(const ::cl::Error &error)
273 {
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100274 if(_log_level >= LogLevel::ERRORS)
275 {
276 std::cerr << "FATAL CL ERROR: " << error.what() << " with code " << error.err() << "\n";
277 }
278
Moritz Pflanzer47752c92017-07-18 13:38:47 +0100279 result.status = TestResult::Status::FAILED;
280
281 if(_throw_errors)
282 {
283 throw;
284 }
285 }
286#endif /* ARM_COMPUTE_CL */
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100287 catch(const std::exception &error)
288 {
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100289 if(_log_level >= LogLevel::ERRORS)
290 {
291 std::cerr << "FATAL ERROR: Received unhandled error: '" << error.what() << "'\n";
292 }
293
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100294 result.status = TestResult::Status::CRASHED;
295
296 if(_throw_errors)
297 {
298 throw;
299 }
300 }
301 catch(...)
302 {
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100303 if(_log_level >= LogLevel::ERRORS)
304 {
305 std::cerr << "FATAL ERROR: Received unhandled exception\n";
306 }
307
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100308 result.status = TestResult::Status::CRASHED;
309
310 if(_throw_errors)
311 {
312 throw;
313 }
314 }
315 }
316 catch(const std::exception &error)
317 {
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100318 if(_log_level >= LogLevel::ERRORS)
319 {
320 std::cerr << "FATAL ERROR: Received unhandled error during fixture creation: '" << error.what() << "'\n";
321 }
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100322
Moritz Pflanzere33eb642017-07-31 14:48:45 +0100323 result.status = TestResult::Status::CRASHED;
324
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100325 if(_throw_errors)
326 {
327 throw;
328 }
329 }
330 catch(...)
331 {
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100332 if(_log_level >= LogLevel::ERRORS)
333 {
334 std::cerr << "FATAL ERROR: Received unhandled exception during fixture creation\n";
335 }
336
Moritz Pflanzere1103a82017-07-18 12:20:45 +0100337 result.status = TestResult::Status::CRASHED;
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100338
339 if(_throw_errors)
340 {
341 throw;
342 }
343 }
344
Moritz Pflanzere1103a82017-07-18 12:20:45 +0100345 _current_test_result = nullptr;
346
Moritz Pflanzerfa811652017-07-26 17:00:37 +0100347 if(result.status == TestResult::Status::FAILED)
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100348 {
Moritz Pflanzerfa811652017-07-26 17:00:37 +0100349 if(info.status == TestCaseFactory::Status::EXPECTED_FAILURE)
350 {
351 result.status = TestResult::Status::EXPECTED_FAILURE;
352 }
Moritz Pflanzere33eb642017-07-31 14:48:45 +0100353 }
354
355 if(result.status == TestResult::Status::FAILED || result.status == TestResult::Status::CRASHED)
356 {
357 if(_stop_on_error)
Moritz Pflanzerfa811652017-07-26 17:00:37 +0100358 {
359 throw std::runtime_error("Abort on first error.");
360 }
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100361 }
362
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100363 result.measurements = profiler.measurements();
364
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100365 set_test_result(info, result);
366 log_test_end(info);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100367}
368
369bool Framework::run()
370{
371 // Clear old test results
372 _test_results.clear();
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100373
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100374 if(_printer != nullptr && _log_level >= LogLevel::TESTS)
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100375 {
376 _printer->print_run_header();
377 }
378
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100379 const std::chrono::time_point<std::chrono::high_resolution_clock> start = std::chrono::high_resolution_clock::now();
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100380
381 int id = 0;
382
383 for(auto &test_factory : _test_factories)
384 {
385 const std::string test_case_name = test_factory->name();
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100386 const TestInfo test_info{ id, test_case_name, test_factory->mode(), test_factory->status() };
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100387
Moritz Pflanzerec2de0f2017-07-27 14:43:46 +0100388 if(_test_filter.is_selected(test_info))
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100389 {
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100390 run_test(test_info, *test_factory);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100391 }
392
393 ++id;
394 }
395
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100396 const std::chrono::time_point<std::chrono::high_resolution_clock> end = std::chrono::high_resolution_clock::now();
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100397
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100398 if(_printer != nullptr && _log_level >= LogLevel::TESTS)
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100399 {
400 _printer->print_run_footer();
401 }
402
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100403 auto runtime = std::chrono::duration_cast<std::chrono::seconds>(end - start);
404 std::map<TestResult::Status, int> results = count_test_results();
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100405
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100406 if(_log_level > LogLevel::NONE)
407 {
Moritz Pflanzer8df3faf2017-07-28 13:57:53 +0100408 std::cout << "Executed " << _test_results.size() << " test(s) ("
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100409 << results[TestResult::Status::SUCCESS] << " passed, "
410 << results[TestResult::Status::EXPECTED_FAILURE] << " expected failures, "
411 << results[TestResult::Status::FAILED] << " failed, "
412 << results[TestResult::Status::CRASHED] << " crashed, "
413 << results[TestResult::Status::DISABLED] << " disabled) in " << runtime.count() << " second(s)\n";
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100414 }
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100415
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100416 int num_successful_tests = results[TestResult::Status::SUCCESS] + results[TestResult::Status::EXPECTED_FAILURE];
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100417
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100418 return (static_cast<unsigned int>(num_successful_tests) == _test_results.size());
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100419}
420
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100421void Framework::set_test_result(TestInfo info, TestResult result)
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100422{
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100423 _test_results.emplace(std::move(info), std::move(result));
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100424}
425
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100426void Framework::print_test_results(Printer &printer) const
427{
428 printer.print_run_header();
429
430 for(const auto &test : _test_results)
431 {
432 printer.print_test_header(test.first);
433 printer.print_measurements(test.second.measurements);
434 printer.print_test_footer();
435 }
436
437 printer.print_run_footer();
438}
439
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100440Profiler Framework::get_profiler() const
441{
442 Profiler profiler;
443
444 for(const auto &instrument : _available_instruments)
445 {
446 if((instrument.first & _instruments) != InstrumentType::NONE)
447 {
448 profiler.add(instrument.second());
449 }
450 }
451
452 return profiler;
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100453}
454
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100455void Framework::set_printer(Printer *printer)
456{
457 _printer = printer;
458}
459
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100460std::vector<TestInfo> Framework::test_infos() const
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100461{
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100462 std::vector<TestInfo> ids;
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100463
464 int id = 0;
465
466 for(const auto &factory : _test_factories)
467 {
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100468 TestInfo test_info{ id, factory->name(), factory->mode(), factory->status() };
469
Moritz Pflanzerec2de0f2017-07-27 14:43:46 +0100470 if(_test_filter.is_selected(test_info))
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100471 {
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100472 ids.emplace_back(std::move(test_info));
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100473 }
474
475 ++id;
476 }
477
478 return ids;
479}
480} // namespace framework
481} // namespace test
482} // namespace arm_compute