benchmark/test/fixture_test.cc

50 lines
1 KiB
C++
Raw Normal View History

2015-04-06 21:00:06 +00:00
#include "benchmark/benchmark.h"
2015-04-06 23:04:12 +00:00
#include <cassert>
#include <memory>
2015-04-06 23:04:12 +00:00
class MyFixture : public ::benchmark::Fixture {
public:
void SetUp(const ::benchmark::State& state) {
if (state.thread_index == 0) {
assert(data.get() == nullptr);
data.reset(new int(42));
}
}
2015-04-06 23:04:12 +00:00
void TearDown(const ::benchmark::State& state) {
if (state.thread_index == 0) {
assert(data.get() != nullptr);
data.reset();
}
}
2015-04-06 23:04:12 +00:00
~MyFixture() { assert(data == nullptr); }
2015-04-06 23:04:12 +00:00
std::unique_ptr<int> data;
2015-04-06 21:00:06 +00:00
};
BENCHMARK_F(MyFixture, Foo)(benchmark::State &st) {
assert(data.get() != nullptr);
assert(*data == 42);
for (auto _ : st) {
}
2015-04-06 21:00:06 +00:00
}
BENCHMARK_DEFINE_F(MyFixture, Bar)(benchmark::State& st) {
if (st.thread_index == 0) {
assert(data.get() != nullptr);
assert(*data == 42);
}
for (auto _ : st) {
assert(data.get() != nullptr);
assert(*data == 42);
2015-04-06 21:00:06 +00:00
}
st.SetItemsProcessed(st.range(0));
2015-04-06 21:00:06 +00:00
}
BENCHMARK_REGISTER_F(MyFixture, Bar)->Arg(42);
BENCHMARK_REGISTER_F(MyFixture, Bar)->Arg(42)->ThreadPerCpu();
2015-04-06 21:00:06 +00:00
BENCHMARK_MAIN();