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 | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 28 | #include "Exceptions.h" |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 29 | #include "Profiler.h" |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 30 | #include "TestCase.h" |
| 31 | #include "TestCaseFactory.h" |
Moritz Pflanzer | ec2de0f | 2017-07-27 14:43:46 +0100 | [diff] [blame] | 32 | #include "TestFilter.h" |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 33 | #include "TestResult.h" |
| 34 | #include "Utils.h" |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 35 | #include "instruments/Instruments.h" |
Moritz Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 36 | #include "printers/Printer.h" |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 37 | |
| 38 | #include <algorithm> |
| 39 | #include <chrono> |
| 40 | #include <map> |
| 41 | #include <memory> |
| 42 | #include <numeric> |
| 43 | #include <ostream> |
| 44 | #include <regex> |
| 45 | #include <set> |
| 46 | #include <sstream> |
| 47 | #include <string> |
| 48 | #include <tuple> |
| 49 | #include <vector> |
| 50 | |
| 51 | namespace arm_compute |
| 52 | { |
| 53 | namespace test |
| 54 | { |
| 55 | namespace framework |
| 56 | { |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 57 | /** Information about a test case. |
| 58 | * |
| 59 | * A test can be identified either via its id or via its name. Additionally |
| 60 | * each test is tagged with the data set mode in which it will be used and |
| 61 | * its status. |
| 62 | * |
| 63 | * @note The mapping between test id and test name is not guaranteed to be |
| 64 | * stable. It is subject to change as new test are added. |
| 65 | */ |
| 66 | struct TestInfo |
| 67 | { |
| 68 | int id; |
| 69 | std::string name; |
| 70 | DatasetMode mode; |
| 71 | TestCaseFactory::Status status; |
| 72 | }; |
| 73 | |
| 74 | inline bool operator<(const TestInfo &lhs, const TestInfo &rhs) |
| 75 | { |
| 76 | return lhs.id < rhs.id; |
| 77 | } |
| 78 | |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 79 | /** Main framework class. |
| 80 | * |
| 81 | * Keeps track of the global state, owns all test cases and collects results. |
| 82 | */ |
| 83 | class Framework final |
| 84 | { |
| 85 | public: |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 86 | /** Access to the singleton. |
| 87 | * |
| 88 | * @return Unique instance of the framework class. |
| 89 | */ |
| 90 | static Framework &get(); |
| 91 | |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 92 | /** Supported instrument types for benchmarking. |
| 93 | * |
| 94 | * @return Set of all available instrument types. |
| 95 | */ |
| 96 | std::set<InstrumentType> available_instruments() const; |
| 97 | |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 98 | /** Init the framework. |
| 99 | * |
Moritz Pflanzer | ec2de0f | 2017-07-27 14:43:46 +0100 | [diff] [blame] | 100 | * @see TestFilter::TestFilter for the format of the string to filter ids. |
| 101 | * |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 102 | * @param[in] instruments Instrument types that will be used for benchmarking. |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 103 | * @param[in] num_iterations Number of iterations per test. |
Moritz Pflanzer | d03b00a | 2017-07-17 13:50:12 +0100 | [diff] [blame] | 104 | * @param[in] mode Dataset mode. |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 105 | * @param[in] name_filter Regular expression to filter tests by name. Only matching tests will be executed. |
Moritz Pflanzer | ec2de0f | 2017-07-27 14:43:46 +0100 | [diff] [blame] | 106 | * @param[in] id_filter String to match selected test ids. Only matching tests will be executed. |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 107 | * @param[in] log_level Verbosity of the output. |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 108 | */ |
Moritz Pflanzer | ec2de0f | 2017-07-27 14:43:46 +0100 | [diff] [blame] | 109 | void init(const std::vector<InstrumentType> &instruments, int num_iterations, DatasetMode mode, const std::string &name_filter, const std::string &id_filter, LogLevel log_level); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 110 | |
| 111 | /** Add a new test suite. |
| 112 | * |
| 113 | * @warning Cannot be used at execution time. It can only be used for |
| 114 | * registering test cases. |
| 115 | * |
| 116 | * @param[in] name Name of the added test suite. |
| 117 | * |
| 118 | * @return Name of the current test suite. |
| 119 | */ |
| 120 | void push_suite(std::string name); |
| 121 | |
| 122 | /** Remove innermost test suite. |
| 123 | * |
| 124 | * @warning Cannot be used at execution time. It can only be used for |
| 125 | * registering test cases. |
| 126 | */ |
| 127 | void pop_suite(); |
| 128 | |
| 129 | /** Add a test case to the framework. |
| 130 | * |
| 131 | * @param[in] test_name Name of the new test case. |
Moritz Pflanzer | d03b00a | 2017-07-17 13:50:12 +0100 | [diff] [blame] | 132 | * @param[in] mode Mode in which to include the test. |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 133 | * @param[in] status Status of the test case. |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 134 | */ |
| 135 | template <typename T> |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 136 | void add_test_case(std::string test_name, DatasetMode mode, TestCaseFactory::Status status); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 137 | |
| 138 | /** Add a data test case to the framework. |
| 139 | * |
| 140 | * @param[in] test_name Name of the new test case. |
Moritz Pflanzer | d03b00a | 2017-07-17 13:50:12 +0100 | [diff] [blame] | 141 | * @param[in] mode Mode in which to include the test. |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 142 | * @param[in] status Status of the test case. |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 143 | * @param[in] description Description of @p data. |
| 144 | * @param[in] data Data that will be used as input to the test. |
| 145 | */ |
| 146 | template <typename T, typename D> |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 147 | void add_data_test_case(std::string test_name, DatasetMode mode, TestCaseFactory::Status status, std::string description, D &&data); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 148 | |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 149 | /** Add info string for the next expectation/assertion. |
| 150 | * |
| 151 | * @param[in] info Info string. |
| 152 | */ |
| 153 | void add_test_info(std::string info); |
| 154 | |
| 155 | /** Clear the collected test info. */ |
| 156 | void clear_test_info(); |
| 157 | |
| 158 | /** Check if any info has been registered. |
| 159 | * |
| 160 | * @return True if there is test info. |
| 161 | */ |
| 162 | bool has_test_info() const; |
| 163 | |
| 164 | /** Print test info. |
| 165 | * |
| 166 | * @param[out] os Output stream. |
| 167 | */ |
| 168 | void print_test_info(std::ostream &os) const; |
| 169 | |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 170 | /** Tell the framework that execution of a test starts. |
| 171 | * |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 172 | * @param[in] info Test info. |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 173 | */ |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 174 | void log_test_start(const TestInfo &info); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 175 | |
| 176 | /** Tell the framework that a test case is skipped. |
| 177 | * |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 178 | * @param[in] info Test info. |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 179 | */ |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 180 | void log_test_skipped(const TestInfo &info); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 181 | |
| 182 | /** Tell the framework that a test case finished. |
| 183 | * |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 184 | * @param[in] info Test info. |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 185 | */ |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 186 | void log_test_end(const TestInfo &info); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 187 | |
| 188 | /** Tell the framework that the currently running test case failed a non-fatal expectation. |
| 189 | * |
Moritz Pflanzer | 24a8246 | 2017-08-04 11:34:44 +0100 | [diff] [blame] | 190 | * @param[in] error Description of the error. |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 191 | */ |
Moritz Pflanzer | 24a8246 | 2017-08-04 11:34:44 +0100 | [diff] [blame] | 192 | void log_failed_expectation(const TestError &error); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 193 | |
| 194 | /** Number of iterations per test case. |
| 195 | * |
| 196 | * @return Number of iterations per test case. |
| 197 | */ |
| 198 | int num_iterations() const; |
| 199 | |
| 200 | /** Set number of iterations per test case. |
| 201 | * |
| 202 | * @param[in] num_iterations Number of iterations per test case. |
| 203 | */ |
| 204 | void set_num_iterations(int num_iterations); |
| 205 | |
| 206 | /** Should errors be caught or thrown by the framework. |
| 207 | * |
| 208 | * @return True if errors are thrown. |
| 209 | */ |
| 210 | bool throw_errors() const; |
| 211 | |
| 212 | /** Set whether errors are caught or thrown by the framework. |
| 213 | * |
| 214 | * @param[in] throw_errors True if errors should be thrown. |
| 215 | */ |
| 216 | void set_throw_errors(bool throw_errors); |
| 217 | |
Moritz Pflanzer | fa81165 | 2017-07-26 17:00:37 +0100 | [diff] [blame] | 218 | /** Indicates if test execution is stopped after the first failed test. |
| 219 | * |
| 220 | * @return True if the execution is going to be aborted after the first failed test. |
| 221 | */ |
| 222 | bool stop_on_error() const; |
| 223 | |
| 224 | /** Set whether to abort execution after the first failed test. |
| 225 | * |
| 226 | * @param[in] stop_on_error True if execution is going to be aborted after first failed test. |
| 227 | */ |
| 228 | void set_stop_on_error(bool stop_on_error); |
| 229 | |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 230 | /** Run all enabled test cases. |
| 231 | * |
| 232 | * @return True if all test cases executed successful. |
| 233 | */ |
| 234 | bool run(); |
| 235 | |
| 236 | /** Set the result for an executed test case. |
| 237 | * |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 238 | * @param[in] info Test info. |
| 239 | * @param[in] result Execution result. |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 240 | */ |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 241 | void set_test_result(TestInfo info, TestResult result); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 242 | |
Moritz Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 243 | /** Use the specified printer to output test results from the last run. |
| 244 | * |
| 245 | * This method can be used if the test results need to be obtained using a |
| 246 | * different printer than the one managed by the framework. |
| 247 | * |
| 248 | * @param[in] printer Printer used to output results. |
| 249 | */ |
| 250 | void print_test_results(Printer &printer) const; |
| 251 | |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 252 | /** Factory method to obtain a configured profiler. |
| 253 | * |
| 254 | * The profiler enables all instruments that have been passed to the @ref |
| 255 | * init method. |
| 256 | * |
| 257 | * @return Configured profiler to collect benchmark results. |
| 258 | */ |
| 259 | Profiler get_profiler() const; |
| 260 | |
Moritz Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 261 | /** Set the printer used for the output of test results. |
| 262 | * |
| 263 | * @param[in] printer Pointer to a printer. |
| 264 | */ |
| 265 | void set_printer(Printer *printer); |
| 266 | |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 267 | /** List of @ref TestInfo's. |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 268 | * |
| 269 | * @return Vector with all test ids. |
| 270 | */ |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 271 | std::vector<TestInfo> test_infos() const; |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 272 | |
| 273 | private: |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 274 | Framework(); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 275 | ~Framework() = default; |
| 276 | |
| 277 | Framework(const Framework &) = delete; |
| 278 | Framework &operator=(const Framework &) = delete; |
| 279 | |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 280 | void run_test(const TestInfo &info, TestCaseFactory &test_factory); |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 281 | std::map<TestResult::Status, int> count_test_results() const; |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 282 | |
| 283 | /** Returns the current test suite name. |
| 284 | * |
| 285 | * @warning Cannot be used at execution time to get the test suite of the |
| 286 | * currently executed test case. It can only be used for registering test |
| 287 | * cases. |
| 288 | * |
| 289 | * @return Name of the current test suite. |
| 290 | */ |
| 291 | std::string current_suite_name() const; |
| 292 | |
| 293 | std::vector<std::string> _test_suite_name{}; |
| 294 | std::vector<std::unique_ptr<TestCaseFactory>> _test_factories{}; |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 295 | std::map<TestInfo, TestResult> _test_results{}; |
| 296 | int _num_iterations{ 1 }; |
| 297 | bool _throw_errors{ false }; |
Moritz Pflanzer | fa81165 | 2017-07-26 17:00:37 +0100 | [diff] [blame] | 298 | bool _stop_on_error{ false }; |
Moritz Pflanzer | 542002c | 2017-07-26 16:03:58 +0100 | [diff] [blame] | 299 | Printer *_printer{ nullptr }; |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 300 | |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 301 | using create_function = std::unique_ptr<Instrument>(); |
| 302 | std::map<InstrumentType, create_function *> _available_instruments{}; |
| 303 | |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 304 | InstrumentType _instruments{ InstrumentType::NONE }; |
Moritz Pflanzer | ec2de0f | 2017-07-27 14:43:46 +0100 | [diff] [blame] | 305 | TestFilter _test_filter{}; |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 306 | LogLevel _log_level{ LogLevel::ALL }; |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 307 | TestResult *_current_test_result{ nullptr }; |
| 308 | std::vector<std::string> _test_info{}; |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 309 | }; |
| 310 | |
| 311 | template <typename T> |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 312 | inline void Framework::add_test_case(std::string test_name, DatasetMode mode, TestCaseFactory::Status status) |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 313 | { |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 314 | _test_factories.emplace_back(support::cpp14::make_unique<SimpleTestCaseFactory<T>>(current_suite_name(), std::move(test_name), mode, status)); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | template <typename T, typename D> |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 318 | inline void Framework::add_data_test_case(std::string test_name, DatasetMode mode, TestCaseFactory::Status status, std::string description, D &&data) |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 319 | { |
| 320 | // WORKAROUND for GCC 4.9 |
| 321 | // The function should get *it which is tuple but that seems to trigger a |
| 322 | // bug in the compiler. |
Moritz Pflanzer | bf234e0 | 2017-07-24 15:04:14 +0100 | [diff] [blame] | 323 | 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 Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 324 | std::move(description), *data)); |
| 325 | _test_factories.emplace_back(std::move(tmp)); |
| 326 | } |
| 327 | } // namespace framework |
| 328 | } // namespace test |
| 329 | } // namespace arm_compute |
| 330 | #endif /* ARM_COMPUTE_TEST_FRAMEWORK */ |