1*58b9f456SAndroid Build Coastguard Worker #include "benchmark/benchmark.h"
2*58b9f456SAndroid Build Coastguard Worker
3*58b9f456SAndroid Build Coastguard Worker #include <assert.h>
4*58b9f456SAndroid Build Coastguard Worker #include <math.h>
5*58b9f456SAndroid Build Coastguard Worker #include <stdint.h>
6*58b9f456SAndroid Build Coastguard Worker
7*58b9f456SAndroid Build Coastguard Worker #include <chrono>
8*58b9f456SAndroid Build Coastguard Worker #include <cstdlib>
9*58b9f456SAndroid Build Coastguard Worker #include <iostream>
10*58b9f456SAndroid Build Coastguard Worker #include <limits>
11*58b9f456SAndroid Build Coastguard Worker #include <list>
12*58b9f456SAndroid Build Coastguard Worker #include <map>
13*58b9f456SAndroid Build Coastguard Worker #include <mutex>
14*58b9f456SAndroid Build Coastguard Worker #include <set>
15*58b9f456SAndroid Build Coastguard Worker #include <sstream>
16*58b9f456SAndroid Build Coastguard Worker #include <string>
17*58b9f456SAndroid Build Coastguard Worker #include <thread>
18*58b9f456SAndroid Build Coastguard Worker #include <utility>
19*58b9f456SAndroid Build Coastguard Worker #include <vector>
20*58b9f456SAndroid Build Coastguard Worker
21*58b9f456SAndroid Build Coastguard Worker #if defined(__GNUC__)
22*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_NOINLINE __attribute__((noinline))
23*58b9f456SAndroid Build Coastguard Worker #else
24*58b9f456SAndroid Build Coastguard Worker #define BENCHMARK_NOINLINE
25*58b9f456SAndroid Build Coastguard Worker #endif
26*58b9f456SAndroid Build Coastguard Worker
27*58b9f456SAndroid Build Coastguard Worker namespace {
28*58b9f456SAndroid Build Coastguard Worker
Factorial(uint32_t n)29*58b9f456SAndroid Build Coastguard Worker int BENCHMARK_NOINLINE Factorial(uint32_t n) {
30*58b9f456SAndroid Build Coastguard Worker return (n == 1) ? 1 : n * Factorial(n - 1);
31*58b9f456SAndroid Build Coastguard Worker }
32*58b9f456SAndroid Build Coastguard Worker
CalculatePi(int depth)33*58b9f456SAndroid Build Coastguard Worker double CalculatePi(int depth) {
34*58b9f456SAndroid Build Coastguard Worker double pi = 0.0;
35*58b9f456SAndroid Build Coastguard Worker for (int i = 0; i < depth; ++i) {
36*58b9f456SAndroid Build Coastguard Worker double numerator = static_cast<double>(((i % 2) * 2) - 1);
37*58b9f456SAndroid Build Coastguard Worker double denominator = static_cast<double>((2 * i) - 1);
38*58b9f456SAndroid Build Coastguard Worker pi += numerator / denominator;
39*58b9f456SAndroid Build Coastguard Worker }
40*58b9f456SAndroid Build Coastguard Worker return (pi - 1.0) * 4;
41*58b9f456SAndroid Build Coastguard Worker }
42*58b9f456SAndroid Build Coastguard Worker
ConstructRandomSet(int64_t size)43*58b9f456SAndroid Build Coastguard Worker std::set<int64_t> ConstructRandomSet(int64_t size) {
44*58b9f456SAndroid Build Coastguard Worker std::set<int64_t> s;
45*58b9f456SAndroid Build Coastguard Worker for (int i = 0; i < size; ++i) s.insert(s.end(), i);
46*58b9f456SAndroid Build Coastguard Worker return s;
47*58b9f456SAndroid Build Coastguard Worker }
48*58b9f456SAndroid Build Coastguard Worker
49*58b9f456SAndroid Build Coastguard Worker std::mutex test_vector_mu;
50*58b9f456SAndroid Build Coastguard Worker std::vector<int>* test_vector = nullptr;
51*58b9f456SAndroid Build Coastguard Worker
52*58b9f456SAndroid Build Coastguard Worker } // end namespace
53*58b9f456SAndroid Build Coastguard Worker
BM_Factorial(benchmark::State & state)54*58b9f456SAndroid Build Coastguard Worker static void BM_Factorial(benchmark::State& state) {
55*58b9f456SAndroid Build Coastguard Worker int fac_42 = 0;
56*58b9f456SAndroid Build Coastguard Worker for (auto _ : state) fac_42 = Factorial(8);
57*58b9f456SAndroid Build Coastguard Worker // Prevent compiler optimizations
58*58b9f456SAndroid Build Coastguard Worker std::stringstream ss;
59*58b9f456SAndroid Build Coastguard Worker ss << fac_42;
60*58b9f456SAndroid Build Coastguard Worker state.SetLabel(ss.str());
61*58b9f456SAndroid Build Coastguard Worker }
62*58b9f456SAndroid Build Coastguard Worker BENCHMARK(BM_Factorial);
63*58b9f456SAndroid Build Coastguard Worker BENCHMARK(BM_Factorial)->UseRealTime();
64*58b9f456SAndroid Build Coastguard Worker
BM_CalculatePiRange(benchmark::State & state)65*58b9f456SAndroid Build Coastguard Worker static void BM_CalculatePiRange(benchmark::State& state) {
66*58b9f456SAndroid Build Coastguard Worker double pi = 0.0;
67*58b9f456SAndroid Build Coastguard Worker for (auto _ : state) pi = CalculatePi(static_cast<int>(state.range(0)));
68*58b9f456SAndroid Build Coastguard Worker std::stringstream ss;
69*58b9f456SAndroid Build Coastguard Worker ss << pi;
70*58b9f456SAndroid Build Coastguard Worker state.SetLabel(ss.str());
71*58b9f456SAndroid Build Coastguard Worker }
72*58b9f456SAndroid Build Coastguard Worker BENCHMARK_RANGE(BM_CalculatePiRange, 1, 1024 * 1024);
73*58b9f456SAndroid Build Coastguard Worker
BM_CalculatePi(benchmark::State & state)74*58b9f456SAndroid Build Coastguard Worker static void BM_CalculatePi(benchmark::State& state) {
75*58b9f456SAndroid Build Coastguard Worker static const int depth = 1024;
76*58b9f456SAndroid Build Coastguard Worker for (auto _ : state) {
77*58b9f456SAndroid Build Coastguard Worker benchmark::DoNotOptimize(CalculatePi(static_cast<int>(depth)));
78*58b9f456SAndroid Build Coastguard Worker }
79*58b9f456SAndroid Build Coastguard Worker }
80*58b9f456SAndroid Build Coastguard Worker BENCHMARK(BM_CalculatePi)->Threads(8);
81*58b9f456SAndroid Build Coastguard Worker BENCHMARK(BM_CalculatePi)->ThreadRange(1, 32);
82*58b9f456SAndroid Build Coastguard Worker BENCHMARK(BM_CalculatePi)->ThreadPerCpu();
83*58b9f456SAndroid Build Coastguard Worker
BM_SetInsert(benchmark::State & state)84*58b9f456SAndroid Build Coastguard Worker static void BM_SetInsert(benchmark::State& state) {
85*58b9f456SAndroid Build Coastguard Worker std::set<int64_t> data;
86*58b9f456SAndroid Build Coastguard Worker for (auto _ : state) {
87*58b9f456SAndroid Build Coastguard Worker state.PauseTiming();
88*58b9f456SAndroid Build Coastguard Worker data = ConstructRandomSet(state.range(0));
89*58b9f456SAndroid Build Coastguard Worker state.ResumeTiming();
90*58b9f456SAndroid Build Coastguard Worker for (int j = 0; j < state.range(1); ++j) data.insert(rand());
91*58b9f456SAndroid Build Coastguard Worker }
92*58b9f456SAndroid Build Coastguard Worker state.SetItemsProcessed(state.iterations() * state.range(1));
93*58b9f456SAndroid Build Coastguard Worker state.SetBytesProcessed(state.iterations() * state.range(1) * sizeof(int));
94*58b9f456SAndroid Build Coastguard Worker }
95*58b9f456SAndroid Build Coastguard Worker
96*58b9f456SAndroid Build Coastguard Worker // Test many inserts at once to reduce the total iterations needed. Otherwise, the slower,
97*58b9f456SAndroid Build Coastguard Worker // non-timed part of each iteration will make the benchmark take forever.
98*58b9f456SAndroid Build Coastguard Worker BENCHMARK(BM_SetInsert)->Ranges({{1 << 10, 8 << 10}, {128, 512}});
99*58b9f456SAndroid Build Coastguard Worker
100*58b9f456SAndroid Build Coastguard Worker template <typename Container,
101*58b9f456SAndroid Build Coastguard Worker typename ValueType = typename Container::value_type>
BM_Sequential(benchmark::State & state)102*58b9f456SAndroid Build Coastguard Worker static void BM_Sequential(benchmark::State& state) {
103*58b9f456SAndroid Build Coastguard Worker ValueType v = 42;
104*58b9f456SAndroid Build Coastguard Worker for (auto _ : state) {
105*58b9f456SAndroid Build Coastguard Worker Container c;
106*58b9f456SAndroid Build Coastguard Worker for (int64_t i = state.range(0); --i;) c.push_back(v);
107*58b9f456SAndroid Build Coastguard Worker }
108*58b9f456SAndroid Build Coastguard Worker const int64_t items_processed = state.iterations() * state.range(0);
109*58b9f456SAndroid Build Coastguard Worker state.SetItemsProcessed(items_processed);
110*58b9f456SAndroid Build Coastguard Worker state.SetBytesProcessed(items_processed * sizeof(v));
111*58b9f456SAndroid Build Coastguard Worker }
112*58b9f456SAndroid Build Coastguard Worker BENCHMARK_TEMPLATE2(BM_Sequential, std::vector<int>, int)
113*58b9f456SAndroid Build Coastguard Worker ->Range(1 << 0, 1 << 10);
114*58b9f456SAndroid Build Coastguard Worker BENCHMARK_TEMPLATE(BM_Sequential, std::list<int>)->Range(1 << 0, 1 << 10);
115*58b9f456SAndroid Build Coastguard Worker // Test the variadic version of BENCHMARK_TEMPLATE in C++11 and beyond.
116*58b9f456SAndroid Build Coastguard Worker #ifdef BENCHMARK_HAS_CXX11
117*58b9f456SAndroid Build Coastguard Worker BENCHMARK_TEMPLATE(BM_Sequential, std::vector<int>, int)->Arg(512);
118*58b9f456SAndroid Build Coastguard Worker #endif
119*58b9f456SAndroid Build Coastguard Worker
BM_StringCompare(benchmark::State & state)120*58b9f456SAndroid Build Coastguard Worker static void BM_StringCompare(benchmark::State& state) {
121*58b9f456SAndroid Build Coastguard Worker size_t len = static_cast<size_t>(state.range(0));
122*58b9f456SAndroid Build Coastguard Worker std::string s1(len, '-');
123*58b9f456SAndroid Build Coastguard Worker std::string s2(len, '-');
124*58b9f456SAndroid Build Coastguard Worker for (auto _ : state) benchmark::DoNotOptimize(s1.compare(s2));
125*58b9f456SAndroid Build Coastguard Worker }
126*58b9f456SAndroid Build Coastguard Worker BENCHMARK(BM_StringCompare)->Range(1, 1 << 20);
127*58b9f456SAndroid Build Coastguard Worker
BM_SetupTeardown(benchmark::State & state)128*58b9f456SAndroid Build Coastguard Worker static void BM_SetupTeardown(benchmark::State& state) {
129*58b9f456SAndroid Build Coastguard Worker if (state.thread_index == 0) {
130*58b9f456SAndroid Build Coastguard Worker // No need to lock test_vector_mu here as this is running single-threaded.
131*58b9f456SAndroid Build Coastguard Worker test_vector = new std::vector<int>();
132*58b9f456SAndroid Build Coastguard Worker }
133*58b9f456SAndroid Build Coastguard Worker int i = 0;
134*58b9f456SAndroid Build Coastguard Worker for (auto _ : state) {
135*58b9f456SAndroid Build Coastguard Worker std::lock_guard<std::mutex> l(test_vector_mu);
136*58b9f456SAndroid Build Coastguard Worker if (i % 2 == 0)
137*58b9f456SAndroid Build Coastguard Worker test_vector->push_back(i);
138*58b9f456SAndroid Build Coastguard Worker else
139*58b9f456SAndroid Build Coastguard Worker test_vector->pop_back();
140*58b9f456SAndroid Build Coastguard Worker ++i;
141*58b9f456SAndroid Build Coastguard Worker }
142*58b9f456SAndroid Build Coastguard Worker if (state.thread_index == 0) {
143*58b9f456SAndroid Build Coastguard Worker delete test_vector;
144*58b9f456SAndroid Build Coastguard Worker }
145*58b9f456SAndroid Build Coastguard Worker }
146*58b9f456SAndroid Build Coastguard Worker BENCHMARK(BM_SetupTeardown)->ThreadPerCpu();
147*58b9f456SAndroid Build Coastguard Worker
BM_LongTest(benchmark::State & state)148*58b9f456SAndroid Build Coastguard Worker static void BM_LongTest(benchmark::State& state) {
149*58b9f456SAndroid Build Coastguard Worker double tracker = 0.0;
150*58b9f456SAndroid Build Coastguard Worker for (auto _ : state) {
151*58b9f456SAndroid Build Coastguard Worker for (int i = 0; i < state.range(0); ++i)
152*58b9f456SAndroid Build Coastguard Worker benchmark::DoNotOptimize(tracker += i);
153*58b9f456SAndroid Build Coastguard Worker }
154*58b9f456SAndroid Build Coastguard Worker }
155*58b9f456SAndroid Build Coastguard Worker BENCHMARK(BM_LongTest)->Range(1 << 16, 1 << 28);
156*58b9f456SAndroid Build Coastguard Worker
BM_ParallelMemset(benchmark::State & state)157*58b9f456SAndroid Build Coastguard Worker static void BM_ParallelMemset(benchmark::State& state) {
158*58b9f456SAndroid Build Coastguard Worker int64_t size = state.range(0) / static_cast<int64_t>(sizeof(int));
159*58b9f456SAndroid Build Coastguard Worker int thread_size = static_cast<int>(size) / state.threads;
160*58b9f456SAndroid Build Coastguard Worker int from = thread_size * state.thread_index;
161*58b9f456SAndroid Build Coastguard Worker int to = from + thread_size;
162*58b9f456SAndroid Build Coastguard Worker
163*58b9f456SAndroid Build Coastguard Worker if (state.thread_index == 0) {
164*58b9f456SAndroid Build Coastguard Worker test_vector = new std::vector<int>(static_cast<size_t>(size));
165*58b9f456SAndroid Build Coastguard Worker }
166*58b9f456SAndroid Build Coastguard Worker
167*58b9f456SAndroid Build Coastguard Worker for (auto _ : state) {
168*58b9f456SAndroid Build Coastguard Worker for (int i = from; i < to; i++) {
169*58b9f456SAndroid Build Coastguard Worker // No need to lock test_vector_mu as ranges
170*58b9f456SAndroid Build Coastguard Worker // do not overlap between threads.
171*58b9f456SAndroid Build Coastguard Worker benchmark::DoNotOptimize(test_vector->at(i) = 1);
172*58b9f456SAndroid Build Coastguard Worker }
173*58b9f456SAndroid Build Coastguard Worker }
174*58b9f456SAndroid Build Coastguard Worker
175*58b9f456SAndroid Build Coastguard Worker if (state.thread_index == 0) {
176*58b9f456SAndroid Build Coastguard Worker delete test_vector;
177*58b9f456SAndroid Build Coastguard Worker }
178*58b9f456SAndroid Build Coastguard Worker }
179*58b9f456SAndroid Build Coastguard Worker BENCHMARK(BM_ParallelMemset)->Arg(10 << 20)->ThreadRange(1, 4);
180*58b9f456SAndroid Build Coastguard Worker
BM_ManualTiming(benchmark::State & state)181*58b9f456SAndroid Build Coastguard Worker static void BM_ManualTiming(benchmark::State& state) {
182*58b9f456SAndroid Build Coastguard Worker int64_t slept_for = 0;
183*58b9f456SAndroid Build Coastguard Worker int64_t microseconds = state.range(0);
184*58b9f456SAndroid Build Coastguard Worker std::chrono::duration<double, std::micro> sleep_duration{
185*58b9f456SAndroid Build Coastguard Worker static_cast<double>(microseconds)};
186*58b9f456SAndroid Build Coastguard Worker
187*58b9f456SAndroid Build Coastguard Worker for (auto _ : state) {
188*58b9f456SAndroid Build Coastguard Worker auto start = std::chrono::high_resolution_clock::now();
189*58b9f456SAndroid Build Coastguard Worker // Simulate some useful workload with a sleep
190*58b9f456SAndroid Build Coastguard Worker std::this_thread::sleep_for(
191*58b9f456SAndroid Build Coastguard Worker std::chrono::duration_cast<std::chrono::nanoseconds>(sleep_duration));
192*58b9f456SAndroid Build Coastguard Worker auto end = std::chrono::high_resolution_clock::now();
193*58b9f456SAndroid Build Coastguard Worker
194*58b9f456SAndroid Build Coastguard Worker auto elapsed =
195*58b9f456SAndroid Build Coastguard Worker std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
196*58b9f456SAndroid Build Coastguard Worker
197*58b9f456SAndroid Build Coastguard Worker state.SetIterationTime(elapsed.count());
198*58b9f456SAndroid Build Coastguard Worker slept_for += microseconds;
199*58b9f456SAndroid Build Coastguard Worker }
200*58b9f456SAndroid Build Coastguard Worker state.SetItemsProcessed(slept_for);
201*58b9f456SAndroid Build Coastguard Worker }
202*58b9f456SAndroid Build Coastguard Worker BENCHMARK(BM_ManualTiming)->Range(1, 1 << 14)->UseRealTime();
203*58b9f456SAndroid Build Coastguard Worker BENCHMARK(BM_ManualTiming)->Range(1, 1 << 14)->UseManualTime();
204*58b9f456SAndroid Build Coastguard Worker
205*58b9f456SAndroid Build Coastguard Worker #ifdef BENCHMARK_HAS_CXX11
206*58b9f456SAndroid Build Coastguard Worker
207*58b9f456SAndroid Build Coastguard Worker template <class... Args>
BM_with_args(benchmark::State & state,Args &&...)208*58b9f456SAndroid Build Coastguard Worker void BM_with_args(benchmark::State& state, Args&&...) {
209*58b9f456SAndroid Build Coastguard Worker for (auto _ : state) {
210*58b9f456SAndroid Build Coastguard Worker }
211*58b9f456SAndroid Build Coastguard Worker }
212*58b9f456SAndroid Build Coastguard Worker BENCHMARK_CAPTURE(BM_with_args, int_test, 42, 43, 44);
213*58b9f456SAndroid Build Coastguard Worker BENCHMARK_CAPTURE(BM_with_args, string_and_pair_test, std::string("abc"),
214*58b9f456SAndroid Build Coastguard Worker std::pair<int, double>(42, 3.8));
215*58b9f456SAndroid Build Coastguard Worker
BM_non_template_args(benchmark::State & state,int,double)216*58b9f456SAndroid Build Coastguard Worker void BM_non_template_args(benchmark::State& state, int, double) {
217*58b9f456SAndroid Build Coastguard Worker while(state.KeepRunning()) {}
218*58b9f456SAndroid Build Coastguard Worker }
219*58b9f456SAndroid Build Coastguard Worker BENCHMARK_CAPTURE(BM_non_template_args, basic_test, 0, 0);
220*58b9f456SAndroid Build Coastguard Worker
221*58b9f456SAndroid Build Coastguard Worker #endif // BENCHMARK_HAS_CXX11
222*58b9f456SAndroid Build Coastguard Worker
BM_DenseThreadRanges(benchmark::State & st)223*58b9f456SAndroid Build Coastguard Worker static void BM_DenseThreadRanges(benchmark::State& st) {
224*58b9f456SAndroid Build Coastguard Worker switch (st.range(0)) {
225*58b9f456SAndroid Build Coastguard Worker case 1:
226*58b9f456SAndroid Build Coastguard Worker assert(st.threads == 1 || st.threads == 2 || st.threads == 3);
227*58b9f456SAndroid Build Coastguard Worker break;
228*58b9f456SAndroid Build Coastguard Worker case 2:
229*58b9f456SAndroid Build Coastguard Worker assert(st.threads == 1 || st.threads == 3 || st.threads == 4);
230*58b9f456SAndroid Build Coastguard Worker break;
231*58b9f456SAndroid Build Coastguard Worker case 3:
232*58b9f456SAndroid Build Coastguard Worker assert(st.threads == 5 || st.threads == 8 || st.threads == 11 ||
233*58b9f456SAndroid Build Coastguard Worker st.threads == 14);
234*58b9f456SAndroid Build Coastguard Worker break;
235*58b9f456SAndroid Build Coastguard Worker default:
236*58b9f456SAndroid Build Coastguard Worker assert(false && "Invalid test case number");
237*58b9f456SAndroid Build Coastguard Worker }
238*58b9f456SAndroid Build Coastguard Worker while (st.KeepRunning()) {
239*58b9f456SAndroid Build Coastguard Worker }
240*58b9f456SAndroid Build Coastguard Worker }
241*58b9f456SAndroid Build Coastguard Worker BENCHMARK(BM_DenseThreadRanges)->Arg(1)->DenseThreadRange(1, 3);
242*58b9f456SAndroid Build Coastguard Worker BENCHMARK(BM_DenseThreadRanges)->Arg(2)->DenseThreadRange(1, 4, 2);
243*58b9f456SAndroid Build Coastguard Worker BENCHMARK(BM_DenseThreadRanges)->Arg(3)->DenseThreadRange(5, 14, 3);
244*58b9f456SAndroid Build Coastguard Worker
245*58b9f456SAndroid Build Coastguard Worker BENCHMARK_MAIN();
246