blob: a7d8a15541440d18f201886c0dadc792fb7b26f9 [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.
90 * @param[in] id_filter Regular expression to filter tests by id. Only matching tests will be executed.
91 */
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +010092 void init(const std::vector<InstrumentType> &instruments, int num_iterations, DatasetMode mode, const std::string &name_filter, const std::string &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
130 /** Tell the framework that execution of a test starts.
131 *
132 * @param[in] test_name Name of the started test case.
133 */
134 void log_test_start(const std::string &test_name);
135
136 /** Tell the framework that a test case is skipped.
137 *
138 * @param[in] test_name Name of the skipped test case.
139 */
140 void log_test_skipped(const std::string &test_name);
141
142 /** Tell the framework that a test case finished.
143 *
144 * @param[in] test_name Name of the finished test case.
145 */
146 void log_test_end(const std::string &test_name);
147
148 /** Tell the framework that the currently running test case failed a non-fatal expectation.
149 *
150 * @param[in] msg Description of the failure.
151 */
152 void log_failed_expectation(const std::string &msg);
153
154 /** Number of iterations per test case.
155 *
156 * @return Number of iterations per test case.
157 */
158 int num_iterations() const;
159
160 /** Set number of iterations per test case.
161 *
162 * @param[in] num_iterations Number of iterations per test case.
163 */
164 void set_num_iterations(int num_iterations);
165
166 /** Should errors be caught or thrown by the framework.
167 *
168 * @return True if errors are thrown.
169 */
170 bool throw_errors() const;
171
172 /** Set whether errors are caught or thrown by the framework.
173 *
174 * @param[in] throw_errors True if errors should be thrown.
175 */
176 void set_throw_errors(bool throw_errors);
177
178 /** Check if a test case would be executed.
179 *
180 * @param[in] id Id of the test case.
181 *
182 * @return True if the test case would be executed.
183 */
184 bool is_enabled(const TestId &id) const;
185
186 /** Run all enabled test cases.
187 *
188 * @return True if all test cases executed successful.
189 */
190 bool run();
191
192 /** Set the result for an executed test case.
193 *
194 * @param[in] test_case_name Name of the executed test case.
195 * @param[in] result Execution result.
196 */
197 void set_test_result(std::string test_case_name, TestResult result);
198
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100199 /** Use the specified printer to output test results from the last run.
200 *
201 * This method can be used if the test results need to be obtained using a
202 * different printer than the one managed by the framework.
203 *
204 * @param[in] printer Printer used to output results.
205 */
206 void print_test_results(Printer &printer) const;
207
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100208 /** Factory method to obtain a configured profiler.
209 *
210 * The profiler enables all instruments that have been passed to the @ref
211 * init method.
212 *
213 * @return Configured profiler to collect benchmark results.
214 */
215 Profiler get_profiler() const;
216
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100217 /** Set the printer used for the output of test results.
218 *
219 * @param[in] printer Pointer to a printer.
220 */
221 void set_printer(Printer *printer);
222
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100223 /** List of @ref TestId's.
224 *
225 * @return Vector with all test ids.
226 */
227 std::vector<Framework::TestId> test_ids() const;
228
229private:
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100230 Framework();
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100231 ~Framework() = default;
232
233 Framework(const Framework &) = delete;
234 Framework &operator=(const Framework &) = delete;
235
236 void run_test(TestCaseFactory &test_factory);
237 std::tuple<int, int, int> count_test_results() const;
238
239 /** Returns the current test suite name.
240 *
241 * @warning Cannot be used at execution time to get the test suite of the
242 * currently executed test case. It can only be used for registering test
243 * cases.
244 *
245 * @return Name of the current test suite.
246 */
247 std::string current_suite_name() const;
248
249 std::vector<std::string> _test_suite_name{};
250 std::vector<std::unique_ptr<TestCaseFactory>> _test_factories{};
251 std::map<std::string, TestResult> _test_results{};
252 std::chrono::seconds _runtime{ 0 };
253 int _num_iterations{ 1 };
254 bool _throw_errors{ false };
Moritz Pflanzer80fffae2017-07-05 11:02:37 +0100255 Printer *_printer{ nullptr };
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100256
Moritz Pflanzera4f711b2017-07-05 11:02:23 +0100257 using create_function = std::unique_ptr<Instrument>();
258 std::map<InstrumentType, create_function *> _available_instruments{};
259
260 InstrumentType _instruments{ InstrumentType::NONE };
261 std::regex _test_name_filter{ ".*" };
262 std::regex _test_id_filter{ ".*" };
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100263 DatasetMode _dataset_mode{ DatasetMode::ALL };
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100264};
265
266template <typename T>
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100267inline void Framework::add_test_case(std::string test_name, DatasetMode mode)
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100268{
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100269 _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 +0100270}
271
272template <typename T, typename D>
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100273inline void Framework::add_data_test_case(std::string test_name, DatasetMode mode, std::string description, D &&data)
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100274{
275 // WORKAROUND for GCC 4.9
276 // The function should get *it which is tuple but that seems to trigger a
277 // bug in the compiler.
Moritz Pflanzerd03b00a2017-07-17 13:50:12 +0100278 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 +0100279 std::move(description), *data));
280 _test_factories.emplace_back(std::move(tmp));
281}
282} // namespace framework
283} // namespace test
284} // namespace arm_compute
285#endif /* ARM_COMPUTE_TEST_FRAMEWORK */