1 // Copyright 2022 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 #pragma once 15 16 #include "pw_perf_test/event_handler.h" 17 #include "pw_perf_test/internal/test_info.h" 18 #include "pw_perf_test/state.h" 19 #include "pw_preprocessor/arguments.h" 20 21 /// Defines a performance test. 22 /// 23 /// The `Framework` will create a `State` and pass it to the provided function. 24 /// This function should perform whatever behavior is to be measured in a loop 25 /// as long as `State::KeepRunning()` returns true. 26 /// 27 /// Example: 28 /// @code{.cpp} 29 /// void TestFunction(::pw::perf_test::State& state, args...) { 30 /// // Create any needed variables. 31 /// while (state.KeepRunning()){ 32 /// // Run code to be measured here. 33 /// } 34 /// } 35 /// PW_PERF_TEST(PerformanceTestName, TestFunction, args...); 36 /// @endcode 37 #define PW_PERF_TEST(name, function, ...) \ 38 const ::pw::perf_test::internal::TestInfo PwPerfTest_##name( \ 39 #name, [](::pw::perf_test::State& pw_perf_test_state) { \ 40 static_cast<void>( \ 41 function(pw_perf_test_state PW_COMMA_ARGS(__VA_ARGS__))); \ 42 }) 43 44 /// Defines a simple performance test. 45 /// 46 /// This macro is similar to `PW_PERF_TEST`, except that the provided function 47 /// does not take a `State` parameter. As a result, the function should NOT call 48 /// `State::KeepRunning()`. Instead, the macro calls the function within its own 49 /// internal state loop. 50 /// 51 /// Example: 52 /// @code{.cpp} 53 /// void TestFunction(args...) { 54 /// // Run code to be measured here. 55 /// } 56 /// PW_PERF_SIMPLE_TEST(PerformanceTestName, TestFunction, args...); 57 /// @endcode 58 #define PW_PERF_TEST_SIMPLE(name, function, ...) \ 59 PW_PERF_TEST( \ 60 name, \ 61 [](::pw::perf_test::State& pw_perf_test_simple_state, \ 62 const auto&... args) { \ 63 while (pw_perf_test_simple_state.KeepRunning()) { \ 64 function(args...); \ 65 } \ 66 }, \ 67 __VA_ARGS__) 68 69 namespace pw::perf_test { 70 71 /// Runs all registered tests, 72 /// 73 /// This function should be called by `main`. The tests will use the provided 74 /// `handler` to report results. 75 void RunAllTests(EventHandler& handler); 76 77 } // namespace pw::perf_test 78