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 | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 27 | #include "Profiler.h" |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 28 | #include "TestCase.h" |
| 29 | #include "TestCaseFactory.h" |
| 30 | #include "TestResult.h" |
| 31 | #include "Utils.h" |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 32 | #include "instruments/Instruments.h" |
Moritz Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 33 | #include "printers/Printer.h" |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 34 | |
| 35 | #include <algorithm> |
| 36 | #include <chrono> |
| 37 | #include <map> |
| 38 | #include <memory> |
| 39 | #include <numeric> |
| 40 | #include <ostream> |
| 41 | #include <regex> |
| 42 | #include <set> |
| 43 | #include <sstream> |
| 44 | #include <string> |
| 45 | #include <tuple> |
| 46 | #include <vector> |
| 47 | |
| 48 | namespace arm_compute |
| 49 | { |
| 50 | namespace test |
| 51 | { |
| 52 | namespace framework |
| 53 | { |
| 54 | /** Main framework class. |
| 55 | * |
| 56 | * Keeps track of the global state, owns all test cases and collects results. |
| 57 | */ |
| 58 | class Framework final |
| 59 | { |
| 60 | public: |
| 61 | /** Type of a test identifier. |
| 62 | * |
| 63 | * A test can be identified either via its id or via its name. |
| 64 | * |
| 65 | * @note The mapping between test id and test name is not guaranteed to be |
| 66 | * stable. It is subject to change as new test are added. |
| 67 | */ |
| 68 | using TestId = std::pair<int, std::string>; |
| 69 | |
| 70 | /** Access to the singleton. |
| 71 | * |
| 72 | * @return Unique instance of the framework class. |
| 73 | */ |
| 74 | static Framework &get(); |
| 75 | |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 76 | /** Supported instrument types for benchmarking. |
| 77 | * |
| 78 | * @return Set of all available instrument types. |
| 79 | */ |
| 80 | std::set<InstrumentType> available_instruments() const; |
| 81 | |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 82 | /** Init the framework. |
| 83 | * |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 84 | * @param[in] instruments Instrument types that will be used for benchmarking. |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 85 | * @param[in] num_iterations Number of iterations per test. |
| 86 | * @param[in] name_filter Regular expression to filter tests by name. Only matching tests will be executed. |
| 87 | * @param[in] id_filter Regular expression to filter tests by id. Only matching tests will be executed. |
| 88 | */ |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 89 | void init(const std::vector<InstrumentType> &instruments, int num_iterations, const std::string &name_filter, const std::string &id_filter); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 90 | |
| 91 | /** Add a new test suite. |
| 92 | * |
| 93 | * @warning Cannot be used at execution time. It can only be used for |
| 94 | * registering test cases. |
| 95 | * |
| 96 | * @param[in] name Name of the added test suite. |
| 97 | * |
| 98 | * @return Name of the current test suite. |
| 99 | */ |
| 100 | void push_suite(std::string name); |
| 101 | |
| 102 | /** Remove innermost test suite. |
| 103 | * |
| 104 | * @warning Cannot be used at execution time. It can only be used for |
| 105 | * registering test cases. |
| 106 | */ |
| 107 | void pop_suite(); |
| 108 | |
| 109 | /** Add a test case to the framework. |
| 110 | * |
| 111 | * @param[in] test_name Name of the new test case. |
| 112 | */ |
| 113 | template <typename T> |
| 114 | void add_test_case(std::string test_name); |
| 115 | |
| 116 | /** Add a data test case to the framework. |
| 117 | * |
| 118 | * @param[in] test_name Name of the new test case. |
| 119 | * @param[in] description Description of @p data. |
| 120 | * @param[in] data Data that will be used as input to the test. |
| 121 | */ |
| 122 | template <typename T, typename D> |
| 123 | void add_data_test_case(std::string test_name, std::string description, D &&data); |
| 124 | |
| 125 | /** Tell the framework that execution of a test starts. |
| 126 | * |
| 127 | * @param[in] test_name Name of the started test case. |
| 128 | */ |
| 129 | void log_test_start(const std::string &test_name); |
| 130 | |
| 131 | /** Tell the framework that a test case is skipped. |
| 132 | * |
| 133 | * @param[in] test_name Name of the skipped test case. |
| 134 | */ |
| 135 | void log_test_skipped(const std::string &test_name); |
| 136 | |
| 137 | /** Tell the framework that a test case finished. |
| 138 | * |
| 139 | * @param[in] test_name Name of the finished test case. |
| 140 | */ |
| 141 | void log_test_end(const std::string &test_name); |
| 142 | |
| 143 | /** Tell the framework that the currently running test case failed a non-fatal expectation. |
| 144 | * |
| 145 | * @param[in] msg Description of the failure. |
| 146 | */ |
| 147 | void log_failed_expectation(const std::string &msg); |
| 148 | |
| 149 | /** Number of iterations per test case. |
| 150 | * |
| 151 | * @return Number of iterations per test case. |
| 152 | */ |
| 153 | int num_iterations() const; |
| 154 | |
| 155 | /** Set number of iterations per test case. |
| 156 | * |
| 157 | * @param[in] num_iterations Number of iterations per test case. |
| 158 | */ |
| 159 | void set_num_iterations(int num_iterations); |
| 160 | |
| 161 | /** Should errors be caught or thrown by the framework. |
| 162 | * |
| 163 | * @return True if errors are thrown. |
| 164 | */ |
| 165 | bool throw_errors() const; |
| 166 | |
| 167 | /** Set whether errors are caught or thrown by the framework. |
| 168 | * |
| 169 | * @param[in] throw_errors True if errors should be thrown. |
| 170 | */ |
| 171 | void set_throw_errors(bool throw_errors); |
| 172 | |
| 173 | /** Check if a test case would be executed. |
| 174 | * |
| 175 | * @param[in] id Id of the test case. |
| 176 | * |
| 177 | * @return True if the test case would be executed. |
| 178 | */ |
| 179 | bool is_enabled(const TestId &id) const; |
| 180 | |
| 181 | /** Run all enabled test cases. |
| 182 | * |
| 183 | * @return True if all test cases executed successful. |
| 184 | */ |
| 185 | bool run(); |
| 186 | |
| 187 | /** Set the result for an executed test case. |
| 188 | * |
| 189 | * @param[in] test_case_name Name of the executed test case. |
| 190 | * @param[in] result Execution result. |
| 191 | */ |
| 192 | void set_test_result(std::string test_case_name, TestResult result); |
| 193 | |
Moritz Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 194 | /** Use the specified printer to output test results from the last run. |
| 195 | * |
| 196 | * This method can be used if the test results need to be obtained using a |
| 197 | * different printer than the one managed by the framework. |
| 198 | * |
| 199 | * @param[in] printer Printer used to output results. |
| 200 | */ |
| 201 | void print_test_results(Printer &printer) const; |
| 202 | |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 203 | /** Factory method to obtain a configured profiler. |
| 204 | * |
| 205 | * The profiler enables all instruments that have been passed to the @ref |
| 206 | * init method. |
| 207 | * |
| 208 | * @return Configured profiler to collect benchmark results. |
| 209 | */ |
| 210 | Profiler get_profiler() const; |
| 211 | |
Moritz Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 212 | /** Set the printer used for the output of test results. |
| 213 | * |
| 214 | * @param[in] printer Pointer to a printer. |
| 215 | */ |
| 216 | void set_printer(Printer *printer); |
| 217 | |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 218 | /** List of @ref TestId's. |
| 219 | * |
| 220 | * @return Vector with all test ids. |
| 221 | */ |
| 222 | std::vector<Framework::TestId> test_ids() const; |
| 223 | |
| 224 | private: |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 225 | Framework(); |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 226 | ~Framework() = default; |
| 227 | |
| 228 | Framework(const Framework &) = delete; |
| 229 | Framework &operator=(const Framework &) = delete; |
| 230 | |
| 231 | void run_test(TestCaseFactory &test_factory); |
| 232 | std::tuple<int, int, int> count_test_results() const; |
| 233 | |
| 234 | /** Returns the current test suite name. |
| 235 | * |
| 236 | * @warning Cannot be used at execution time to get the test suite of the |
| 237 | * currently executed test case. It can only be used for registering test |
| 238 | * cases. |
| 239 | * |
| 240 | * @return Name of the current test suite. |
| 241 | */ |
| 242 | std::string current_suite_name() const; |
| 243 | |
| 244 | std::vector<std::string> _test_suite_name{}; |
| 245 | std::vector<std::unique_ptr<TestCaseFactory>> _test_factories{}; |
| 246 | std::map<std::string, TestResult> _test_results{}; |
| 247 | std::chrono::seconds _runtime{ 0 }; |
| 248 | int _num_iterations{ 1 }; |
| 249 | bool _throw_errors{ false }; |
Moritz Pflanzer | 80fffae | 2017-07-05 11:02:37 +0100 | [diff] [blame] | 250 | Printer *_printer{ nullptr }; |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 251 | |
Moritz Pflanzer | a4f711b | 2017-07-05 11:02:23 +0100 | [diff] [blame] | 252 | using create_function = std::unique_ptr<Instrument>(); |
| 253 | std::map<InstrumentType, create_function *> _available_instruments{}; |
| 254 | |
| 255 | InstrumentType _instruments{ InstrumentType::NONE }; |
| 256 | std::regex _test_name_filter{ ".*" }; |
| 257 | std::regex _test_id_filter{ ".*" }; |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 258 | }; |
| 259 | |
| 260 | template <typename T> |
| 261 | inline void Framework::add_test_case(std::string test_name) |
| 262 | { |
| 263 | _test_factories.emplace_back(support::cpp14::make_unique<SimpleTestCaseFactory<T>>(current_suite_name(), std::move(test_name))); |
| 264 | } |
| 265 | |
| 266 | template <typename T, typename D> |
| 267 | inline void Framework::add_data_test_case(std::string test_name, std::string description, D &&data) |
| 268 | { |
| 269 | // WORKAROUND for GCC 4.9 |
| 270 | // The function should get *it which is tuple but that seems to trigger a |
| 271 | // bug in the compiler. |
| 272 | 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), |
| 273 | std::move(description), *data)); |
| 274 | _test_factories.emplace_back(std::move(tmp)); |
| 275 | } |
| 276 | } // namespace framework |
| 277 | } // namespace test |
| 278 | } // namespace arm_compute |
| 279 | #endif /* ARM_COMPUTE_TEST_FRAMEWORK */ |