mirror of
https://github.com/google/benchmark.git
synced 2025-03-04 22:30:13 +08:00
Merge pull request #186 from amin-jabri/multithreaded_Fixture_TearDown
Pass const State to Fixture::TearDown. Fix memory leak in fixture_test
This commit is contained in:
commit
e4ad1afa1f
@ -491,11 +491,11 @@ public:
|
|||||||
virtual void Run(State& st) {
|
virtual void Run(State& st) {
|
||||||
this->SetUp(st);
|
this->SetUp(st);
|
||||||
this->BenchmarkCase(st);
|
this->BenchmarkCase(st);
|
||||||
this->TearDown();
|
this->TearDown(st);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void SetUp(const State&) {}
|
virtual void SetUp(const State&) {}
|
||||||
virtual void TearDown() {}
|
virtual void TearDown(const State&) {}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void BenchmarkCase(State&) = 0;
|
virtual void BenchmarkCase(State&) = 0;
|
||||||
|
@ -6,14 +6,18 @@
|
|||||||
|
|
||||||
class MyFixture : public ::benchmark::Fixture {
|
class MyFixture : public ::benchmark::Fixture {
|
||||||
public:
|
public:
|
||||||
void SetUp(const ::benchmark::State&) {
|
void SetUp(const ::benchmark::State& state) {
|
||||||
assert(data.get() == nullptr);
|
if (state.thread_index == 0) {
|
||||||
data.reset(new int(42));
|
assert(data.get() == nullptr);
|
||||||
|
data.reset(new int(42));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TearDown() {
|
void TearDown(const ::benchmark::State& state) {
|
||||||
assert(data.get() != nullptr);
|
if (state.thread_index == 0) {
|
||||||
data.release();
|
assert(data.get() != nullptr);
|
||||||
|
data.reset();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
~MyFixture() {
|
~MyFixture() {
|
||||||
@ -32,10 +36,17 @@ BENCHMARK_F(MyFixture, Foo)(benchmark::State& st) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
BENCHMARK_DEFINE_F(MyFixture, Bar)(benchmark::State& st) {
|
BENCHMARK_DEFINE_F(MyFixture, Bar)(benchmark::State& st) {
|
||||||
|
if (st.thread_index == 0) {
|
||||||
|
assert(data.get() != nullptr);
|
||||||
|
assert(*data == 42);
|
||||||
|
}
|
||||||
while (st.KeepRunning()) {
|
while (st.KeepRunning()) {
|
||||||
|
assert(data.get() != nullptr);
|
||||||
|
assert(*data == 42);
|
||||||
}
|
}
|
||||||
st.SetItemsProcessed(st.range_x());
|
st.SetItemsProcessed(st.range_x());
|
||||||
}
|
}
|
||||||
BENCHMARK_REGISTER_F(MyFixture, Bar)->Arg(42);
|
BENCHMARK_REGISTER_F(MyFixture, Bar)->Arg(42);
|
||||||
|
BENCHMARK_REGISTER_F(MyFixture, Bar)->Arg(42)->ThreadPerCpu();
|
||||||
|
|
||||||
BENCHMARK_MAIN()
|
BENCHMARK_MAIN()
|
||||||
|
@ -36,7 +36,7 @@ class MapFixture : public ::benchmark::Fixture {
|
|||||||
m = ConstructRandomMap(st.range_x());
|
m = ConstructRandomMap(st.range_x());
|
||||||
}
|
}
|
||||||
|
|
||||||
void TearDown() {
|
void TearDown(const ::benchmark::State&) {
|
||||||
m.clear();
|
m.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user