benchmark_test.cc 7.1 KB

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