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