1 2#include "benchmark/benchmark.h" 3 4#include <cassert> 5#include <memory> 6 7class MyFixture : public ::benchmark::Fixture { 8 public: 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 16 void TearDown(const ::benchmark::State& state) { 17 if (state.thread_index == 0) { 18 assert(data.get() != nullptr); 19 data.reset(); 20 } 21 } 22 23 ~MyFixture() { assert(data == nullptr); } 24 25 std::unique_ptr<int> data; 26}; 27 28BENCHMARK_F(MyFixture, Foo)(benchmark::State &st) { 29 assert(data.get() != nullptr); 30 assert(*data == 42); 31 while (st.KeepRunning()) { 32 } 33} 34 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 while (st.KeepRunning()) { 41 assert(data.get() != nullptr); 42 assert(*data == 42); 43 } 44 st.SetItemsProcessed(st.range(0)); 45} 46BENCHMARK_REGISTER_F(MyFixture, Bar)->Arg(42); 47BENCHMARK_REGISTER_F(MyFixture, Bar)->Arg(42)->ThreadPerCpu(); 48 49BENCHMARK_MAIN() 50