blob: 7708537fd6b0b153fae580e7897774b092897986 [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
26#include "Exceptions.h"
27#include "support/ToolchainSupport.h"
28
Moritz Pflanzer47752c92017-07-18 13:38:47 +010029#ifdef ARM_COMPUTE_CL
30#include "arm_compute/core/CL/OpenCL.h"
31#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 Pflanzerfc95ed22017-07-05 11:07:07 +010065std::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
93Framework &Framework::get()
94{
95 static Framework instance;
96 return instance;
97}
98
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +010099void Framework::init(const std::vector<InstrumentType> &instruments, int num_iterations, DatasetMode mode, const std::string &name_filter, const std::string &id_filter)
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100100{
101 _test_name_filter = std::regex{ name_filter };
102 _test_id_filter = std::regex{ id_filter };
103 _num_iterations = num_iterations;
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100104 _dataset_mode = mode;
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100105
106 _instruments = InstrumentType::NONE;
107
108 for(const auto &instrument : instruments)
109 {
110 _instruments |= instrument;
111 }
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100112}
113
114std::string Framework::current_suite_name() const
115{
116 return join(_test_suite_name.cbegin(), _test_suite_name.cend(), "/");
117}
118
119void Framework::push_suite(std::string name)
120{
121 _test_suite_name.emplace_back(std::move(name));
122}
123
124void Framework::pop_suite()
125{
126 _test_suite_name.pop_back();
127}
128
129void Framework::log_test_start(const std::string &test_name)
130{
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100131 if(_printer != nullptr)
132 {
133 _printer->print_test_header(test_name);
134 }
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100135}
136
137void Framework::log_test_skipped(const std::string &test_name)
138{
139 static_cast<void>(test_name);
140}
141
142void Framework::log_test_end(const std::string &test_name)
143{
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100144 if(_printer != nullptr)
145 {
146 _printer->print_measurements(_test_results.at(test_name).measurements);
147 _printer->print_test_footer();
148 }
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100149}
150
151void Framework::log_failed_expectation(const std::string &msg)
152{
153 std::cerr << "ERROR: " << msg << "\n";
Moritz Pflanzere1103a82017-07-18 12:20:45 +0100154
155 if(_current_test_result != nullptr)
156 {
157 _current_test_result->status = TestResult::Status::FAILED;
158 }
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100159}
160
161int Framework::num_iterations() const
162{
163 return _num_iterations;
164}
165
166void Framework::set_num_iterations(int num_iterations)
167{
168 _num_iterations = num_iterations;
169}
170
171void Framework::set_throw_errors(bool throw_errors)
172{
173 _throw_errors = throw_errors;
174}
175
176bool Framework::throw_errors() const
177{
178 return _throw_errors;
179}
180
181bool Framework::is_enabled(const TestId &id) const
182{
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100183 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 Pflanzerfc95ed22017-07-05 11:07:07 +0100204}
205
206void 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 Pflanzera4f711b2017-07-05 11:02:23 +0100212 Profiler profiler = get_profiler();
Moritz Pflanzere1103a82017-07-18 12:20:45 +0100213 TestResult result(TestResult::Status::SUCCESS);
214
215 _current_test_result = &result;
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100216
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 Pflanzera4f711b2017-07-05 11:02:23 +0100227 profiler.start();
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100228 test_case->do_run();
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100229 profiler.stop();
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100230 }
231
232 test_case->do_teardown();
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100233 }
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 Pflanzer47752c92017-07-18 13:38:47 +0100244#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 Pflanzerfc95ed22017-07-05 11:07:07 +0100256 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 Pflanzere1103a82017-07-18 12:20:45 +0100289 result.status = TestResult::Status::CRASHED;
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100290
291 if(_throw_errors)
292 {
293 throw;
294 }
295 }
296
Moritz Pflanzere1103a82017-07-18 12:20:45 +0100297 _current_test_result = nullptr;
298
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100299 result.measurements = profiler.measurements();
300
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100301 set_test_result(test_case_name, result);
302 log_test_end(test_case_name);
303}
304
305bool Framework::run()
306{
307 // Clear old test results
308 _test_results.clear();
309 _runtime = std::chrono::seconds{ 0 };
310
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100311 if(_printer != nullptr)
312 {
313 _printer->print_run_header();
314 }
315
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100316 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 Pflanzerd03b00a2017-07-17 13:50:12 +0100324 if(!is_enabled(TestId(id, test_case_name, test_factory->mode())))
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100325 {
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 Pflanzer80fffae2017-07-05 11:02:37 +0100338 if(_printer != nullptr)
339 {
340 _printer->print_run_footer();
341 }
342
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100343 _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
356void Framework::set_test_result(std::string test_case_name, TestResult result)
357{
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100358 _test_results.emplace(std::move(test_case_name), std::move(result));
359}
360
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100361void 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 Pflanzera4f711b2017-07-05 11:02:23 +0100375Profiler 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 Pflanzerfc95ed22017-07-05 11:07:07 +0100388}
389
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100390void Framework::set_printer(Printer *printer)
391{
392 _printer = printer;
393}
394
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100395std::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 Pflanzerd03b00a2017-07-17 13:50:12 +0100403 if(is_enabled(TestId(id, factory->name(), factory->mode())))
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100404 {
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100405 ids.emplace_back(id, factory->name(), factory->mode());
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100406 }
407
408 ++id;
409 }
410
411 return ids;
412}
413} // namespace framework
414} // namespace test
415} // namespace arm_compute