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