blob: 5f52b4fbe86182641e4bc9784bfe4ff5179e5c12 [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#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>
43#include <regex>
44#include <set>
45#include <sstream>
46#include <string>
47#include <tuple>
48#include <vector>
49
50namespace arm_compute
51{
52namespace test
53{
54namespace framework
55{
56/** Main framework class.
57 *
58 * Keeps track of the global state, owns all test cases and collects results.
59 */
60class Framework final
61{
62public:
Moritz Pflanzerbf234e02017-07-24 15:04:14 +010063 /** Information about a test case.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010064 *
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +010065 * A test can be identified either via its id or via its name. Additionally
Moritz Pflanzerbf234e02017-07-24 15:04:14 +010066 * each test is tagged with the data set mode in which it will be used and
67 * its status.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010068 *
69 * @note The mapping between test id and test name is not guaranteed to be
70 * stable. It is subject to change as new test are added.
71 */
Moritz Pflanzerbf234e02017-07-24 15:04:14 +010072 struct TestInfo
73 {
74 int id;
75 std::string name;
76 DatasetMode mode;
77 TestCaseFactory::Status status;
78 };
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010079
80 /** Access to the singleton.
81 *
82 * @return Unique instance of the framework class.
83 */
84 static Framework &get();
85
Moritz Pflanzera4f711b2017-07-05 11:02:23 +010086 /** Supported instrument types for benchmarking.
87 *
88 * @return Set of all available instrument types.
89 */
90 std::set<InstrumentType> available_instruments() const;
91
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010092 /** Init the framework.
93 *
Moritz Pflanzera4f711b2017-07-05 11:02:23 +010094 * @param[in] instruments Instrument types that will be used for benchmarking.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010095 * @param[in] num_iterations Number of iterations per test.
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +010096 * @param[in] mode Dataset mode.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010097 * @param[in] name_filter Regular expression to filter tests by name. Only matching tests will be executed.
Moritz Pflanzer81527bf2017-07-20 15:11:33 +010098 * @param[in] id_filter Test id. Only this test will be executed.
Moritz Pflanzer2ac50402017-07-24 15:52:54 +010099 * @param[in] log_level Verbosity of the output.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100100 */
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100101 void init(const std::vector<InstrumentType> &instruments, int num_iterations, DatasetMode mode, const std::string &name_filter, int64_t id_filter, LogLevel log_level);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100102
103 /** Add a new test suite.
104 *
105 * @warning Cannot be used at execution time. It can only be used for
106 * registering test cases.
107 *
108 * @param[in] name Name of the added test suite.
109 *
110 * @return Name of the current test suite.
111 */
112 void push_suite(std::string name);
113
114 /** Remove innermost test suite.
115 *
116 * @warning Cannot be used at execution time. It can only be used for
117 * registering test cases.
118 */
119 void pop_suite();
120
121 /** Add a test case to the framework.
122 *
123 * @param[in] test_name Name of the new test case.
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100124 * @param[in] mode Mode in which to include the test.
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100125 * @param[in] status Status of the test case.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100126 */
127 template <typename T>
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100128 void add_test_case(std::string test_name, DatasetMode mode, TestCaseFactory::Status status);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100129
130 /** Add a data test case to the framework.
131 *
132 * @param[in] test_name Name of the new test case.
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100133 * @param[in] mode Mode in which to include the test.
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100134 * @param[in] status Status of the test case.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100135 * @param[in] description Description of @p data.
136 * @param[in] data Data that will be used as input to the test.
137 */
138 template <typename T, typename D>
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100139 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 +0100140
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100141 /** Add info string for the next expectation/assertion.
142 *
143 * @param[in] info Info string.
144 */
145 void add_test_info(std::string info);
146
147 /** Clear the collected test info. */
148 void clear_test_info();
149
150 /** Check if any info has been registered.
151 *
152 * @return True if there is test info.
153 */
154 bool has_test_info() const;
155
156 /** Print test info.
157 *
158 * @param[out] os Output stream.
159 */
160 void print_test_info(std::ostream &os) const;
161
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100162 /** Tell the framework that execution of a test starts.
163 *
164 * @param[in] test_name Name of the started test case.
165 */
166 void log_test_start(const std::string &test_name);
167
168 /** Tell the framework that a test case is skipped.
169 *
170 * @param[in] test_name Name of the skipped test case.
171 */
172 void log_test_skipped(const std::string &test_name);
173
174 /** Tell the framework that a test case finished.
175 *
176 * @param[in] test_name Name of the finished test case.
177 */
178 void log_test_end(const std::string &test_name);
179
180 /** Tell the framework that the currently running test case failed a non-fatal expectation.
181 *
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100182 * @param[in] msg Description of the failure.
183 * @param[in] level Severity of the failed expectation.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100184 */
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100185 void log_failed_expectation(const std::string &msg, LogLevel level = LogLevel::ERRORS);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100186
187 /** Number of iterations per test case.
188 *
189 * @return Number of iterations per test case.
190 */
191 int num_iterations() const;
192
193 /** Set number of iterations per test case.
194 *
195 * @param[in] num_iterations Number of iterations per test case.
196 */
197 void set_num_iterations(int num_iterations);
198
199 /** Should errors be caught or thrown by the framework.
200 *
201 * @return True if errors are thrown.
202 */
203 bool throw_errors() const;
204
205 /** Set whether errors are caught or thrown by the framework.
206 *
207 * @param[in] throw_errors True if errors should be thrown.
208 */
209 void set_throw_errors(bool throw_errors);
210
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100211 /** Check if a test case is selected to be executed.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100212 *
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100213 * @param[in] info Test case info.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100214 *
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100215 * @return True if the test case is selected to be executed.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100216 */
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100217 bool is_selected(const TestInfo &info) const;
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100218
219 /** Run all enabled test cases.
220 *
221 * @return True if all test cases executed successful.
222 */
223 bool run();
224
225 /** Set the result for an executed test case.
226 *
227 * @param[in] test_case_name Name of the executed test case.
228 * @param[in] result Execution result.
229 */
230 void set_test_result(std::string test_case_name, TestResult result);
231
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100232 /** Use the specified printer to output test results from the last run.
233 *
234 * This method can be used if the test results need to be obtained using a
235 * different printer than the one managed by the framework.
236 *
237 * @param[in] printer Printer used to output results.
238 */
239 void print_test_results(Printer &printer) const;
240
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100241 /** Factory method to obtain a configured profiler.
242 *
243 * The profiler enables all instruments that have been passed to the @ref
244 * init method.
245 *
246 * @return Configured profiler to collect benchmark results.
247 */
248 Profiler get_profiler() const;
249
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100250 /** Set the printer used for the output of test results.
251 *
252 * @param[in] printer Pointer to a printer.
253 */
254 void set_printer(Printer *printer);
255
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100256 /** List of @ref TestInfo's.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100257 *
258 * @return Vector with all test ids.
259 */
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100260 std::vector<Framework::TestInfo> test_infos() const;
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100261
262private:
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100263 Framework();
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100264 ~Framework() = default;
265
266 Framework(const Framework &) = delete;
267 Framework &operator=(const Framework &) = delete;
268
269 void run_test(TestCaseFactory &test_factory);
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100270 std::map<TestResult::Status, int> count_test_results() const;
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100271
272 /** Returns the current test suite name.
273 *
274 * @warning Cannot be used at execution time to get the test suite of the
275 * currently executed test case. It can only be used for registering test
276 * cases.
277 *
278 * @return Name of the current test suite.
279 */
280 std::string current_suite_name() const;
281
282 std::vector<std::string> _test_suite_name{};
283 std::vector<std::unique_ptr<TestCaseFactory>> _test_factories{};
284 std::map<std::string, TestResult> _test_results{};
285 std::chrono::seconds _runtime{ 0 };
286 int _num_iterations{ 1 };
287 bool _throw_errors{ false };
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100288 Printer *_printer{ nullptr };
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100289
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100290 using create_function = std::unique_ptr<Instrument>();
291 std::map<InstrumentType, create_function *> _available_instruments{};
292
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100293 InstrumentType _instruments{ InstrumentType::NONE };
294 std::regex _test_name_filter{ ".*" };
295 int64_t _test_id_filter{ -1 };
296 DatasetMode _dataset_mode{ DatasetMode::ALL };
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100297 LogLevel _log_level{ LogLevel::ALL };
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100298 TestResult *_current_test_result{ nullptr };
299 std::vector<std::string> _test_info{};
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100300};
301
302template <typename T>
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100303inline void Framework::add_test_case(std::string test_name, DatasetMode mode, TestCaseFactory::Status status)
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100304{
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100305 _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 +0100306}
307
308template <typename T, typename D>
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100309inline 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 +0100310{
311 // WORKAROUND for GCC 4.9
312 // The function should get *it which is tuple but that seems to trigger a
313 // bug in the compiler.
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100314 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 +0100315 std::move(description), *data));
316 _test_factories.emplace_back(std::move(tmp));
317}
318} // namespace framework
319} // namespace test
320} // namespace arm_compute
321#endif /* ARM_COMPUTE_TEST_FRAMEWORK */