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