2015-04-06 21:00:06 +00:00
|
|
|
|
|
|
|
#include "benchmark/benchmark.h"
|
|
|
|
|
2015-04-06 23:04:12 +00:00
|
|
|
#include <cassert>
|
2016-02-15 11:04:19 +00:00
|
|
|
#include <memory>
|
2015-04-06 23:04:12 +00:00
|
|
|
|
2016-02-15 11:04:19 +00:00
|
|
|
class MyFixture : public ::benchmark::Fixture {
|
|
|
|
public:
|
2016-02-24 08:36:34 +00:00
|
|
|
void SetUp(const ::benchmark::State& state) {
|
|
|
|
if (state.thread_index == 0) {
|
|
|
|
assert(data.get() == nullptr);
|
|
|
|
data.reset(new int(42));
|
|
|
|
}
|
2016-02-15 11:04:19 +00:00
|
|
|
}
|
2015-04-06 23:04:12 +00:00
|
|
|
|
2016-02-24 08:36:34 +00:00
|
|
|
void TearDown(const ::benchmark::State& state) {
|
|
|
|
if (state.thread_index == 0) {
|
|
|
|
assert(data.get() != nullptr);
|
|
|
|
data.reset();
|
|
|
|
}
|
2016-02-15 11:04:19 +00:00
|
|
|
}
|
2015-04-06 23:04:12 +00:00
|
|
|
|
2016-10-07 18:04:50 +00:00
|
|
|
~MyFixture() { assert(data == nullptr); }
|
2015-04-06 23:04:12 +00:00
|
|
|
|
2016-02-15 11:04:19 +00:00
|
|
|
std::unique_ptr<int> data;
|
2015-04-06 21:00:06 +00:00
|
|
|
};
|
|
|
|
|
2016-10-07 18:04:50 +00:00
|
|
|
BENCHMARK_F(MyFixture, Foo)(benchmark::State &st) {
|
2016-02-15 11:04:19 +00:00
|
|
|
assert(data.get() != nullptr);
|
|
|
|
assert(*data == 42);
|
|
|
|
while (st.KeepRunning()) {
|
|
|
|
}
|
2015-04-06 21:00:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BENCHMARK_DEFINE_F(MyFixture, Bar)(benchmark::State& st) {
|
2016-02-24 08:36:34 +00:00
|
|
|
if (st.thread_index == 0) {
|
|
|
|
assert(data.get() != nullptr);
|
|
|
|
assert(*data == 42);
|
|
|
|
}
|
2015-04-06 21:00:06 +00:00
|
|
|
while (st.KeepRunning()) {
|
2016-02-24 08:36:34 +00:00
|
|
|
assert(data.get() != nullptr);
|
|
|
|
assert(*data == 42);
|
2015-04-06 21:00:06 +00:00
|
|
|
}
|
2016-08-04 19:30:14 +00:00
|
|
|
st.SetItemsProcessed(st.range(0));
|
2015-04-06 21:00:06 +00:00
|
|
|
}
|
|
|
|
BENCHMARK_REGISTER_F(MyFixture, Bar)->Arg(42);
|
2016-02-24 08:36:34 +00:00
|
|
|
BENCHMARK_REGISTER_F(MyFixture, Bar)->Arg(42)->ThreadPerCpu();
|
2015-04-06 21:00:06 +00:00
|
|
|
|
|
|
|
BENCHMARK_MAIN()
|