Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 1 | /* |
| 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 Pflanzer | d03b00a | 2017-07-17 13:50:12 +0100 | [diff] [blame^] | 27 | #include "DatasetModes.h" |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 28 | #include "Profiler.h" |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 29 | #include "TestCase.h" |
| 30 | #include "TestCaseFactory.h" |
| 31 | #include "TestResult.h" |
| 32 | #include "Utils.h" |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 33 | #include "instruments/Instruments.h" |
Moritz Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 34 | #include "printers/Printer.h" |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 35 | |
| 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 | |
| 49 | namespace arm_compute |
| 50 | { |
| 51 | namespace test |
| 52 | { |
| 53 | namespace framework |
| 54 | { |
| 55 | /** Main framework class. |
| 56 | * |
| 57 | * Keeps track of the global state, owns all test cases and collects results. |
| 58 | */ |
| 59 | class Framework final |
| 60 | { |
| 61 | public: |
| 62 | /** Type of a test identifier. |
| 63 | * |
Moritz Pflanzer | d03b00a | 2017-07-17 13:50:12 +0100 | [diff] [blame^] | 64 | * 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 Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 66 | * |
| 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 Pflanzer | d03b00a | 2017-07-17 13:50:12 +0100 | [diff] [blame^] | 70 | using TestId = std::tuple<int, std::string, DatasetMode>; |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 71 | |
| 72 | /** Access to the singleton. |
| 73 | * |
| 74 | * @return Unique instance of the framework class. |
| 75 | */ |
| 76 | static Framework &get(); |
| 77 | |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 78 | /** Supported instrument types for benchmarking. |
| 79 | * |
| 80 | * @return Set of all available instrument types. |
| 81 | */ |
| 82 | std::set<InstrumentType> available_instruments() const; |
| 83 | |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 84 | /** Init the framework. |
| 85 | * |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 86 | * @param[in] instruments Instrument types that will be used for benchmarking. |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 87 | * @param[in] num_iterations Number of iterations per test. |
Moritz Pflanzer | d03b00a | 2017-07-17 13:50:12 +0100 | [diff] [blame^] | 88 | * @param[in] mode Dataset mode. |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 89 | * @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 Pflanzer | d03b00a | 2017-07-17 13:50:12 +0100 | [diff] [blame^] | 92 | void init(const std::vector<InstrumentType> &instruments, int num_iterations, DatasetMode mode, const std::string &name_filter, const std::string &id_filter); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 93 | |
| 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 Pflanzer | d03b00a | 2017-07-17 13:50:12 +0100 | [diff] [blame^] | 115 | * @param[in] mode Mode in which to include the test. |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 116 | */ |
| 117 | template <typename T> |
Moritz Pflanzer | d03b00a | 2017-07-17 13:50:12 +0100 | [diff] [blame^] | 118 | void add_test_case(std::string test_name, DatasetMode mode); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 119 | |
| 120 | /** Add a data test case to the framework. |
| 121 | * |
| 122 | * @param[in] test_name Name of the new test case. |
Moritz Pflanzer | d03b00a | 2017-07-17 13:50:12 +0100 | [diff] [blame^] | 123 | * @param[in] mode Mode in which to include the test. |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 124 | * @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 Pflanzer | d03b00a | 2017-07-17 13:50:12 +0100 | [diff] [blame^] | 128 | void add_data_test_case(std::string test_name, DatasetMode mode, std::string description, D &&data); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 129 | |
| 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 Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 199 | /** 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 Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 208 | /** 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 Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 217 | /** 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 Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 223 | /** List of @ref TestId's. |
| 224 | * |
| 225 | * @return Vector with all test ids. |
| 226 | */ |
| 227 | std::vector<Framework::TestId> test_ids() const; |
| 228 | |
| 229 | private: |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 230 | Framework(); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 231 | ~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 Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 255 | Printer *_printer{ nullptr }; |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 256 | |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 257 | 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 Pflanzer | d03b00a | 2017-07-17 13:50:12 +0100 | [diff] [blame^] | 263 | DatasetMode _dataset_mode{ DatasetMode::ALL }; |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 264 | }; |
| 265 | |
| 266 | template <typename T> |
Moritz Pflanzer | d03b00a | 2017-07-17 13:50:12 +0100 | [diff] [blame^] | 267 | inline void Framework::add_test_case(std::string test_name, DatasetMode mode) |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 268 | { |
Moritz Pflanzer | d03b00a | 2017-07-17 13:50:12 +0100 | [diff] [blame^] | 269 | _test_factories.emplace_back(support::cpp14::make_unique<SimpleTestCaseFactory<T>>(current_suite_name(), std::move(test_name), mode)); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | template <typename T, typename D> |
Moritz Pflanzer | d03b00a | 2017-07-17 13:50:12 +0100 | [diff] [blame^] | 273 | inline void Framework::add_data_test_case(std::string test_name, DatasetMode mode, std::string description, D &&data) |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 274 | { |
| 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 Pflanzer | d03b00a | 2017-07-17 13:50:12 +0100 | [diff] [blame^] | 278 | 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 Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 279 | 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 */ |