Pull from FB rev 63ce89e2f2301e6bba44a111cc7d4218022156f6
[folly.git] / folly / test / function_benchmark / main.cpp
1 /*
2  * Copyright 2012 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "folly/test/function_benchmark/benchmark_impl.h"
18 #include "folly/test/function_benchmark/test_functions.h"
19
20 #include "folly/Benchmark.h"
21 #include "folly/ScopeGuard.h"
22 #include <gflags/gflags.h>
23 #include <glog/logging.h>
24
25 using folly::ScopeGuard;
26 using folly::makeGuard;
27
28 // Declare the bm_max_iters flag from folly/Benchmark.cpp
29 DECLARE_int32(bm_max_iters);
30
31 // Directly invoking a function
32 BENCHMARK(fn_invoke, iters) {
33   for (int n = 0; n < iters; ++n) {
34     doNothing();
35   }
36 }
37
38 // Invoking a function through a function pointer
39 BENCHMARK(fn_ptr_invoke, iters) {
40   BM_fn_ptr_invoke_impl(iters, doNothing);
41 }
42
43 // Invoking a function through a std::function object
44 BENCHMARK(std_function_invoke, iters) {
45   BM_std_function_invoke_impl(iters, doNothing);
46 }
47
48 // Invoking a member function through a member function pointer
49 BENCHMARK(mem_fn_invoke, iters) {
50   TestClass tc;
51   BM_mem_fn_invoke_impl(iters, &tc, &TestClass::doNothing);
52 }
53
54 // Invoke a function pointer through an inlined wrapper function
55 BENCHMARK(fn_ptr_invoke_through_inline, iters) {
56   BM_fn_ptr_invoke_inlined_impl(iters, doNothing);
57 }
58
59 // Invoke a lambda that calls doNothing() through an inlined wrapper function
60 BENCHMARK(lambda_invoke_fn, iters) {
61   BM_invoke_fn_template_impl(iters, [] { doNothing(); });
62 }
63
64 // Invoke a lambda that does nothing
65 BENCHMARK(lambda_noop, iters) {
66   BM_invoke_fn_template_impl(iters, [] {});
67 }
68
69 // Invoke a lambda that modifies a local variable
70 BENCHMARK(lambda_local_var, iters) {
71   uint32_t count1 = 0;
72   uint32_t count2 = 0;
73   BM_invoke_fn_template_impl(iters, [&] {
74     // Do something slightly more complicated than just incrementing a
75     // variable.  Otherwise gcc is smart enough to optimize the loop away.
76     if (count1 & 0x1) {
77       ++count2;
78     }
79     ++count1;
80   });
81
82   // Use the values we computed, so gcc won't optimize the loop away
83   CHECK_EQ(iters, count1);
84   CHECK_EQ(iters / 2, count2);
85 }
86
87 // Invoke a function pointer through the same wrapper used for lambdas
88 BENCHMARK(fn_ptr_invoke_through_template, iters) {
89   BM_invoke_fn_template_impl(iters, doNothing);
90 }
91
92 // Invoking a virtual method
93 BENCHMARK(virtual_fn_invoke, iters) {
94   VirtualClass vc;
95   BM_virtual_fn_invoke_impl(iters, &vc);
96 }
97
98 // Creating a function pointer and invoking it
99 BENCHMARK(fn_ptr_create_invoke, iters) {
100   for (int n = 0; n < iters; ++n) {
101     void (*fn)() = doNothing;
102     fn();
103   }
104 }
105
106 // Creating a std::function object from a function pointer, and invoking it
107 BENCHMARK(std_function_create_invoke, iters) {
108   for (int n = 0; n < iters; ++n) {
109     std::function<void()> fn = doNothing;
110     fn();
111   }
112 }
113
114 // Creating a pointer-to-member and invoking it
115 BENCHMARK(mem_fn_create_invoke, iters) {
116   TestClass tc;
117   for (int n = 0; n < iters; ++n) {
118     void (TestClass::*memfn)() = &TestClass::doNothing;
119     (tc.*memfn)();
120   }
121 }
122
123 // Using std::bind to create a std::function from a member function,
124 // and invoking it
125 BENCHMARK(std_bind_create_invoke, iters) {
126   TestClass tc;
127   for (int n = 0; n < iters; ++n) {
128     std::function<void()> fn = std::bind(&TestClass::doNothing, &tc);
129     fn();
130   }
131 }
132
133 // Using ScopeGuard to invoke a std::function
134 BENCHMARK(scope_guard_std_function, iters) {
135   std::function<void()> fn(doNothing);
136   for (int n = 0; n < iters; ++n) {
137     ScopeGuard g = makeGuard(fn);
138   }
139 }
140
141 // Using ScopeGuard to invoke a std::function,
142 // but create the ScopeGuard with an rvalue to a std::function
143 BENCHMARK(scope_guard_std_function_rvalue, iters) {
144   for (int n = 0; n < iters; ++n) {
145     ScopeGuard g = makeGuard(std::function<void()>(doNothing));
146   }
147 }
148
149 // Using ScopeGuard to invoke a function pointer
150 BENCHMARK(scope_guard_fn_ptr, iters) {
151   for (int n = 0; n < iters; ++n) {
152     ScopeGuard g = makeGuard(doNothing);
153   }
154 }
155
156 // Using ScopeGuard to invoke a lambda that does nothing
157 BENCHMARK(scope_guard_lambda_noop, iters) {
158   for (int n = 0; n < iters; ++n) {
159     ScopeGuard g = makeGuard([] {});
160   }
161 }
162
163 // Using ScopeGuard to invoke a lambda that invokes a function
164 BENCHMARK(scope_guard_lambda_function, iters) {
165   for (int n = 0; n < iters; ++n) {
166     ScopeGuard g = makeGuard([] { doNothing(); });
167   }
168 }
169
170 // Using ScopeGuard to invoke a lambda that modifies a local variable
171 BENCHMARK(scope_guard_lambda_local_var, iters) {
172   uint32_t count = 0;
173   for (int n = 0; n < iters; ++n) {
174     ScopeGuard g = makeGuard([&] {
175       // Increment count if n is odd.  Without this conditional check
176       // (i.e., if we just increment count each time through the loop),
177       // gcc is smart enough to optimize the entire loop away, and just set
178       // count = iters.
179       if (n & 0x1) {
180         ++count;
181       }
182     });
183   }
184
185   // Check that the value of count is what we expect.
186   // This check is necessary: if we don't use count, gcc detects that count is
187   // unused and optimizes the entire loop away.
188   CHECK_EQ(iters / 2, count);
189 }
190
191 // main()
192
193 int main(int argc, char** argv) {
194   google::ParseCommandLineFlags(&argc, &argv, true);
195   folly::runBenchmarks();
196 }