benchmark/test/fixture_test.cc

50 lines
1.1 KiB
C++
Raw Normal View History

2015-04-07 05:00:06 +08:00
#include "benchmark/benchmark.h"
2015-04-07 07:04:12 +08:00
#include <cassert>
#include <memory>
2015-04-07 07:04:12 +08: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-07 07:04:12 +08:00
void TearDown(const ::benchmark::State& state) {
if (state.thread_index == 0) {
assert(data.get() != nullptr);
data.reset();
}
}
2015-04-07 07:04:12 +08:00
~MyFixture() { assert(data == nullptr); }
2015-04-07 07:04:12 +08:00
std::unique_ptr<int> data;
2015-04-07 05:00:06 +08:00
};
BENCHMARK_F(MyFixture, Foo)(benchmark::State &st) {
assert(data.get() != nullptr);
assert(*data == 42);
while (st.KeepRunning()) {
}
2015-04-07 05:00:06 +08:00
}
BENCHMARK_DEFINE_F(MyFixture, Bar)(benchmark::State& st) {
if (st.thread_index == 0) {
assert(data.get() != nullptr);
assert(*data == 42);
}
2015-04-07 05:00:06 +08:00
while (st.KeepRunning()) {
assert(data.get() != nullptr);
assert(*data == 42);
2015-04-07 05:00:06 +08:00
}
st.SetItemsProcessed(st.range(0));
2015-04-07 05:00:06 +08:00
}
BENCHMARK_REGISTER_F(MyFixture, Bar)->Arg(42);
BENCHMARK_REGISTER_F(MyFixture, Bar)->Arg(42)->ThreadPerCpu();
2015-04-07 05:00:06 +08:00
BENCHMARK_MAIN()