blob: 65ffc0a81845d71b853d8f34ea4e0de78090a0fa [file] [log] [blame]
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +01001/*
Alex Gildayc357c472018-03-21 13:54:09 +00002 * Copyright (c) 2017-2018 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"
Moritz Pflanzerec2de0f2017-07-27 14:43:46 +010032#include "TestFilter.h"
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010033#include "TestResult.h"
34#include "Utils.h"
Moritz Pflanzera4f711b2017-07-05 11:02:23 +010035#include "instruments/Instruments.h"
Moritz Pflanzer80fffae2017-07-05 11:02:37 +010036#include "printers/Printer.h"
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010037
38#include <algorithm>
39#include <chrono>
40#include <map>
41#include <memory>
42#include <numeric>
43#include <ostream>
44#include <regex>
45#include <set>
46#include <sstream>
47#include <string>
48#include <tuple>
49#include <vector>
50
51namespace arm_compute
52{
53namespace test
54{
55namespace framework
56{
Moritz Pflanzer542002c2017-07-26 16:03:58 +010057/** Information about a test case.
58 *
59 * A test can be identified either via its id or via its name. Additionally
60 * each test is tagged with the data set mode in which it will be used and
61 * its status.
62 *
63 * @note The mapping between test id and test name is not guaranteed to be
64 * stable. It is subject to change as new test are added.
65 */
66struct TestInfo
67{
Alex Gildayc357c472018-03-21 13:54:09 +000068 int id; /**< Test ID */
69 std::string name; /**< Test name */
70 DatasetMode mode; /**< Test data set mode */
71 TestCaseFactory::Status status; /**< Test status */
Moritz Pflanzer542002c2017-07-26 16:03:58 +010072};
73
74inline bool operator<(const TestInfo &lhs, const TestInfo &rhs)
75{
76 return lhs.id < rhs.id;
77}
78
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010079/** Main framework class.
80 *
81 * Keeps track of the global state, owns all test cases and collects results.
82 */
83class Framework final
84{
85public:
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010086 /** Access to the singleton.
87 *
88 * @return Unique instance of the framework class.
89 */
90 static Framework &get();
91
Moritz Pflanzera4f711b2017-07-05 11:02:23 +010092 /** Supported instrument types for benchmarking.
93 *
94 * @return Set of all available instrument types.
95 */
Giorgio Arenace58a9f2017-10-31 17:59:17 +000096 std::set<InstrumentsDescription> available_instruments() const;
Moritz Pflanzera4f711b2017-07-05 11:02:23 +010097
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010098 /** Init the framework.
99 *
Moritz Pflanzerec2de0f2017-07-27 14:43:46 +0100100 * @see TestFilter::TestFilter for the format of the string to filter ids.
101 *
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100102 * @param[in] instruments Instrument types that will be used for benchmarking.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100103 * @param[in] num_iterations Number of iterations per test.
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100104 * @param[in] mode Dataset mode.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100105 * @param[in] name_filter Regular expression to filter tests by name. Only matching tests will be executed.
Moritz Pflanzerec2de0f2017-07-27 14:43:46 +0100106 * @param[in] id_filter String to match selected test ids. Only matching tests will be executed.
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100107 * @param[in] log_level Verbosity of the output.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100108 */
Giorgio Arenace58a9f2017-10-31 17:59:17 +0000109 void init(const std::vector<framework::InstrumentsDescription> &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 +0100110
111 /** Add a new test suite.
112 *
113 * @warning Cannot be used at execution time. It can only be used for
114 * registering test cases.
115 *
116 * @param[in] name Name of the added test suite.
117 *
118 * @return Name of the current test suite.
119 */
120 void push_suite(std::string name);
121
122 /** Remove innermost test suite.
123 *
124 * @warning Cannot be used at execution time. It can only be used for
125 * registering test cases.
126 */
127 void pop_suite();
128
129 /** Add a test case to the framework.
130 *
131 * @param[in] test_name Name of the new test case.
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100132 * @param[in] mode Mode in which to include the test.
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100133 * @param[in] status Status of the test case.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100134 */
135 template <typename T>
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100136 void add_test_case(std::string test_name, DatasetMode mode, TestCaseFactory::Status status);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100137
138 /** Add a data test case to the framework.
139 *
140 * @param[in] test_name Name of the new test case.
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100141 * @param[in] mode Mode in which to include the test.
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100142 * @param[in] status Status of the test case.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100143 * @param[in] description Description of @p data.
144 * @param[in] data Data that will be used as input to the test.
145 */
146 template <typename T, typename D>
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100147 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 +0100148
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100149 /** Add info string for the next expectation/assertion.
150 *
151 * @param[in] info Info string.
152 */
153 void add_test_info(std::string info);
154
155 /** Clear the collected test info. */
156 void clear_test_info();
157
158 /** Check if any info has been registered.
159 *
160 * @return True if there is test info.
161 */
162 bool has_test_info() const;
163
164 /** Print test info.
165 *
166 * @param[out] os Output stream.
167 */
168 void print_test_info(std::ostream &os) const;
169
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100170 /** Tell the framework that execution of a test starts.
171 *
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100172 * @param[in] info Test info.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100173 */
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100174 void log_test_start(const TestInfo &info);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100175
176 /** Tell the framework that a test case is skipped.
177 *
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100178 * @param[in] info Test info.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100179 */
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100180 void log_test_skipped(const TestInfo &info);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100181
182 /** Tell the framework that a test case finished.
183 *
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100184 * @param[in] info Test info.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100185 */
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100186 void log_test_end(const TestInfo &info);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100187
188 /** Tell the framework that the currently running test case failed a non-fatal expectation.
189 *
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100190 * @param[in] error Description of the error.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100191 */
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100192 void log_failed_expectation(const TestError &error);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100193
steniu01172c58d2017-08-31 13:49:08 +0100194 /** Print the debug information that has already been logged
195 *
196 * @param[in] info Description of the log info.
197 */
198 void log_info(const std::string &info);
199
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100200 /** Number of iterations per test case.
201 *
202 * @return Number of iterations per test case.
203 */
204 int num_iterations() const;
205
206 /** Set number of iterations per test case.
207 *
208 * @param[in] num_iterations Number of iterations per test case.
209 */
210 void set_num_iterations(int num_iterations);
211
212 /** Should errors be caught or thrown by the framework.
213 *
214 * @return True if errors are thrown.
215 */
216 bool throw_errors() const;
217
218 /** Set whether errors are caught or thrown by the framework.
219 *
220 * @param[in] throw_errors True if errors should be thrown.
221 */
222 void set_throw_errors(bool throw_errors);
223
Moritz Pflanzerfa811652017-07-26 17:00:37 +0100224 /** Indicates if test execution is stopped after the first failed test.
225 *
226 * @return True if the execution is going to be aborted after the first failed test.
227 */
228 bool stop_on_error() const;
229
230 /** Set whether to abort execution after the first failed test.
231 *
232 * @param[in] stop_on_error True if execution is going to be aborted after first failed test.
233 */
234 void set_stop_on_error(bool stop_on_error);
235
Anthony Barbierf6705ec2017-09-28 12:01:10 +0100236 /** Indicates if a test should be marked as failed when its assets are missing.
237 *
238 * @return True if a test should be marked as failed when its assets are missing.
239 */
240 bool error_on_missing_assets() const;
241
242 /** Set whether a test should be considered as failed if its assets cannot be found.
243 *
244 * @param[in] error_on_missing_assets True if a test should be marked as failed when its assets are missing.
245 */
246 void set_error_on_missing_assets(bool error_on_missing_assets);
247
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100248 /** Run all enabled test cases.
249 *
250 * @return True if all test cases executed successful.
251 */
252 bool run();
253
254 /** Set the result for an executed test case.
255 *
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100256 * @param[in] info Test info.
257 * @param[in] result Execution result.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100258 */
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100259 void set_test_result(TestInfo info, TestResult result);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100260
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100261 /** Use the specified printer to output test results from the last run.
262 *
263 * This method can be used if the test results need to be obtained using a
264 * different printer than the one managed by the framework.
265 *
266 * @param[in] printer Printer used to output results.
267 */
268 void print_test_results(Printer &printer) const;
269
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100270 /** Factory method to obtain a configured profiler.
271 *
272 * The profiler enables all instruments that have been passed to the @ref
273 * init method.
274 *
275 * @return Configured profiler to collect benchmark results.
276 */
277 Profiler get_profiler() const;
278
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100279 /** Set the printer used for the output of test results.
280 *
281 * @param[in] printer Pointer to a printer.
282 */
Giorgio Arena2d099932017-10-25 15:47:08 +0100283 void add_printer(Printer *printer);
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100284
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100285 /** List of @ref TestInfo's.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100286 *
287 * @return Vector with all test ids.
288 */
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100289 std::vector<TestInfo> test_infos() const;
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100290
steniu01172c58d2017-08-31 13:49:08 +0100291 /** Get the current logging level
292 *
293 * @return The current logging level.
294 */
295 LogLevel log_level() const;
296
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100297private:
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100298 Framework();
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100299 ~Framework() = default;
300
301 Framework(const Framework &) = delete;
302 Framework &operator=(const Framework &) = delete;
303
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100304 void run_test(const TestInfo &info, TestCaseFactory &test_factory);
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100305 std::map<TestResult::Status, int> count_test_results() const;
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100306
307 /** Returns the current test suite name.
308 *
309 * @warning Cannot be used at execution time to get the test suite of the
310 * currently executed test case. It can only be used for registering test
311 * cases.
312 *
313 * @return Name of the current test suite.
314 */
315 std::string current_suite_name() const;
316
Giorgio Arena2d099932017-10-25 15:47:08 +0100317 /* Perform func on all printers */
318 template <typename F>
319 void func_on_all_printers(F &&func);
320
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100321 std::vector<std::string> _test_suite_name{};
322 std::vector<std::unique_ptr<TestCaseFactory>> _test_factories{};
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100323 std::map<TestInfo, TestResult> _test_results{};
Giorgio Arena2d099932017-10-25 15:47:08 +0100324 int _num_iterations{ 1 };
325 bool _throw_errors{ false };
326 bool _stop_on_error{ false };
327 bool _error_on_missing_assets{ false };
328 std::vector<Printer *> _printers{};
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100329
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100330 using create_function = std::unique_ptr<Instrument>();
Giorgio Arenace58a9f2017-10-31 17:59:17 +0000331 std::map<InstrumentsDescription, create_function *> _available_instruments{};
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100332
Giorgio Arenace58a9f2017-10-31 17:59:17 +0000333 std::set<framework::InstrumentsDescription> _instruments{ std::pair<InstrumentType, ScaleFactor>(InstrumentType::NONE, ScaleFactor::NONE) };
334 TestFilter _test_filter{};
335 LogLevel _log_level{ LogLevel::ALL };
336 const TestInfo *_current_test_info{ nullptr };
337 TestResult *_current_test_result{ nullptr };
338 std::vector<std::string> _test_info{};
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100339};
340
341template <typename T>
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100342inline void Framework::add_test_case(std::string test_name, DatasetMode mode, TestCaseFactory::Status status)
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100343{
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100344 _test_factories.emplace_back(support::cpp14::make_unique<SimpleTestCaseFactory<T>>(current_suite_name(), std::move(test_name), mode, status));
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100345}
346
347template <typename T, typename D>
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100348inline 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 +0100349{
350 // WORKAROUND for GCC 4.9
351 // The function should get *it which is tuple but that seems to trigger a
352 // bug in the compiler.
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100353 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 +0100354 std::move(description), *data));
355 _test_factories.emplace_back(std::move(tmp));
356}
357} // namespace framework
358} // namespace test
359} // namespace arm_compute
360#endif /* ARM_COMPUTE_TEST_FRAMEWORK */