options_test.cc 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include "benchmark/benchmark.h"
  2. #include <chrono>
  3. #include <thread>
  4. #if defined(NDEBUG)
  5. #undef NDEBUG
  6. #endif
  7. #include <cassert>
  8. void BM_basic(benchmark::State& state) {
  9. for (auto _ : state) {
  10. }
  11. }
  12. void BM_basic_slow(benchmark::State& state) {
  13. std::chrono::milliseconds sleep_duration(state.range(0));
  14. for (auto _ : state) {
  15. std::this_thread::sleep_for(
  16. std::chrono::duration_cast<std::chrono::nanoseconds>(sleep_duration));
  17. }
  18. }
  19. BENCHMARK(BM_basic);
  20. BENCHMARK(BM_basic)->Arg(42);
  21. BENCHMARK(BM_basic_slow)->Arg(10)->Unit(benchmark::kNanosecond);
  22. BENCHMARK(BM_basic_slow)->Arg(100)->Unit(benchmark::kMicrosecond);
  23. BENCHMARK(BM_basic_slow)->Arg(1000)->Unit(benchmark::kMillisecond);
  24. BENCHMARK(BM_basic)->Range(1, 8);
  25. BENCHMARK(BM_basic)->RangeMultiplier(2)->Range(1, 8);
  26. BENCHMARK(BM_basic)->DenseRange(10, 15);
  27. BENCHMARK(BM_basic)->Args({42, 42});
  28. BENCHMARK(BM_basic)->Ranges({{64, 512}, {64, 512}});
  29. BENCHMARK(BM_basic)->MinTime(0.7);
  30. BENCHMARK(BM_basic)->UseRealTime();
  31. BENCHMARK(BM_basic)->ThreadRange(2, 4);
  32. BENCHMARK(BM_basic)->ThreadPerCpu();
  33. BENCHMARK(BM_basic)->Repetitions(3);
  34. void CustomArgs(benchmark::internal::Benchmark* b) {
  35. for (int i = 0; i < 10; ++i) {
  36. b->Arg(i);
  37. }
  38. }
  39. BENCHMARK(BM_basic)->Apply(CustomArgs);
  40. void BM_explicit_iteration_count(benchmark::State& state) {
  41. // Test that benchmarks specified with an explicit iteration count are
  42. // only run once.
  43. static bool invoked_before = false;
  44. assert(!invoked_before);
  45. invoked_before = true;
  46. // Test that the requested iteration count is respected.
  47. assert(state.max_iterations == 42);
  48. size_t actual_iterations = 0;
  49. for (auto _ : state)
  50. ++actual_iterations;
  51. assert(state.iterations() == state.max_iterations);
  52. assert(state.iterations() == 42);
  53. }
  54. BENCHMARK(BM_explicit_iteration_count)->Iterations(42);
  55. BENCHMARK_MAIN();