You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.2 KiB

2 months ago
  1. #include <cassert>
  2. #include <memory>
  3. #include "benchmark/benchmark.h"
  4. #define FIXTURE_BECHMARK_NAME MyFixture
  5. class FIXTURE_BECHMARK_NAME : public ::benchmark::Fixture {
  6. public:
  7. void SetUp(const ::benchmark::State& state) override {
  8. if (state.thread_index() == 0) {
  9. assert(data.get() == nullptr);
  10. data.reset(new int(42));
  11. }
  12. }
  13. void TearDown(const ::benchmark::State& state) override {
  14. if (state.thread_index() == 0) {
  15. assert(data.get() != nullptr);
  16. data.reset();
  17. }
  18. }
  19. ~FIXTURE_BECHMARK_NAME() override { assert(data == nullptr); }
  20. std::unique_ptr<int> data;
  21. };
  22. BENCHMARK_F(FIXTURE_BECHMARK_NAME, Foo)(benchmark::State& st) {
  23. assert(data.get() != nullptr);
  24. assert(*data == 42);
  25. for (auto _ : st) {
  26. }
  27. }
  28. BENCHMARK_DEFINE_F(FIXTURE_BECHMARK_NAME, Bar)(benchmark::State& st) {
  29. if (st.thread_index() == 0) {
  30. assert(data.get() != nullptr);
  31. assert(*data == 42);
  32. }
  33. for (auto _ : st) {
  34. assert(data.get() != nullptr);
  35. assert(*data == 42);
  36. }
  37. st.SetItemsProcessed(st.range(0));
  38. }
  39. BENCHMARK_REGISTER_F(FIXTURE_BECHMARK_NAME, Bar)->Arg(42);
  40. BENCHMARK_REGISTER_F(FIXTURE_BECHMARK_NAME, Bar)->Arg(42)->ThreadPerCpu();
  41. BENCHMARK_MAIN();