brintos

brintos / llvm-project-archived public Read only

0
0
Text · 8.9 KiB · 8b14017 Raw
301 lines · plain
1#include "benchmark/benchmark.h"2 3#include <assert.h>4#include <math.h>5#include <stdint.h>6 7#include <chrono>8#include <complex>9#include <cstdlib>10#include <iostream>11#include <limits>12#include <list>13#include <map>14#include <mutex>15#include <set>16#include <sstream>17#include <string>18#include <thread>19#include <type_traits>20#include <utility>21#include <vector>22 23#if defined(__GNUC__)24#define BENCHMARK_NOINLINE __attribute__((noinline))25#else26#define BENCHMARK_NOINLINE27#endif28 29namespace {30 31int BENCHMARK_NOINLINE Factorial(int n) {32  return (n == 1) ? 1 : n * Factorial(n - 1);33}34 35double CalculatePi(int depth) {36  double pi = 0.0;37  for (int i = 0; i < depth; ++i) {38    double numerator = static_cast<double>(((i % 2) * 2) - 1);39    double denominator = static_cast<double>((2 * i) - 1);40    pi += numerator / denominator;41  }42  return (pi - 1.0) * 4;43}44 45std::set<int64_t> ConstructRandomSet(int64_t size) {46  std::set<int64_t> s;47  for (int i = 0; i < size; ++i) s.insert(s.end(), i);48  return s;49}50 51std::mutex test_vector_mu;52std::vector<int>* test_vector = nullptr;53 54}  // end namespace55 56static void BM_Factorial(benchmark::State& state) {57  int fac_42 = 0;58  for (auto _ : state) fac_42 = Factorial(8);59  // Prevent compiler optimizations60  std::stringstream ss;61  ss << fac_42;62  state.SetLabel(ss.str());63}64BENCHMARK(BM_Factorial);65BENCHMARK(BM_Factorial)->UseRealTime();66 67static void BM_CalculatePiRange(benchmark::State& state) {68  double pi = 0.0;69  for (auto _ : state) pi = CalculatePi(static_cast<int>(state.range(0)));70  std::stringstream ss;71  ss << pi;72  state.SetLabel(ss.str());73}74BENCHMARK_RANGE(BM_CalculatePiRange, 1, 1024 * 1024);75 76static void BM_CalculatePi(benchmark::State& state) {77  static const int depth = 1024;78  for (auto _ : state) {79    double pi = CalculatePi(static_cast<int>(depth));80    benchmark::DoNotOptimize(pi);81  }82}83BENCHMARK(BM_CalculatePi)->Threads(8);84BENCHMARK(BM_CalculatePi)->ThreadRange(1, 32);85BENCHMARK(BM_CalculatePi)->ThreadPerCpu();86 87static void BM_SetInsert(benchmark::State& state) {88  std::set<int64_t> data;89  for (auto _ : state) {90    state.PauseTiming();91    data = ConstructRandomSet(state.range(0));92    state.ResumeTiming();93    for (int j = 0; j < state.range(1); ++j) data.insert(rand());94  }95  state.SetItemsProcessed(state.iterations() * state.range(1));96  state.SetBytesProcessed(state.iterations() * state.range(1) *97                          static_cast<int64_t>(sizeof(int)));98}99 100// Test many inserts at once to reduce the total iterations needed. Otherwise,101// the slower, non-timed part of each iteration will make the benchmark take102// forever.103BENCHMARK(BM_SetInsert)->Ranges({{1 << 10, 8 << 10}, {128, 512}});104 105template <typename Container,106          typename ValueType = typename Container::value_type>107static void BM_Sequential(benchmark::State& state) {108  ValueType v = 42;109  for (auto _ : state) {110    Container c;111    for (int64_t i = state.range(0); --i;) c.push_back(v);112  }113  const int64_t items_processed = state.iterations() * state.range(0);114  state.SetItemsProcessed(items_processed);115  state.SetBytesProcessed(items_processed * static_cast<int64_t>(sizeof(v)));116}117BENCHMARK_TEMPLATE2(BM_Sequential, std::vector<int>, int)118    ->Range(1 << 0, 1 << 10);119BENCHMARK_TEMPLATE(BM_Sequential, std::list<int>)->Range(1 << 0, 1 << 10);120// Test the variadic version of BENCHMARK_TEMPLATE in C++11 and beyond.121#ifdef BENCHMARK_HAS_CXX11122BENCHMARK_TEMPLATE(BM_Sequential, std::vector<int>, int)->Arg(512);123#endif124 125static void BM_StringCompare(benchmark::State& state) {126  size_t len = static_cast<size_t>(state.range(0));127  std::string s1(len, '-');128  std::string s2(len, '-');129  for (auto _ : state) {130    auto comp = s1.compare(s2);131    benchmark::DoNotOptimize(comp);132  }133}134BENCHMARK(BM_StringCompare)->Range(1, 1 << 20);135 136static void BM_SetupTeardown(benchmark::State& state) {137  if (state.thread_index() == 0) {138    // No need to lock test_vector_mu here as this is running single-threaded.139    test_vector = new std::vector<int>();140  }141  int i = 0;142  for (auto _ : state) {143    std::lock_guard<std::mutex> l(test_vector_mu);144    if (i % 2 == 0)145      test_vector->push_back(i);146    else147      test_vector->pop_back();148    ++i;149  }150  if (state.thread_index() == 0) {151    delete test_vector;152  }153}154BENCHMARK(BM_SetupTeardown)->ThreadPerCpu();155 156static void BM_LongTest(benchmark::State& state) {157  double tracker = 0.0;158  for (auto _ : state) {159    for (int i = 0; i < state.range(0); ++i)160      benchmark::DoNotOptimize(tracker += i);161  }162}163BENCHMARK(BM_LongTest)->Range(1 << 16, 1 << 28);164 165static void BM_ParallelMemset(benchmark::State& state) {166  int64_t size = state.range(0) / static_cast<int64_t>(sizeof(int));167  int thread_size = static_cast<int>(size) / state.threads();168  int from = thread_size * state.thread_index();169  int to = from + thread_size;170 171  if (state.thread_index() == 0) {172    test_vector = new std::vector<int>(static_cast<size_t>(size));173  }174 175  for (auto _ : state) {176    for (int i = from; i < to; i++) {177      // No need to lock test_vector_mu as ranges178      // do not overlap between threads.179      benchmark::DoNotOptimize(test_vector->at(static_cast<size_t>(i)) = 1);180    }181  }182 183  if (state.thread_index() == 0) {184    delete test_vector;185  }186}187BENCHMARK(BM_ParallelMemset)->Arg(10 << 20)->ThreadRange(1, 4);188 189static void BM_ManualTiming(benchmark::State& state) {190  int64_t slept_for = 0;191  int64_t microseconds = state.range(0);192  std::chrono::duration<double, std::micro> sleep_duration{193      static_cast<double>(microseconds)};194 195  for (auto _ : state) {196    auto start = std::chrono::high_resolution_clock::now();197    // Simulate some useful workload with a sleep198    std::this_thread::sleep_for(199        std::chrono::duration_cast<std::chrono::nanoseconds>(sleep_duration));200    auto end = std::chrono::high_resolution_clock::now();201 202    auto elapsed =203        std::chrono::duration_cast<std::chrono::duration<double>>(end - start);204 205    state.SetIterationTime(elapsed.count());206    slept_for += microseconds;207  }208  state.SetItemsProcessed(slept_for);209}210BENCHMARK(BM_ManualTiming)->Range(1, 1 << 14)->UseRealTime();211BENCHMARK(BM_ManualTiming)->Range(1, 1 << 14)->UseManualTime();212 213#ifdef BENCHMARK_HAS_CXX11214 215template <class... Args>216void BM_with_args(benchmark::State& state, Args&&...) {217  for (auto _ : state) {218  }219}220BENCHMARK_CAPTURE(BM_with_args, int_test, 42, 43, 44);221BENCHMARK_CAPTURE(BM_with_args, string_and_pair_test, std::string("abc"),222                  std::pair<int, double>(42, 3.8));223 224void BM_non_template_args(benchmark::State& state, int, double) {225  while (state.KeepRunning()) {226  }227}228BENCHMARK_CAPTURE(BM_non_template_args, basic_test, 0, 0);229 230template <class T, class U, class... ExtraArgs>231void BM_template2_capture(benchmark::State& state, ExtraArgs&&... extra_args) {232  static_assert(std::is_same<T, void>::value, "");233  static_assert(std::is_same<U, char*>::value, "");234  static_assert(std::is_same<ExtraArgs..., unsigned int>::value, "");235  unsigned int dummy[sizeof...(ExtraArgs)] = {extra_args...};236  assert(dummy[0] == 42);237  for (auto _ : state) {238  }239}240BENCHMARK_TEMPLATE2_CAPTURE(BM_template2_capture, void, char*, foo, 42U);241BENCHMARK_CAPTURE((BM_template2_capture<void, char*>), foo, 42U);242 243template <class T, class... ExtraArgs>244void BM_template1_capture(benchmark::State& state, ExtraArgs&&... extra_args) {245  static_assert(std::is_same<T, void>::value, "");246  static_assert(std::is_same<ExtraArgs..., unsigned long>::value, "");247  unsigned long dummy[sizeof...(ExtraArgs)] = {extra_args...};248  assert(dummy[0] == 24);249  for (auto _ : state) {250  }251}252BENCHMARK_TEMPLATE1_CAPTURE(BM_template1_capture, void, foo, 24UL);253BENCHMARK_CAPTURE(BM_template1_capture<void>, foo, 24UL);254 255#endif  // BENCHMARK_HAS_CXX11256 257static void BM_DenseThreadRanges(benchmark::State& st) {258  switch (st.range(0)) {259    case 1:260      assert(st.threads() == 1 || st.threads() == 2 || st.threads() == 3);261      break;262    case 2:263      assert(st.threads() == 1 || st.threads() == 3 || st.threads() == 4);264      break;265    case 3:266      assert(st.threads() == 5 || st.threads() == 8 || st.threads() == 11 ||267             st.threads() == 14);268      break;269    default:270      assert(false && "Invalid test case number");271  }272  while (st.KeepRunning()) {273  }274}275BENCHMARK(BM_DenseThreadRanges)->Arg(1)->DenseThreadRange(1, 3);276BENCHMARK(BM_DenseThreadRanges)->Arg(2)->DenseThreadRange(1, 4, 2);277BENCHMARK(BM_DenseThreadRanges)->Arg(3)->DenseThreadRange(5, 14, 3);278 279static void BM_BenchmarkName(benchmark::State& state) {280  for (auto _ : state) {281  }282 283  // Check that the benchmark name is passed correctly to `state`.284  assert("BM_BenchmarkName" == state.name());285}286BENCHMARK(BM_BenchmarkName);287 288// regression test for #1446289template <typename type>290static void BM_templated_test(benchmark::State& state) {291  for (auto _ : state) {292    type created_string;293    benchmark::DoNotOptimize(created_string);294  }295}296 297static auto BM_templated_test_double = BM_templated_test<std::complex<double>>;298BENCHMARK(BM_templated_test_double);299 300BENCHMARK_MAIN();301