blob: 4f60fed7211f5f4acd47e8ad9283f8eb126b025f [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:
62 /** Type of a test identifier.
63 *
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +010064 * A test can be identified either via its id or via its name. Additionally
65 * each test is tagged with the data set mode in which it will be used.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010066 *
67 * @note The mapping between test id and test name is not guaranteed to be
68 * stable. It is subject to change as new test are added.
69 */
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +010070 using TestId = std::tuple<int, std::string, DatasetMode>;
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010071
72 /** Access to the singleton.
73 *
74 * @return Unique instance of the framework class.
75 */
76 static Framework &get();
77
Moritz Pflanzera4f711b2017-07-05 11:02:23 +010078 /** Supported instrument types for benchmarking.
79 *
80 * @return Set of all available instrument types.
81 */
82 std::set<InstrumentType> available_instruments() const;
83
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010084 /** Init the framework.
85 *
Moritz Pflanzera4f711b2017-07-05 11:02:23 +010086 * @param[in] instruments Instrument types that will be used for benchmarking.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010087 * @param[in] num_iterations Number of iterations per test.
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +010088 * @param[in] mode Dataset mode.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010089 * @param[in] name_filter Regular expression to filter tests by name. Only matching tests will be executed.
Moritz Pflanzer81527bf2017-07-20 15:11:33 +010090 * @param[in] id_filter Test id. Only this test will be executed.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +010091 */
Moritz Pflanzer81527bf2017-07-20 15:11:33 +010092 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 +010093
94 /** Add a new test suite.
95 *
96 * @warning Cannot be used at execution time. It can only be used for
97 * registering test cases.
98 *
99 * @param[in] name Name of the added test suite.
100 *
101 * @return Name of the current test suite.
102 */
103 void push_suite(std::string name);
104
105 /** Remove innermost test suite.
106 *
107 * @warning Cannot be used at execution time. It can only be used for
108 * registering test cases.
109 */
110 void pop_suite();
111
112 /** Add a test case to the framework.
113 *
114 * @param[in] test_name Name of the new test case.
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100115 * @param[in] mode Mode in which to include the test.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100116 */
117 template <typename T>
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100118 void add_test_case(std::string test_name, DatasetMode mode);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100119
120 /** Add a data test case to the framework.
121 *
122 * @param[in] test_name Name of the new test case.
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100123 * @param[in] mode Mode in which to include the test.
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100124 * @param[in] description Description of @p data.
125 * @param[in] data Data that will be used as input to the test.
126 */
127 template <typename T, typename D>
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100128 void add_data_test_case(std::string test_name, DatasetMode mode, std::string description, D &&data);
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100129
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100130 /** Add info string for the next expectation/assertion.
131 *
132 * @param[in] info Info string.
133 */
134 void add_test_info(std::string info);
135
136 /** Clear the collected test info. */
137 void clear_test_info();
138
139 /** Check if any info has been registered.
140 *
141 * @return True if there is test info.
142 */
143 bool has_test_info() const;
144
145 /** Print test info.
146 *
147 * @param[out] os Output stream.
148 */
149 void print_test_info(std::ostream &os) const;
150
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100151 /** Tell the framework that execution of a test starts.
152 *
153 * @param[in] test_name Name of the started test case.
154 */
155 void log_test_start(const std::string &test_name);
156
157 /** Tell the framework that a test case is skipped.
158 *
159 * @param[in] test_name Name of the skipped test case.
160 */
161 void log_test_skipped(const std::string &test_name);
162
163 /** Tell the framework that a test case finished.
164 *
165 * @param[in] test_name Name of the finished test case.
166 */
167 void log_test_end(const std::string &test_name);
168
169 /** Tell the framework that the currently running test case failed a non-fatal expectation.
170 *
171 * @param[in] msg Description of the failure.
172 */
173 void log_failed_expectation(const std::string &msg);
174
175 /** Number of iterations per test case.
176 *
177 * @return Number of iterations per test case.
178 */
179 int num_iterations() const;
180
181 /** Set number of iterations per test case.
182 *
183 * @param[in] num_iterations Number of iterations per test case.
184 */
185 void set_num_iterations(int num_iterations);
186
187 /** Should errors be caught or thrown by the framework.
188 *
189 * @return True if errors are thrown.
190 */
191 bool throw_errors() const;
192
193 /** Set whether errors are caught or thrown by the framework.
194 *
195 * @param[in] throw_errors True if errors should be thrown.
196 */
197 void set_throw_errors(bool throw_errors);
198
199 /** Check if a test case would be executed.
200 *
201 * @param[in] id Id of the test case.
202 *
203 * @return True if the test case would be executed.
204 */
205 bool is_enabled(const TestId &id) const;
206
207 /** Run all enabled test cases.
208 *
209 * @return True if all test cases executed successful.
210 */
211 bool run();
212
213 /** Set the result for an executed test case.
214 *
215 * @param[in] test_case_name Name of the executed test case.
216 * @param[in] result Execution result.
217 */
218 void set_test_result(std::string test_case_name, TestResult result);
219
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100220 /** Use the specified printer to output test results from the last run.
221 *
222 * This method can be used if the test results need to be obtained using a
223 * different printer than the one managed by the framework.
224 *
225 * @param[in] printer Printer used to output results.
226 */
227 void print_test_results(Printer &printer) const;
228
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100229 /** Factory method to obtain a configured profiler.
230 *
231 * The profiler enables all instruments that have been passed to the @ref
232 * init method.
233 *
234 * @return Configured profiler to collect benchmark results.
235 */
236 Profiler get_profiler() const;
237
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100238 /** Set the printer used for the output of test results.
239 *
240 * @param[in] printer Pointer to a printer.
241 */
242 void set_printer(Printer *printer);
243
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100244 /** List of @ref TestId's.
245 *
246 * @return Vector with all test ids.
247 */
248 std::vector<Framework::TestId> test_ids() const;
249
250private:
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100251 Framework();
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100252 ~Framework() = default;
253
254 Framework(const Framework &) = delete;
255 Framework &operator=(const Framework &) = delete;
256
257 void run_test(TestCaseFactory &test_factory);
258 std::tuple<int, int, int> count_test_results() const;
259
260 /** Returns the current test suite name.
261 *
262 * @warning Cannot be used at execution time to get the test suite of the
263 * currently executed test case. It can only be used for registering test
264 * cases.
265 *
266 * @return Name of the current test suite.
267 */
268 std::string current_suite_name() const;
269
270 std::vector<std::string> _test_suite_name{};
271 std::vector<std::unique_ptr<TestCaseFactory>> _test_factories{};
272 std::map<std::string, TestResult> _test_results{};
273 std::chrono::seconds _runtime{ 0 };
274 int _num_iterations{ 1 };
275 bool _throw_errors{ false };
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100276 Printer *_printer{ nullptr };
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100277
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100278 using create_function = std::unique_ptr<Instrument>();
279 std::map<InstrumentType, create_function *> _available_instruments{};
280
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100281 InstrumentType _instruments{ InstrumentType::NONE };
282 std::regex _test_name_filter{ ".*" };
283 int64_t _test_id_filter{ -1 };
284 DatasetMode _dataset_mode{ DatasetMode::ALL };
285 TestResult *_current_test_result{ nullptr };
286 std::vector<std::string> _test_info{};
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100287};
288
289template <typename T>
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100290inline void Framework::add_test_case(std::string test_name, DatasetMode mode)
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100291{
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100292 _test_factories.emplace_back(support::cpp14::make_unique<SimpleTestCaseFactory<T>>(current_suite_name(), std::move(test_name), mode));
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100293}
294
295template <typename T, typename D>
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100296inline void Framework::add_data_test_case(std::string test_name, DatasetMode mode, std::string description, D &&data)
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100297{
298 // WORKAROUND for GCC 4.9
299 // The function should get *it which is tuple but that seems to trigger a
300 // bug in the compiler.
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100301 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,
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100302 std::move(description), *data));
303 _test_factories.emplace_back(std::move(tmp));
304}
305} // namespace framework
306} // namespace test
307} // namespace arm_compute
308#endif /* ARM_COMPUTE_TEST_FRAMEWORK */