options_test.cc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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_slow)->Arg(1000)->Unit(benchmark::kSecond);
  25. BENCHMARK(BM_basic)->Range(1, 8);
  26. BENCHMARK(BM_basic)->RangeMultiplier(2)->Range(1, 8);
  27. BENCHMARK(BM_basic)->DenseRange(10, 15);
  28. BENCHMARK(BM_basic)->Args({42, 42});
  29. BENCHMARK(BM_basic)->Ranges({{64, 512}, {64, 512}});
  30. BENCHMARK(BM_basic)->MinTime(0.7);
  31. BENCHMARK(BM_basic)->UseRealTime();
  32. BENCHMARK(BM_basic)->ThreadRange(2, 4);
  33. BENCHMARK(BM_basic)->ThreadPerCpu();
  34. BENCHMARK(BM_basic)->Repetitions(3);
  35. BENCHMARK(BM_basic)
  36. ->RangeMultiplier(std::numeric_limits<int>::max())
  37. ->Range(std::numeric_limits<int64_t>::min(),
  38. std::numeric_limits<int64_t>::max());
  39. // Negative ranges
  40. BENCHMARK(BM_basic)->Range(-64, -1);
  41. BENCHMARK(BM_basic)->RangeMultiplier(4)->Range(-8, 8);
  42. BENCHMARK(BM_basic)->DenseRange(-2, 2, 1);
  43. BENCHMARK(BM_basic)->Ranges({{-64, 1}, {-8, -1}});
  44. void CustomArgs(benchmark::internal::Benchmark* b) {
  45. for (int i = 0; i < 10; ++i) {
  46. b->Arg(i);
  47. }
  48. }
  49. BENCHMARK(BM_basic)->Apply(CustomArgs);
  50. void BM_explicit_iteration_count(benchmark::State& state) {
  51. // Test that benchmarks specified with an explicit iteration count are
  52. // only run once.
  53. static bool invoked_before = false;
  54. assert(!invoked_before);
  55. invoked_before = true;
  56. // Test that the requested iteration count is respected.
  57. assert(state.max_iterations == 42);
  58. size_t actual_iterations = 0;
  59. for (auto _ : state)
  60. ++actual_iterations;
  61. assert(state.iterations() == state.max_iterations);
  62. assert(state.iterations() == 42);
  63. }
  64. BENCHMARK(BM_explicit_iteration_count)->Iterations(42);
  65. BENCHMARK_MAIN();