1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2017-2021 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #ifndef ARM_COMPUTE_TEST_FRAMEWORK
25*c217d954SCole Faust #define ARM_COMPUTE_TEST_FRAMEWORK
26*c217d954SCole Faust
27*c217d954SCole Faust #include "DatasetModes.h"
28*c217d954SCole Faust #include "Exceptions.h"
29*c217d954SCole Faust #include "Profiler.h"
30*c217d954SCole Faust #include "TestCase.h"
31*c217d954SCole Faust #include "TestCaseFactory.h"
32*c217d954SCole Faust #include "TestResult.h"
33*c217d954SCole Faust #include "Utils.h"
34*c217d954SCole Faust #include "instruments/Instruments.h"
35*c217d954SCole Faust #include "printers/Printer.h"
36*c217d954SCole Faust
37*c217d954SCole Faust #include <algorithm>
38*c217d954SCole Faust #include <chrono>
39*c217d954SCole Faust #include <map>
40*c217d954SCole Faust #include <memory>
41*c217d954SCole Faust #include <numeric>
42*c217d954SCole Faust #include <ostream>
43*c217d954SCole Faust #include <set>
44*c217d954SCole Faust #include <sstream>
45*c217d954SCole Faust #include <string>
46*c217d954SCole Faust #include <vector>
47*c217d954SCole Faust
48*c217d954SCole Faust namespace arm_compute
49*c217d954SCole Faust {
50*c217d954SCole Faust namespace test
51*c217d954SCole Faust {
52*c217d954SCole Faust namespace framework
53*c217d954SCole Faust {
54*c217d954SCole Faust class TestFilter;
55*c217d954SCole Faust
56*c217d954SCole Faust /** Framework configuration structure */
57*c217d954SCole Faust struct FrameworkConfig
58*c217d954SCole Faust {
59*c217d954SCole Faust std::vector<framework::InstrumentsDescription> instruments{}; /**< Instrument types that will be used for benchmarking. */
60*c217d954SCole Faust std::string name_filter{}; /**< Regular expression to filter tests by name. Only matching tests will be executed. */
61*c217d954SCole Faust std::string id_filter{}; /**< String to match selected test ids. Only matching tests will be executed. */
62*c217d954SCole Faust DatasetMode mode{ DatasetMode::ALL }; /**< Dataset mode. */
63*c217d954SCole Faust int num_iterations{ 1 }; /**< Number of iterations per test. */
64*c217d954SCole Faust float cooldown_sec{ -1.f }; /**< Delay between tests in seconds. */
65*c217d954SCole Faust LogLevel log_level{ LogLevel::NONE }; /**< Verbosity of the output. */
66*c217d954SCole Faust bool configure_only{ false }; /**< Only configure kernels */
67*c217d954SCole Faust };
68*c217d954SCole Faust
69*c217d954SCole Faust /** Information about a test case.
70*c217d954SCole Faust *
71*c217d954SCole Faust * A test can be identified either via its id or via its name. Additionally
72*c217d954SCole Faust * each test is tagged with the data set mode in which it will be used and
73*c217d954SCole Faust * its status.
74*c217d954SCole Faust *
75*c217d954SCole Faust * @note The mapping between test id and test name is not guaranteed to be
76*c217d954SCole Faust * stable. It is subject to change as new test are added.
77*c217d954SCole Faust */
78*c217d954SCole Faust struct TestInfo
79*c217d954SCole Faust {
80*c217d954SCole Faust int id; /**< Test ID */
81*c217d954SCole Faust std::string name; /**< Test name */
82*c217d954SCole Faust DatasetMode mode; /**< Test data set mode */
83*c217d954SCole Faust TestCaseFactory::Status status; /**< Test status */
84*c217d954SCole Faust };
85*c217d954SCole Faust
86*c217d954SCole Faust inline bool operator<(const TestInfo &lhs, const TestInfo &rhs)
87*c217d954SCole Faust {
88*c217d954SCole Faust return lhs.id < rhs.id;
89*c217d954SCole Faust }
90*c217d954SCole Faust
91*c217d954SCole Faust /** Main framework class.
92*c217d954SCole Faust *
93*c217d954SCole Faust * Keeps track of the global state, owns all test cases and collects results.
94*c217d954SCole Faust */
95*c217d954SCole Faust class Framework final
96*c217d954SCole Faust {
97*c217d954SCole Faust public:
98*c217d954SCole Faust /** Access to the singleton.
99*c217d954SCole Faust *
100*c217d954SCole Faust * @return Unique instance of the framework class.
101*c217d954SCole Faust */
102*c217d954SCole Faust static Framework &get();
103*c217d954SCole Faust
104*c217d954SCole Faust /** Supported instrument types for benchmarking.
105*c217d954SCole Faust *
106*c217d954SCole Faust * @return Set of all available instrument types.
107*c217d954SCole Faust */
108*c217d954SCole Faust std::set<InstrumentsDescription> available_instruments() const;
109*c217d954SCole Faust
110*c217d954SCole Faust /** Init the framework.
111*c217d954SCole Faust *
112*c217d954SCole Faust * @see TestFilter::TestFilter for the format of the string to filter ids.
113*c217d954SCole Faust *
114*c217d954SCole Faust * @param[in] config Framework configuration meta-data.
115*c217d954SCole Faust */
116*c217d954SCole Faust void init(const FrameworkConfig &config);
117*c217d954SCole Faust
118*c217d954SCole Faust /** Add a new test suite.
119*c217d954SCole Faust *
120*c217d954SCole Faust * @warning Cannot be used at execution time. It can only be used for
121*c217d954SCole Faust * registering test cases.
122*c217d954SCole Faust *
123*c217d954SCole Faust * @param[in] name Name of the added test suite.
124*c217d954SCole Faust *
125*c217d954SCole Faust * @return Name of the current test suite.
126*c217d954SCole Faust */
127*c217d954SCole Faust void push_suite(std::string name);
128*c217d954SCole Faust
129*c217d954SCole Faust /** Remove innermost test suite.
130*c217d954SCole Faust *
131*c217d954SCole Faust * @warning Cannot be used at execution time. It can only be used for
132*c217d954SCole Faust * registering test cases.
133*c217d954SCole Faust */
134*c217d954SCole Faust void pop_suite();
135*c217d954SCole Faust
136*c217d954SCole Faust /** Add a test case to the framework.
137*c217d954SCole Faust *
138*c217d954SCole Faust * @param[in] test_name Name of the new test case.
139*c217d954SCole Faust * @param[in] mode Mode in which to include the test.
140*c217d954SCole Faust * @param[in] status Status of the test case.
141*c217d954SCole Faust */
142*c217d954SCole Faust template <typename T>
143*c217d954SCole Faust void add_test_case(std::string test_name, DatasetMode mode, TestCaseFactory::Status status);
144*c217d954SCole Faust
145*c217d954SCole Faust /** Add a data test case to the framework.
146*c217d954SCole Faust *
147*c217d954SCole Faust * @param[in] test_name Name of the new test case.
148*c217d954SCole Faust * @param[in] mode Mode in which to include the test.
149*c217d954SCole Faust * @param[in] status Status of the test case.
150*c217d954SCole Faust * @param[in] description Description of @p data.
151*c217d954SCole Faust * @param[in] data Data that will be used as input to the test.
152*c217d954SCole Faust */
153*c217d954SCole Faust template <typename T, typename D>
154*c217d954SCole Faust void add_data_test_case(std::string test_name, DatasetMode mode, TestCaseFactory::Status status, std::string description, D &&data);
155*c217d954SCole Faust
156*c217d954SCole Faust /** Add info string for the next expectation/assertion.
157*c217d954SCole Faust *
158*c217d954SCole Faust * @param[in] info Info string.
159*c217d954SCole Faust */
160*c217d954SCole Faust void add_test_info(std::string info);
161*c217d954SCole Faust
162*c217d954SCole Faust /** Clear the collected test info. */
163*c217d954SCole Faust void clear_test_info();
164*c217d954SCole Faust
165*c217d954SCole Faust /** Check if any info has been registered.
166*c217d954SCole Faust *
167*c217d954SCole Faust * @return True if there is test info.
168*c217d954SCole Faust */
169*c217d954SCole Faust bool has_test_info() const;
170*c217d954SCole Faust
171*c217d954SCole Faust /** Print test info.
172*c217d954SCole Faust *
173*c217d954SCole Faust * @param[out] os Output stream.
174*c217d954SCole Faust */
175*c217d954SCole Faust void print_test_info(std::ostream &os) const;
176*c217d954SCole Faust
177*c217d954SCole Faust /** Tell the framework that execution of a test starts.
178*c217d954SCole Faust *
179*c217d954SCole Faust * @param[in] info Test info.
180*c217d954SCole Faust */
181*c217d954SCole Faust void log_test_start(const TestInfo &info);
182*c217d954SCole Faust
183*c217d954SCole Faust /** Tell the framework that a test case is skipped.
184*c217d954SCole Faust *
185*c217d954SCole Faust * @param[in] info Test info.
186*c217d954SCole Faust */
187*c217d954SCole Faust void log_test_skipped(const TestInfo &info);
188*c217d954SCole Faust
189*c217d954SCole Faust /** Tell the framework that a test case finished.
190*c217d954SCole Faust *
191*c217d954SCole Faust * @param[in] info Test info.
192*c217d954SCole Faust */
193*c217d954SCole Faust void log_test_end(const TestInfo &info);
194*c217d954SCole Faust
195*c217d954SCole Faust /** Tell the framework that the currently running test case failed a non-fatal expectation.
196*c217d954SCole Faust *
197*c217d954SCole Faust * @param[in] error Description of the error.
198*c217d954SCole Faust */
199*c217d954SCole Faust void log_failed_expectation(const TestError &error);
200*c217d954SCole Faust
201*c217d954SCole Faust /** Print the debug information that has already been logged
202*c217d954SCole Faust *
203*c217d954SCole Faust * @param[in] info Description of the log info.
204*c217d954SCole Faust */
205*c217d954SCole Faust void log_info(const std::string &info);
206*c217d954SCole Faust
207*c217d954SCole Faust /** Number of iterations per test case.
208*c217d954SCole Faust *
209*c217d954SCole Faust * @return Number of iterations per test case.
210*c217d954SCole Faust */
211*c217d954SCole Faust int num_iterations() const;
212*c217d954SCole Faust
213*c217d954SCole Faust /** Set number of iterations per test case.
214*c217d954SCole Faust *
215*c217d954SCole Faust * @param[in] num_iterations Number of iterations per test case.
216*c217d954SCole Faust */
217*c217d954SCole Faust void set_num_iterations(int num_iterations);
218*c217d954SCole Faust
219*c217d954SCole Faust /** Should errors be caught or thrown by the framework.
220*c217d954SCole Faust *
221*c217d954SCole Faust * @return True if errors are thrown.
222*c217d954SCole Faust */
223*c217d954SCole Faust bool throw_errors() const;
224*c217d954SCole Faust
225*c217d954SCole Faust /** Set whether errors are caught or thrown by the framework.
226*c217d954SCole Faust *
227*c217d954SCole Faust * @param[in] throw_errors True if errors should be thrown.
228*c217d954SCole Faust */
229*c217d954SCole Faust void set_throw_errors(bool throw_errors);
230*c217d954SCole Faust
231*c217d954SCole Faust /** Indicates if test execution is stopped after the first failed test.
232*c217d954SCole Faust *
233*c217d954SCole Faust * @return True if the execution is going to be stopped after the first failed test.
234*c217d954SCole Faust */
235*c217d954SCole Faust bool stop_on_error() const;
236*c217d954SCole Faust
237*c217d954SCole Faust /** Set whether to stop execution after the first failed test.
238*c217d954SCole Faust *
239*c217d954SCole Faust * @param[in] stop_on_error True if execution is going to be stopped after first failed test.
240*c217d954SCole Faust */
241*c217d954SCole Faust void set_stop_on_error(bool stop_on_error);
242*c217d954SCole Faust
243*c217d954SCole Faust /** Indicates if a test should be marked as failed when its assets are missing.
244*c217d954SCole Faust *
245*c217d954SCole Faust * @return True if a test should be marked as failed when its assets are missing.
246*c217d954SCole Faust */
247*c217d954SCole Faust bool error_on_missing_assets() const;
248*c217d954SCole Faust
249*c217d954SCole Faust /** Set whether a test should be considered as failed if its assets cannot be found.
250*c217d954SCole Faust *
251*c217d954SCole Faust * @param[in] error_on_missing_assets True if a test should be marked as failed when its assets are missing.
252*c217d954SCole Faust */
253*c217d954SCole Faust void set_error_on_missing_assets(bool error_on_missing_assets);
254*c217d954SCole Faust
255*c217d954SCole Faust /** Run all enabled test cases.
256*c217d954SCole Faust *
257*c217d954SCole Faust * @return True if all test cases executed successful.
258*c217d954SCole Faust */
259*c217d954SCole Faust bool run();
260*c217d954SCole Faust
261*c217d954SCole Faust /** Set the result for an executed test case.
262*c217d954SCole Faust *
263*c217d954SCole Faust * @param[in] info Test info.
264*c217d954SCole Faust * @param[in] result Execution result.
265*c217d954SCole Faust */
266*c217d954SCole Faust void set_test_result(TestInfo info, TestResult result);
267*c217d954SCole Faust
268*c217d954SCole Faust /** Use the specified printer to output test results from the last run.
269*c217d954SCole Faust *
270*c217d954SCole Faust * This method can be used if the test results need to be obtained using a
271*c217d954SCole Faust * different printer than the one managed by the framework.
272*c217d954SCole Faust *
273*c217d954SCole Faust * @param[in] printer Printer used to output results.
274*c217d954SCole Faust */
275*c217d954SCole Faust void print_test_results(Printer &printer) const;
276*c217d954SCole Faust
277*c217d954SCole Faust /** Factory method to obtain a configured profiler.
278*c217d954SCole Faust *
279*c217d954SCole Faust * The profiler enables all instruments that have been passed to the @ref
280*c217d954SCole Faust * init method.
281*c217d954SCole Faust *
282*c217d954SCole Faust * @return Configured profiler to collect benchmark results.
283*c217d954SCole Faust */
284*c217d954SCole Faust Profiler get_profiler() const;
285*c217d954SCole Faust
286*c217d954SCole Faust /** Set the printer used for the output of test results.
287*c217d954SCole Faust *
288*c217d954SCole Faust * @param[in] printer Pointer to a printer.
289*c217d954SCole Faust */
290*c217d954SCole Faust void add_printer(Printer *printer);
291*c217d954SCole Faust
292*c217d954SCole Faust /** List of @ref TestInfo's.
293*c217d954SCole Faust *
294*c217d954SCole Faust * @return Vector with all test ids.
295*c217d954SCole Faust */
296*c217d954SCole Faust std::vector<TestInfo> test_infos() const;
297*c217d954SCole Faust
298*c217d954SCole Faust /** Get the current logging level
299*c217d954SCole Faust *
300*c217d954SCole Faust * @return The current logging level.
301*c217d954SCole Faust */
302*c217d954SCole Faust LogLevel log_level() const;
303*c217d954SCole Faust /** Sets instruments info
304*c217d954SCole Faust *
305*c217d954SCole Faust * @note TODO(COMPMID-2638) : Remove once instruments are transferred outside the framework.
306*c217d954SCole Faust *
307*c217d954SCole Faust * @param[in] instr_info Instruments info to set
308*c217d954SCole Faust */
309*c217d954SCole Faust void set_instruments_info(InstrumentsInfo instr_info);
310*c217d954SCole Faust /** Get the configure only flag
311*c217d954SCole Faust *
312*c217d954SCole Faust * @return The current configure only flag.
313*c217d954SCole Faust */
314*c217d954SCole Faust bool configure_only() const;
315*c217d954SCole Faust /** Return whether the new fixture has been called
316*c217d954SCole Faust *
317*c217d954SCole Faust * @return The current new fixture call flag.
318*c217d954SCole Faust */
319*c217d954SCole Faust bool new_fixture_call() const;
320*c217d954SCole Faust /** Set the new fixture call flag
321*c217d954SCole Faust *
322*c217d954SCole Faust * @param[in] val Value to set for the flag
323*c217d954SCole Faust */
324*c217d954SCole Faust void set_new_fixture_call(bool val);
325*c217d954SCole Faust
326*c217d954SCole Faust private:
327*c217d954SCole Faust Framework();
328*c217d954SCole Faust ~Framework() = default;
329*c217d954SCole Faust
330*c217d954SCole Faust Framework(const Framework &) = delete;
331*c217d954SCole Faust Framework &operator=(const Framework &) = delete;
332*c217d954SCole Faust
333*c217d954SCole Faust void run_test(const TestInfo &info, TestCaseFactory &test_factory);
334*c217d954SCole Faust std::map<TestResult::Status, int> count_test_results() const;
335*c217d954SCole Faust
336*c217d954SCole Faust /** Returns the current test suite name.
337*c217d954SCole Faust *
338*c217d954SCole Faust * @warning Cannot be used at execution time to get the test suite of the
339*c217d954SCole Faust * currently executed test case. It can only be used for registering test
340*c217d954SCole Faust * cases.
341*c217d954SCole Faust *
342*c217d954SCole Faust * @return Name of the current test suite.
343*c217d954SCole Faust */
344*c217d954SCole Faust std::string current_suite_name() const;
345*c217d954SCole Faust
346*c217d954SCole Faust /* Perform func on all printers */
347*c217d954SCole Faust template <typename F>
348*c217d954SCole Faust void func_on_all_printers(F &&func);
349*c217d954SCole Faust
350*c217d954SCole Faust std::vector<std::string> _test_suite_name{};
351*c217d954SCole Faust std::vector<std::unique_ptr<TestCaseFactory>> _test_factories{};
352*c217d954SCole Faust std::map<TestInfo, TestResult> _test_results{};
353*c217d954SCole Faust int _num_iterations{ 1 };
354*c217d954SCole Faust float _cooldown_sec{ -1.f };
355*c217d954SCole Faust bool _throw_errors{ false };
356*c217d954SCole Faust bool _stop_on_error{ false };
357*c217d954SCole Faust bool _error_on_missing_assets{ false };
358*c217d954SCole Faust std::vector<Printer *> _printers{};
359*c217d954SCole Faust bool _configure_only{ false };
360*c217d954SCole Faust bool _new_fixture_call{ false };
361*c217d954SCole Faust
362*c217d954SCole Faust using create_function = std::unique_ptr<Instrument>();
363*c217d954SCole Faust std::map<InstrumentsDescription, create_function *> _available_instruments{};
364*c217d954SCole Faust
365*c217d954SCole Faust std::set<framework::InstrumentsDescription> _instruments{ std::pair<InstrumentType, ScaleFactor>(InstrumentType::NONE, ScaleFactor::NONE) };
366*c217d954SCole Faust std::unique_ptr<TestFilter> _test_filter;
367*c217d954SCole Faust LogLevel _log_level{ LogLevel::ALL };
368*c217d954SCole Faust const TestInfo *_current_test_info{ nullptr };
369*c217d954SCole Faust TestResult *_current_test_result{ nullptr };
370*c217d954SCole Faust std::vector<std::string> _test_info{};
371*c217d954SCole Faust };
372*c217d954SCole Faust
373*c217d954SCole Faust template <typename T>
add_test_case(std::string test_name,DatasetMode mode,TestCaseFactory::Status status)374*c217d954SCole Faust inline void Framework::add_test_case(std::string test_name, DatasetMode mode, TestCaseFactory::Status status)
375*c217d954SCole Faust {
376*c217d954SCole Faust _test_factories.emplace_back(std::make_unique<SimpleTestCaseFactory<T>>(current_suite_name(), std::move(test_name), mode, status));
377*c217d954SCole Faust }
378*c217d954SCole Faust
379*c217d954SCole Faust template <typename T, typename D>
add_data_test_case(std::string test_name,DatasetMode mode,TestCaseFactory::Status status,std::string description,D && data)380*c217d954SCole Faust inline void Framework::add_data_test_case(std::string test_name, DatasetMode mode, TestCaseFactory::Status status, std::string description, D &&data)
381*c217d954SCole Faust {
382*c217d954SCole Faust // WORKAROUND for GCC 4.9
383*c217d954SCole Faust // The function should get *it which is tuple but that seems to trigger a
384*c217d954SCole Faust // bug in the compiler.
385*c217d954SCole Faust 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,
386*c217d954SCole Faust std::move(description), *data));
387*c217d954SCole Faust _test_factories.emplace_back(std::move(tmp));
388*c217d954SCole Faust }
389*c217d954SCole Faust } // namespace framework
390*c217d954SCole Faust } // namespace test
391*c217d954SCole Faust } // namespace arm_compute
392*c217d954SCole Faust #endif /* ARM_COMPUTE_TEST_FRAMEWORK */
393