blob: 6bf3f18f3f697d949535ad818ca7e9cc4a999ba5 [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 Pflanzera4f711b2017-07-05 11:02:23 +010028#include "Profiler.h"
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010029#include "TestCase.h"
30#include "TestCaseFactory.h"
31#include "TestResult.h"
32#include "Utils.h"
Moritz Pflanzera4f711b2017-07-05 11:02:23 +010033#include "instruments/Instruments.h"
Moritz Pflanzer80fffae2017-07-05 11:02:37 +010034#include "printers/Printer.h"
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010035
36#include <algorithm>
37#include <chrono>
38#include <map>
39#include <memory>
40#include <numeric>
41#include <ostream>
42#include <regex>
43#include <set>
44#include <sstream>
45#include <string>
46#include <tuple>
47#include <vector>
48
49namespace arm_compute
50{
51namespace test
52{
53namespace framework
54{
55/** Main framework class.
56 *
57 * Keeps track of the global state, owns all test cases and collects results.
58 */
59class Framework final
60{
61public:
Moritz Pflanzerbf234e02017-07-24 15:04:14 +010062 /** Information about a test case.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010063 *
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +010064 * A test can be identified either via its id or via its name. Additionally
Moritz Pflanzerbf234e02017-07-24 15:04:14 +010065 * each test is tagged with the data set mode in which it will be used and
66 * its status.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010067 *
68 * @note The mapping between test id and test name is not guaranteed to be
69 * stable. It is subject to change as new test are added.
70 */
Moritz Pflanzerbf234e02017-07-24 15:04:14 +010071 struct TestInfo
72 {
73 int id;
74 std::string name;
75 DatasetMode mode;
76 TestCaseFactory::Status status;
77 };
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010078
79 /** Access to the singleton.
80 *
81 * @return Unique instance of the framework class.
82 */
83 static Framework &get();
84
Moritz Pflanzera4f711b2017-07-05 11:02:23 +010085 /** Supported instrument types for benchmarking.
86 *
87 * @return Set of all available instrument types.
88 */
89 std::set<InstrumentType> available_instruments() const;
90
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010091 /** Init the framework.
92 *
Moritz Pflanzera4f711b2017-07-05 11:02:23 +010093 * @param[in] instruments Instrument types that will be used for benchmarking.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010094 * @param[in] num_iterations Number of iterations per test.
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +010095 * @param[in] mode Dataset mode.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010096 * @param[in] name_filter Regular expression to filter tests by name. Only matching tests will be executed.
Moritz Pflanzer81527bf2017-07-20 15:11:33 +010097 * @param[in] id_filter Test id. Only this test will be executed.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010098 */
Moritz Pflanzer81527bf2017-07-20 15:11:33 +010099 void init(const std::vector<InstrumentType> &instruments, int num_iterations, DatasetMode mode, const std::string &name_filter, int64_t id_filter);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100100
101 /** Add a new test suite.
102 *
103 * @warning Cannot be used at execution time. It can only be used for
104 * registering test cases.
105 *
106 * @param[in] name Name of the added test suite.
107 *
108 * @return Name of the current test suite.
109 */
110 void push_suite(std::string name);
111
112 /** Remove innermost test suite.
113 *
114 * @warning Cannot be used at execution time. It can only be used for
115 * registering test cases.
116 */
117 void pop_suite();
118
119 /** Add a test case to the framework.
120 *
121 * @param[in] test_name Name of the new test case.
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100122 * @param[in] mode Mode in which to include the test.
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100123 * @param[in] status Status of the test case.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100124 */
125 template <typename T>
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100126 void add_test_case(std::string test_name, DatasetMode mode, TestCaseFactory::Status status);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100127
128 /** Add a data test case to the framework.
129 *
130 * @param[in] test_name Name of the new test case.
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100131 * @param[in] mode Mode in which to include the test.
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100132 * @param[in] status Status of the test case.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100133 * @param[in] description Description of @p data.
134 * @param[in] data Data that will be used as input to the test.
135 */
136 template <typename T, typename D>
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100137 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 +0100138
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100139 /** Add info string for the next expectation/assertion.
140 *
141 * @param[in] info Info string.
142 */
143 void add_test_info(std::string info);
144
145 /** Clear the collected test info. */
146 void clear_test_info();
147
148 /** Check if any info has been registered.
149 *
150 * @return True if there is test info.
151 */
152 bool has_test_info() const;
153
154 /** Print test info.
155 *
156 * @param[out] os Output stream.
157 */
158 void print_test_info(std::ostream &os) const;
159
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100160 /** Tell the framework that execution of a test starts.
161 *
162 * @param[in] test_name Name of the started test case.
163 */
164 void log_test_start(const std::string &test_name);
165
166 /** Tell the framework that a test case is skipped.
167 *
168 * @param[in] test_name Name of the skipped test case.
169 */
170 void log_test_skipped(const std::string &test_name);
171
172 /** Tell the framework that a test case finished.
173 *
174 * @param[in] test_name Name of the finished test case.
175 */
176 void log_test_end(const std::string &test_name);
177
178 /** Tell the framework that the currently running test case failed a non-fatal expectation.
179 *
180 * @param[in] msg Description of the failure.
181 */
182 void log_failed_expectation(const std::string &msg);
183
184 /** Number of iterations per test case.
185 *
186 * @return Number of iterations per test case.
187 */
188 int num_iterations() const;
189
190 /** Set number of iterations per test case.
191 *
192 * @param[in] num_iterations Number of iterations per test case.
193 */
194 void set_num_iterations(int num_iterations);
195
196 /** Should errors be caught or thrown by the framework.
197 *
198 * @return True if errors are thrown.
199 */
200 bool throw_errors() const;
201
202 /** Set whether errors are caught or thrown by the framework.
203 *
204 * @param[in] throw_errors True if errors should be thrown.
205 */
206 void set_throw_errors(bool throw_errors);
207
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100208 /** Check if a test case is selected to be executed.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100209 *
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100210 * @param[in] info Test case info.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100211 *
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100212 * @return True if the test case is selected to be executed.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100213 */
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100214 bool is_selected(const TestInfo &info) const;
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100215
216 /** Run all enabled test cases.
217 *
218 * @return True if all test cases executed successful.
219 */
220 bool run();
221
222 /** Set the result for an executed test case.
223 *
224 * @param[in] test_case_name Name of the executed test case.
225 * @param[in] result Execution result.
226 */
227 void set_test_result(std::string test_case_name, TestResult result);
228
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100229 /** Use the specified printer to output test results from the last run.
230 *
231 * This method can be used if the test results need to be obtained using a
232 * different printer than the one managed by the framework.
233 *
234 * @param[in] printer Printer used to output results.
235 */
236 void print_test_results(Printer &printer) const;
237
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100238 /** Factory method to obtain a configured profiler.
239 *
240 * The profiler enables all instruments that have been passed to the @ref
241 * init method.
242 *
243 * @return Configured profiler to collect benchmark results.
244 */
245 Profiler get_profiler() const;
246
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100247 /** Set the printer used for the output of test results.
248 *
249 * @param[in] printer Pointer to a printer.
250 */
251 void set_printer(Printer *printer);
252
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100253 /** List of @ref TestInfo's.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100254 *
255 * @return Vector with all test ids.
256 */
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100257 std::vector<Framework::TestInfo> test_infos() const;
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100258
259private:
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100260 Framework();
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100261 ~Framework() = default;
262
263 Framework(const Framework &) = delete;
264 Framework &operator=(const Framework &) = delete;
265
266 void run_test(TestCaseFactory &test_factory);
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100267 std::map<TestResult::Status, int> count_test_results() const;
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100268
269 /** Returns the current test suite name.
270 *
271 * @warning Cannot be used at execution time to get the test suite of the
272 * currently executed test case. It can only be used for registering test
273 * cases.
274 *
275 * @return Name of the current test suite.
276 */
277 std::string current_suite_name() const;
278
279 std::vector<std::string> _test_suite_name{};
280 std::vector<std::unique_ptr<TestCaseFactory>> _test_factories{};
281 std::map<std::string, TestResult> _test_results{};
282 std::chrono::seconds _runtime{ 0 };
283 int _num_iterations{ 1 };
284 bool _throw_errors{ false };
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100285 Printer *_printer{ nullptr };
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100286
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100287 using create_function = std::unique_ptr<Instrument>();
288 std::map<InstrumentType, create_function *> _available_instruments{};
289
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100290 InstrumentType _instruments{ InstrumentType::NONE };
291 std::regex _test_name_filter{ ".*" };
292 int64_t _test_id_filter{ -1 };
293 DatasetMode _dataset_mode{ DatasetMode::ALL };
294 TestResult *_current_test_result{ nullptr };
295 std::vector<std::string> _test_info{};
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100296};
297
298template <typename T>
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100299inline void Framework::add_test_case(std::string test_name, DatasetMode mode, TestCaseFactory::Status status)
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100300{
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100301 _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 +0100302}
303
304template <typename T, typename D>
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100305inline 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 +0100306{
307 // WORKAROUND for GCC 4.9
308 // The function should get *it which is tuple but that seems to trigger a
309 // bug in the compiler.
Moritz Pflanzerbf234e02017-07-24 15:04:14 +0100310 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 +0100311 std::move(description), *data));
312 _test_factories.emplace_back(std::move(tmp));
313}
314} // namespace framework
315} // namespace test
316} // namespace arm_compute
317#endif /* ARM_COMPUTE_TEST_FRAMEWORK */