blob: 0929b4ab2002dcb5d3e7978c0829cfb63ad5528a [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{
Moritz Pflanzer542002c2017-07-26 16:03:58 +010056/** Information about a test case.
57 *
58 * A test can be identified either via its id or via its name. Additionally
59 * each test is tagged with the data set mode in which it will be used and
60 * its status.
61 *
62 * @note The mapping between test id and test name is not guaranteed to be
63 * stable. It is subject to change as new test are added.
64 */
65struct TestInfo
66{
67 int id;
68 std::string name;
69 DatasetMode mode;
70 TestCaseFactory::Status status;
71};
72
73inline bool operator<(const TestInfo &lhs, const TestInfo &rhs)
74{
75 return lhs.id < rhs.id;
76}
77
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010078/** Main framework class.
79 *
80 * Keeps track of the global state, owns all test cases and collects results.
81 */
82class Framework final
83{
84public:
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010085 /** Access to the singleton.
86 *
87 * @return Unique instance of the framework class.
88 */
89 static Framework &get();
90
Moritz Pflanzera4f711b2017-07-05 11:02:23 +010091 /** Supported instrument types for benchmarking.
92 *
93 * @return Set of all available instrument types.
94 */
95 std::set<InstrumentType> available_instruments() const;
96
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010097 /** Init the framework.
98 *
Moritz Pflanzera4f711b2017-07-05 11:02:23 +010099 * @param[in] instruments Instrument types that will be used for benchmarking.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100100 * @param[in] num_iterations Number of iterations per test.
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100101 * @param[in] mode Dataset mode.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100102 * @param[in] name_filter Regular expression to filter tests by name. Only matching tests will be executed.
Moritz Pflanzer81527bf2017-07-20 15:11:33 +0100103 * @param[in] id_filter Test id. Only this test will be executed.
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100104 * @param[in] log_level Verbosity of the output.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100105 */
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100106 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 +0100107
108 /** Add a new test suite.
109 *
110 * @warning Cannot be used at execution time. It can only be used for
111 * registering test cases.
112 *
113 * @param[in] name Name of the added test suite.
114 *
115 * @return Name of the current test suite.
116 */
117 void push_suite(std::string name);
118
119 /** Remove innermost test suite.
120 *
121 * @warning Cannot be used at execution time. It can only be used for
122 * registering test cases.
123 */
124 void pop_suite();
125
126 /** Add a test case to the framework.
127 *
128 * @param[in] test_name Name of the new test case.
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100129 * @param[in] mode Mode in which to include the test.
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100130 * @param[in] status Status of the test case.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100131 */
132 template <typename T>
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100133 void add_test_case(std::string test_name, DatasetMode mode, TestCaseFactory::Status status);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100134
135 /** Add a data test case to the framework.
136 *
137 * @param[in] test_name Name of the new test case.
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100138 * @param[in] mode Mode in which to include the test.
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100139 * @param[in] status Status of the test case.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100140 * @param[in] description Description of @p data.
141 * @param[in] data Data that will be used as input to the test.
142 */
143 template <typename T, typename D>
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100144 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 +0100145
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100146 /** Add info string for the next expectation/assertion.
147 *
148 * @param[in] info Info string.
149 */
150 void add_test_info(std::string info);
151
152 /** Clear the collected test info. */
153 void clear_test_info();
154
155 /** Check if any info has been registered.
156 *
157 * @return True if there is test info.
158 */
159 bool has_test_info() const;
160
161 /** Print test info.
162 *
163 * @param[out] os Output stream.
164 */
165 void print_test_info(std::ostream &os) const;
166
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100167 /** Tell the framework that execution of a test starts.
168 *
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100169 * @param[in] info Test info.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100170 */
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100171 void log_test_start(const TestInfo &info);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100172
173 /** Tell the framework that a test case is skipped.
174 *
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100175 * @param[in] info Test info.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100176 */
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100177 void log_test_skipped(const TestInfo &info);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100178
179 /** Tell the framework that a test case finished.
180 *
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100181 * @param[in] info Test info.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100182 */
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100183 void log_test_end(const TestInfo &info);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100184
185 /** Tell the framework that the currently running test case failed a non-fatal expectation.
186 *
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100187 * @param[in] msg Description of the failure.
188 * @param[in] level Severity of the failed expectation.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100189 */
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100190 void log_failed_expectation(const std::string &msg, LogLevel level = LogLevel::ERRORS);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100191
192 /** Number of iterations per test case.
193 *
194 * @return Number of iterations per test case.
195 */
196 int num_iterations() const;
197
198 /** Set number of iterations per test case.
199 *
200 * @param[in] num_iterations Number of iterations per test case.
201 */
202 void set_num_iterations(int num_iterations);
203
204 /** Should errors be caught or thrown by the framework.
205 *
206 * @return True if errors are thrown.
207 */
208 bool throw_errors() const;
209
210 /** Set whether errors are caught or thrown by the framework.
211 *
212 * @param[in] throw_errors True if errors should be thrown.
213 */
214 void set_throw_errors(bool throw_errors);
215
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100216 /** Check if a test case is selected to be executed.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100217 *
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100218 * @param[in] info Test case info.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100219 *
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100220 * @return True if the test case is selected to be executed.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100221 */
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100222 bool is_selected(const TestInfo &info) const;
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100223
224 /** Run all enabled test cases.
225 *
226 * @return True if all test cases executed successful.
227 */
228 bool run();
229
230 /** Set the result for an executed test case.
231 *
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100232 * @param[in] info Test info.
233 * @param[in] result Execution result.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100234 */
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100235 void set_test_result(TestInfo info, TestResult result);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100236
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100237 /** Use the specified printer to output test results from the last run.
238 *
239 * This method can be used if the test results need to be obtained using a
240 * different printer than the one managed by the framework.
241 *
242 * @param[in] printer Printer used to output results.
243 */
244 void print_test_results(Printer &printer) const;
245
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100246 /** Factory method to obtain a configured profiler.
247 *
248 * The profiler enables all instruments that have been passed to the @ref
249 * init method.
250 *
251 * @return Configured profiler to collect benchmark results.
252 */
253 Profiler get_profiler() const;
254
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100255 /** Set the printer used for the output of test results.
256 *
257 * @param[in] printer Pointer to a printer.
258 */
259 void set_printer(Printer *printer);
260
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100261 /** List of @ref TestInfo's.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100262 *
263 * @return Vector with all test ids.
264 */
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100265 std::vector<TestInfo> test_infos() const;
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100266
267private:
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100268 Framework();
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100269 ~Framework() = default;
270
271 Framework(const Framework &) = delete;
272 Framework &operator=(const Framework &) = delete;
273
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100274 void run_test(const TestInfo &info, TestCaseFactory &test_factory);
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100275 std::map<TestResult::Status, int> count_test_results() const;
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100276
277 /** Returns the current test suite name.
278 *
279 * @warning Cannot be used at execution time to get the test suite of the
280 * currently executed test case. It can only be used for registering test
281 * cases.
282 *
283 * @return Name of the current test suite.
284 */
285 std::string current_suite_name() const;
286
287 std::vector<std::string> _test_suite_name{};
288 std::vector<std::unique_ptr<TestCaseFactory>> _test_factories{};
Moritz Pflanzer542002c2017-07-26 16:03:58 +0100289 std::map<TestInfo, TestResult> _test_results{};
290 int _num_iterations{ 1 };
291 bool _throw_errors{ false };
292 Printer *_printer{ nullptr };
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100293
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100294 using create_function = std::unique_ptr<Instrument>();
295 std::map<InstrumentType, create_function *> _available_instruments{};
296
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100297 InstrumentType _instruments{ InstrumentType::NONE };
298 std::regex _test_name_filter{ ".*" };
299 int64_t _test_id_filter{ -1 };
300 DatasetMode _dataset_mode{ DatasetMode::ALL };
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100301 LogLevel _log_level{ LogLevel::ALL };
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100302 TestResult *_current_test_result{ nullptr };
303 std::vector<std::string> _test_info{};
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100304};
305
306template <typename T>
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100307inline void Framework::add_test_case(std::string test_name, DatasetMode mode, TestCaseFactory::Status status)
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100308{
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100309 _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 +0100310}
311
312template <typename T, typename D>
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100313inline 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 +0100314{
315 // WORKAROUND for GCC 4.9
316 // The function should get *it which is tuple but that seems to trigger a
317 // bug in the compiler.
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100318 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 +0100319 std::move(description), *data));
320 _test_factories.emplace_back(std::move(tmp));
321}
322} // namespace framework
323} // namespace test
324} // namespace arm_compute
325#endif /* ARM_COMPUTE_TEST_FRAMEWORK */