Benchmark.tests.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "catch.hpp"
  2. #include <map>
  3. #if defined(CATCH_CONFIG_ENABLE_BENCHMARKING)
  4. namespace {
  5. std::uint64_t Fibonacci(std::uint64_t number) {
  6. return number < 2 ? 1 : Fibonacci(number - 1) + Fibonacci(number - 2);
  7. }
  8. }
  9. TEST_CASE("Benchmark Fibonacci", "[!benchmark]") {
  10. CHECK(Fibonacci(0) == 1);
  11. // some more asserts..
  12. CHECK(Fibonacci(5) == 8);
  13. // some more asserts..
  14. BENCHMARK("Fibonacci 20") {
  15. return Fibonacci(20);
  16. };
  17. BENCHMARK("Fibonacci 25") {
  18. return Fibonacci(25);
  19. };
  20. BENCHMARK("Fibonacci 30") {
  21. return Fibonacci(30);
  22. };
  23. BENCHMARK("Fibonacci 35") {
  24. return Fibonacci(35);
  25. };
  26. }
  27. TEST_CASE("Benchmark containers", "[!benchmark]") {
  28. static const int size = 100;
  29. std::vector<int> v;
  30. std::map<int, int> m;
  31. SECTION("without generator") {
  32. BENCHMARK("Load up a vector") {
  33. v = std::vector<int>();
  34. for (int i = 0; i < size; ++i)
  35. v.push_back(i);
  36. };
  37. REQUIRE(v.size() == size);
  38. // test optimizer control
  39. BENCHMARK("Add up a vector's content") {
  40. uint64_t add = 0;
  41. for (int i = 0; i < size; ++i)
  42. add += v[i];
  43. return add;
  44. };
  45. BENCHMARK("Load up a map") {
  46. m = std::map<int, int>();
  47. for (int i = 0; i < size; ++i)
  48. m.insert({ i, i + 1 });
  49. };
  50. REQUIRE(m.size() == size);
  51. BENCHMARK("Reserved vector") {
  52. v = std::vector<int>();
  53. v.reserve(size);
  54. for (int i = 0; i < size; ++i)
  55. v.push_back(i);
  56. };
  57. REQUIRE(v.size() == size);
  58. BENCHMARK("Resized vector") {
  59. v = std::vector<int>();
  60. v.resize(size);
  61. for (int i = 0; i < size; ++i)
  62. v[i] = i;
  63. };
  64. REQUIRE(v.size() == size);
  65. int array[size];
  66. BENCHMARK("A fixed size array that should require no allocations") {
  67. for (int i = 0; i < size; ++i)
  68. array[i] = i;
  69. };
  70. int sum = 0;
  71. for (int i = 0; i < size; ++i)
  72. sum += array[i];
  73. REQUIRE(sum > size);
  74. SECTION("XYZ") {
  75. BENCHMARK_ADVANCED("Load up vector with chronometer")(Catch::Benchmark::Chronometer meter) {
  76. std::vector<int> k;
  77. meter.measure([&](int idx) {
  78. k = std::vector<int>();
  79. for (int i = 0; i < size; ++i)
  80. k.push_back(idx);
  81. });
  82. REQUIRE(k.size() == size);
  83. };
  84. int runs = 0;
  85. BENCHMARK("Fill vector indexed", benchmarkIndex) {
  86. v = std::vector<int>();
  87. v.resize(size);
  88. for (int i = 0; i < size; ++i)
  89. v[i] = benchmarkIndex;
  90. runs = benchmarkIndex;
  91. };
  92. for (size_t i = 0; i < v.size(); ++i) {
  93. REQUIRE(v[i] == runs);
  94. }
  95. }
  96. }
  97. SECTION("with generator") {
  98. auto generated = GENERATE(range(0, 10));
  99. BENCHMARK("Fill vector generated") {
  100. v = std::vector<int>();
  101. v.resize(size);
  102. for (int i = 0; i < size; ++i)
  103. v[i] = generated;
  104. };
  105. for (size_t i = 0; i < v.size(); ++i) {
  106. REQUIRE(v[i] == generated);
  107. }
  108. }
  109. SECTION("construct and destroy example") {
  110. BENCHMARK_ADVANCED("construct")(Catch::Benchmark::Chronometer meter) {
  111. std::vector<Catch::Benchmark::storage_for<std::string>> storage(meter.runs());
  112. meter.measure([&](int i) { storage[i].construct("thing"); });
  113. };
  114. BENCHMARK_ADVANCED("destroy")(Catch::Benchmark::Chronometer meter) {
  115. std::vector<Catch::Benchmark::destructable_object<std::string>> storage(meter.runs());
  116. for(auto&& o : storage)
  117. o.construct("thing");
  118. meter.measure([&](int i) { storage[i].destruct(); });
  119. };
  120. }
  121. }
  122. #endif // CATCH_CONFIG_ENABLE_BENCHMARKING