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