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