xref: /aosp_15_r20/external/pigweed/pw_perf_test/examples/example_perf_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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 
15 #include <cstddef>
16 
17 #include "pw_perf_test/perf_test.h"
18 
19 namespace pw::perf_test {
20 namespace {
21 
22 // DOCSTAG: [pw_perf_test_examples-simulate_work]
SimulateWork(size_t a,size_t b)23 void SimulateWork(size_t a, size_t b) {
24   for (volatile size_t i = 0; i < a * b * 100000; i = i + 1) {
25   }
26 }
27 // DOCSTAG: [pw_perf_test_examples-simulate_work]
28 
29 // DOCSTAG: [pw_perf_test_examples-simple_example]
30 PW_PERF_TEST_SIMPLE(SimpleFunction, SimulateWork, 2, 4);
31 // DOCSTAG: [pw_perf_test_examples-simple_example]
32 
33 // DOCSTAG: [pw_perf_test_examples-full_example]
TestFunction(pw::perf_test::State & state,size_t a,size_t b)34 void TestFunction(pw::perf_test::State& state, size_t a, size_t b) {
35   while (state.KeepRunning()) {
36     SimulateWork(a, b);
37   }
38 }
39 PW_PERF_TEST(FunctionWithArgs, TestFunction, 2, 4);
40 // DOCSTAG: [pw_perf_test_examples-full_example]
41 
42 // DOCSTAG: [pw_perf_test_examples-lambda_example]
43 PW_PERF_TEST_SIMPLE(
__anondd6d8bb50202(size_t a, size_t b) 44     SimpleLambda, [](size_t a, size_t b) { SimulateWork(a, b); }, 2, 4);
45 
46 PW_PERF_TEST(
47     LambdaFunction,
__anondd6d8bb50302(pw::perf_test::State& state, size_t a, size_t b) 48     [](pw::perf_test::State& state, size_t a, size_t b) {
49       while (state.KeepRunning()) {
50         SimulateWork(a, b);
51       }
52     },
53     2,
54     4);
55 // DOCSTAG: [pw_perf_test_examples-lambda_example]
56 
57 }  // namespace
58 }  // namespace pw::perf_test
59