blob: 2dded300384c609811518a39551b2eb5275fef90 [file] [log] [blame]
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +01001/*
Ramy Elgammal1355ec42023-05-15 13:50:46 +01002 * Copyright (c) 2017-2021, 2023 Arm Limited.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +01003 *
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#ifndef ARM_COMPUTE_TEST_FRAMEWORK
25#define ARM_COMPUTE_TEST_FRAMEWORK
26
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +010027#include "DatasetModes.h"
Moritz Pflanzer2ac50402017-07-24 15:52:54 +010028#include "Exceptions.h"
Moritz Pflanzera4f711b2017-07-05 11:02:23 +010029#include "Profiler.h"
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010030#include "TestCase.h"
31#include "TestCaseFactory.h"
32#include "TestResult.h"
33#include "Utils.h"
Moritz Pflanzera4f711b2017-07-05 11:02:23 +010034#include "instruments/Instruments.h"
Moritz Pflanzer80fffae2017-07-05 11:02:37 +010035#include "printers/Printer.h"
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010036
37#include <algorithm>
38#include <chrono>
39#include <map>
40#include <memory>
41#include <numeric>
42#include <ostream>
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010043#include <set>
44#include <sstream>
45#include <string>
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010046#include <vector>
47
48namespace arm_compute
49{
50namespace test
51{
52namespace framework
53{
Matthew Bentham470bc1e2020-03-09 10:55:40 +000054class TestFilter;
55
Georgios Pinitas7f152512019-12-16 19:59:52 +000056/** Framework configuration structure */
57struct FrameworkConfig
58{
59 std::vector<framework::InstrumentsDescription> instruments{}; /**< Instrument types that will be used for benchmarking. */
60 std::string name_filter{}; /**< Regular expression to filter tests by name. Only matching tests will be executed. */
61 std::string id_filter{}; /**< String to match selected test ids. Only matching tests will be executed. */
62 DatasetMode mode{ DatasetMode::ALL }; /**< Dataset mode. */
63 int num_iterations{ 1 }; /**< Number of iterations per test. */
64 float cooldown_sec{ -1.f }; /**< Delay between tests in seconds. */
65 LogLevel log_level{ LogLevel::NONE }; /**< Verbosity of the output. */
Giorgio Arena68e29da2021-02-08 16:31:10 +000066 bool configure_only{ false }; /**< Only configure kernels */
Ramy Elgammal1355ec42023-05-15 13:50:46 +010067 bool print_rerun_cmd{ false }; /**< Print the command to rerun the failed testcase */
ramy.elgammal@arm.coma2561f02023-06-16 20:45:48 +010068 unsigned int seed{ 0 }; /**< The seed that is used to fill tensors with random values.*/
Georgios Pinitas7f152512019-12-16 19:59:52 +000069};
70
Moritz Pflanzer542002c2017-07-26 16:03:58 +010071/** Information about a test case.
72 *
73 * A test can be identified either via its id or via its name. Additionally
74 * each test is tagged with the data set mode in which it will be used and
75 * its status.
76 *
77 * @note The mapping between test id and test name is not guaranteed to be
78 * stable. It is subject to change as new test are added.
79 */
80struct TestInfo
81{
Alex Gildayc357c472018-03-21 13:54:09 +000082 int id; /**< Test ID */
83 std::string name; /**< Test name */
84 DatasetMode mode; /**< Test data set mode */
85 TestCaseFactory::Status status; /**< Test status */
Moritz Pflanzer542002c2017-07-26 16:03:58 +010086};
87
88inline bool operator<(const TestInfo &lhs, const TestInfo &rhs)
89{
90 return lhs.id < rhs.id;
91}
92
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010093/** Main framework class.
94 *
95 * Keeps track of the global state, owns all test cases and collects results.
96 */
97class Framework final
98{
99public:
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100100 /** Access to the singleton.
101 *
102 * @return Unique instance of the framework class.
103 */
104 static Framework &get();
105
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100106 /** Supported instrument types for benchmarking.
107 *
108 * @return Set of all available instrument types.
109 */
Giorgio Arenace58a9f2017-10-31 17:59:17 +0000110 std::set<InstrumentsDescription> available_instruments() const;
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100111
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100112 /** Init the framework.
113 *
Moritz Pflanzerec2de0f2017-07-27 14:43:46 +0100114 * @see TestFilter::TestFilter for the format of the string to filter ids.
115 *
Georgios Pinitas7f152512019-12-16 19:59:52 +0000116 * @param[in] config Framework configuration meta-data.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100117 */
Georgios Pinitas7f152512019-12-16 19:59:52 +0000118 void init(const FrameworkConfig &config);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100119
120 /** Add a new test suite.
121 *
122 * @warning Cannot be used at execution time. It can only be used for
123 * registering test cases.
124 *
125 * @param[in] name Name of the added test suite.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100126 */
127 void push_suite(std::string name);
128
129 /** Remove innermost test suite.
130 *
131 * @warning Cannot be used at execution time. It can only be used for
132 * registering test cases.
133 */
134 void pop_suite();
135
136 /** Add a test case to the framework.
137 *
138 * @param[in] test_name Name of the new test case.
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100139 * @param[in] mode Mode in which to include the test.
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100140 * @param[in] status Status of the test case.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100141 */
142 template <typename T>
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100143 void add_test_case(std::string test_name, DatasetMode mode, TestCaseFactory::Status status);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100144
145 /** Add a data test case to the framework.
146 *
147 * @param[in] test_name Name of the new test case.
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100148 * @param[in] mode Mode in which to include the test.
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100149 * @param[in] status Status of the test case.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100150 * @param[in] description Description of @p data.
151 * @param[in] data Data that will be used as input to the test.
152 */
153 template <typename T, typename D>
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100154 void add_data_test_case(std::string test_name, DatasetMode mode, TestCaseFactory::Status status, std::string description, D &&data);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100155
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100156 /** Add info string for the next expectation/assertion.
157 *
158 * @param[in] info Info string.
159 */
160 void add_test_info(std::string info);
161
162 /** Clear the collected test info. */
163 void clear_test_info();
164
165 /** Check if any info has been registered.
166 *
167 * @return True if there is test info.
168 */
169 bool has_test_info() const;
170
171 /** Print test info.
172 *
173 * @param[out] os Output stream.
174 */
175 void print_test_info(std::ostream &os) const;
176
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100177 /** Tell the framework that execution of a test starts.
178 *
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100179 * @param[in] info Test info.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100180 */
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100181 void log_test_start(const TestInfo &info);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100182
183 /** Tell the framework that a test case is skipped.
184 *
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100185 * @param[in] info Test info.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100186 */
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100187 void log_test_skipped(const TestInfo &info);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100188
189 /** Tell the framework that a test case finished.
190 *
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100191 * @param[in] info Test info.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100192 */
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100193 void log_test_end(const TestInfo &info);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100194
195 /** Tell the framework that the currently running test case failed a non-fatal expectation.
196 *
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100197 * @param[in] error Description of the error.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100198 */
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100199 void log_failed_expectation(const TestError &error);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100200
steniu01172c58d2017-08-31 13:49:08 +0100201 /** Print the debug information that has already been logged
202 *
203 * @param[in] info Description of the log info.
204 */
205 void log_info(const std::string &info);
206
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100207 /** Number of iterations per test case.
208 *
209 * @return Number of iterations per test case.
210 */
211 int num_iterations() const;
212
213 /** Set number of iterations per test case.
214 *
215 * @param[in] num_iterations Number of iterations per test case.
216 */
217 void set_num_iterations(int num_iterations);
218
219 /** Should errors be caught or thrown by the framework.
220 *
221 * @return True if errors are thrown.
222 */
223 bool throw_errors() const;
224
225 /** Set whether errors are caught or thrown by the framework.
226 *
227 * @param[in] throw_errors True if errors should be thrown.
228 */
229 void set_throw_errors(bool throw_errors);
230
Moritz Pflanzerfa811652017-07-26 17:00:37 +0100231 /** Indicates if test execution is stopped after the first failed test.
232 *
ramelg01b2eba7f2021-12-23 08:32:08 +0000233 * @return True if the execution is going to be stopped after the first failed test.
Moritz Pflanzerfa811652017-07-26 17:00:37 +0100234 */
235 bool stop_on_error() const;
236
ramelg01b2eba7f2021-12-23 08:32:08 +0000237 /** Set whether to stop execution after the first failed test.
Moritz Pflanzerfa811652017-07-26 17:00:37 +0100238 *
ramelg01b2eba7f2021-12-23 08:32:08 +0000239 * @param[in] stop_on_error True if execution is going to be stopped after first failed test.
Moritz Pflanzerfa811652017-07-26 17:00:37 +0100240 */
241 void set_stop_on_error(bool stop_on_error);
242
Anthony Barbierf6705ec2017-09-28 12:01:10 +0100243 /** Indicates if a test should be marked as failed when its assets are missing.
244 *
245 * @return True if a test should be marked as failed when its assets are missing.
246 */
247 bool error_on_missing_assets() const;
248
249 /** Set whether a test should be considered as failed if its assets cannot be found.
250 *
251 * @param[in] error_on_missing_assets True if a test should be marked as failed when its assets are missing.
252 */
253 void set_error_on_missing_assets(bool error_on_missing_assets);
254
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100255 /** Run all enabled test cases.
256 *
257 * @return True if all test cases executed successful.
258 */
259 bool run();
260
261 /** Set the result for an executed test case.
262 *
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100263 * @param[in] info Test info.
264 * @param[in] result Execution result.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100265 */
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100266 void set_test_result(TestInfo info, TestResult result);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100267
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100268 /** Use the specified printer to output test results from the last run.
269 *
270 * This method can be used if the test results need to be obtained using a
271 * different printer than the one managed by the framework.
272 *
273 * @param[in] printer Printer used to output results.
274 */
275 void print_test_results(Printer &printer) const;
276
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100277 /** Factory method to obtain a configured profiler.
278 *
279 * The profiler enables all instruments that have been passed to the @ref
280 * init method.
281 *
282 * @return Configured profiler to collect benchmark results.
283 */
284 Profiler get_profiler() const;
285
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100286 /** Set the printer used for the output of test results.
287 *
288 * @param[in] printer Pointer to a printer.
289 */
Giorgio Arena2d099932017-10-25 15:47:08 +0100290 void add_printer(Printer *printer);
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100291
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100292 /** List of @ref TestInfo's.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100293 *
294 * @return Vector with all test ids.
295 */
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100296 std::vector<TestInfo> test_infos() const;
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100297
steniu01172c58d2017-08-31 13:49:08 +0100298 /** Get the current logging level
299 *
300 * @return The current logging level.
301 */
302 LogLevel log_level() const;
Georgios Pinitas12833d02019-07-25 13:31:10 +0100303 /** Sets instruments info
304 *
305 * @note TODO(COMPMID-2638) : Remove once instruments are transferred outside the framework.
306 *
307 * @param[in] instr_info Instruments info to set
308 */
309 void set_instruments_info(InstrumentsInfo instr_info);
Giorgio Arena68e29da2021-02-08 16:31:10 +0000310 /** Get the configure only flag
311 *
312 * @return The current configure only flag.
313 */
314 bool configure_only() const;
315 /** Return whether the new fixture has been called
316 *
317 * @return The current new fixture call flag.
318 */
319 bool new_fixture_call() const;
320 /** Set the new fixture call flag
321 *
322 * @param[in] val Value to set for the flag
323 */
324 void set_new_fixture_call(bool val);
steniu01172c58d2017-08-31 13:49:08 +0100325
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100326private:
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100327 Framework();
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100328 ~Framework() = default;
329
330 Framework(const Framework &) = delete;
331 Framework &operator=(const Framework &) = delete;
332
Ramy Elgammal1355ec42023-05-15 13:50:46 +0100333 TestResult::Status run_test(const TestInfo &info, TestCaseFactory &test_factory);
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100334 std::map<TestResult::Status, int> count_test_results() const;
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100335
336 /** Returns the current test suite name.
337 *
338 * @warning Cannot be used at execution time to get the test suite of the
339 * currently executed test case. It can only be used for registering test
340 * cases.
341 *
342 * @return Name of the current test suite.
343 */
344 std::string current_suite_name() const;
345
Giorgio Arena2d099932017-10-25 15:47:08 +0100346 /* Perform func on all printers */
347 template <typename F>
348 void func_on_all_printers(F &&func);
349
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100350 std::vector<std::string> _test_suite_name{};
351 std::vector<std::unique_ptr<TestCaseFactory>> _test_factories{};
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100352 std::map<TestInfo, TestResult> _test_results{};
Giorgio Arena2d099932017-10-25 15:47:08 +0100353 int _num_iterations{ 1 };
Georgios Pinitas7f152512019-12-16 19:59:52 +0000354 float _cooldown_sec{ -1.f };
Giorgio Arena2d099932017-10-25 15:47:08 +0100355 bool _throw_errors{ false };
356 bool _stop_on_error{ false };
357 bool _error_on_missing_assets{ false };
358 std::vector<Printer *> _printers{};
Giorgio Arena68e29da2021-02-08 16:31:10 +0000359 bool _configure_only{ false };
360 bool _new_fixture_call{ false };
Ramy Elgammal1355ec42023-05-15 13:50:46 +0100361 bool _print_rerun_cmd{ false };
ramy.elgammal@arm.coma2561f02023-06-16 20:45:48 +0100362 unsigned int _seed{ 0 };
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100363
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100364 using create_function = std::unique_ptr<Instrument>();
Giorgio Arenace58a9f2017-10-31 17:59:17 +0000365 std::map<InstrumentsDescription, create_function *> _available_instruments{};
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100366
Giorgio Arenace58a9f2017-10-31 17:59:17 +0000367 std::set<framework::InstrumentsDescription> _instruments{ std::pair<InstrumentType, ScaleFactor>(InstrumentType::NONE, ScaleFactor::NONE) };
Matthew Bentham470bc1e2020-03-09 10:55:40 +0000368 std::unique_ptr<TestFilter> _test_filter;
Giorgio Arenace58a9f2017-10-31 17:59:17 +0000369 LogLevel _log_level{ LogLevel::ALL };
370 const TestInfo *_current_test_info{ nullptr };
371 TestResult *_current_test_result{ nullptr };
372 std::vector<std::string> _test_info{};
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100373};
374
375template <typename T>
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100376inline void Framework::add_test_case(std::string test_name, DatasetMode mode, TestCaseFactory::Status status)
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100377{
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000378 _test_factories.emplace_back(std::make_unique<SimpleTestCaseFactory<T>>(current_suite_name(), std::move(test_name), mode, status));
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100379}
380
381template <typename T, typename D>
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100382inline void Framework::add_data_test_case(std::string test_name, DatasetMode mode, TestCaseFactory::Status status, std::string description, D &&data)
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100383{
384 // WORKAROUND for GCC 4.9
385 // The function should get *it which is tuple but that seems to trigger a
386 // bug in the compiler.
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100387 auto tmp = std::unique_ptr<DataTestCaseFactory<T, decltype(*std::declval<D>())>>(new DataTestCaseFactory<T, decltype(*std::declval<D>())>(current_suite_name(), std::move(test_name), mode, status,
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100388 std::move(description), *data));
389 _test_factories.emplace_back(std::move(tmp));
390}
391} // namespace framework
392} // namespace test
393} // namespace arm_compute
394#endif /* ARM_COMPUTE_TEST_FRAMEWORK */