Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/benchmark/benchmark_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -486,12 +486,12 @@ class Fixture: public internal::Benchmark {
Fixture() : internal::Benchmark("") {}

virtual void Run(State& st) {
this->SetUp();
this->SetUp(st);
this->BenchmarkCase(st);
this->TearDown();
}

virtual void SetUp() {}
virtual void SetUp(const State&) {}
virtual void TearDown() {}

protected:
Expand Down
3 changes: 3 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ add_test(basic_benchmark basic_test --benchmark_min_time=0.01)
compile_benchmark_test(fixture_test)
add_test(fixture_test fixture_test --benchmark_min_time=0.01)

compile_benchmark_test(map_test)
add_test(map_test map_test --benchmark_min_time=0.01)

compile_benchmark_test(cxx03_test)
set_target_properties(cxx03_test
PROPERTIES COMPILE_FLAGS "${CXX03_FLAGS}")
Expand Down
39 changes: 19 additions & 20 deletions test/fixture_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@
#include "benchmark/benchmark.h"

#include <cassert>
#include <memory>

class MyFixture : public ::benchmark::Fixture
{
public:
void SetUp() {
data = new int(42);
}
class MyFixture : public ::benchmark::Fixture {
public:
void SetUp(const ::benchmark::State&) {
assert(data.get() == nullptr);
data.reset(new int(42));
}

void TearDown() {
assert(data != nullptr);
delete data;
data = nullptr;
}
void TearDown() {
assert(data.get() != nullptr);
data.release();
}

~MyFixture() {
assert(data == nullptr);
}
~MyFixture() {
assert(data == nullptr);
}

int* data;
std::unique_ptr<int> data;
};


BENCHMARK_F(MyFixture, Foo)(benchmark::State& st) {
assert(data != nullptr);
assert(*data == 42);
while (st.KeepRunning()) {
}
assert(data.get() != nullptr);
assert(*data == 42);
while (st.KeepRunning()) {
}
}

BENCHMARK_DEFINE_F(MyFixture, Bar)(benchmark::State& st) {
Expand All @@ -38,5 +38,4 @@ BENCHMARK_DEFINE_F(MyFixture, Bar)(benchmark::State& st) {
}
BENCHMARK_REGISTER_F(MyFixture, Bar)->Arg(42);


BENCHMARK_MAIN()
57 changes: 57 additions & 0 deletions test/map_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "benchmark/benchmark.h"

#include <map>

namespace {

std::map<int, int> ConstructRandomMap(int size) {
std::map<int, int> m;
for (int i = 0; i < size; ++i) {
m.insert(std::make_pair(rand() % size, rand() % size));
}
return m;
}

} // namespace

// Basic version.
static void BM_MapLookup(benchmark::State& state) {
const int size = state.range_x();
while (state.KeepRunning()) {
state.PauseTiming();
std::map<int, int> m = ConstructRandomMap(size);
state.ResumeTiming();
for (int i = 0; i < size; ++i) {
benchmark::DoNotOptimize(m.find(rand() % size));
}
}
state.SetItemsProcessed(state.iterations() * size);
}
BENCHMARK(BM_MapLookup)->Range(1 << 3, 1 << 12);

// Using fixtures.
class MapFixture : public ::benchmark::Fixture {
public:
void SetUp(const ::benchmark::State& st) {
m = ConstructRandomMap(st.range_x());
}

void TearDown() {
m.clear();
}

std::map<int, int> m;
};

BENCHMARK_DEFINE_F(MapFixture, Lookup)(benchmark::State& state) {
const int size = state.range_x();
while (state.KeepRunning()) {
for (int i = 0; i < size; ++i) {
benchmark::DoNotOptimize(m.find(rand() % size));
}
}
state.SetItemsProcessed(state.iterations() * size);
}
BENCHMARK_REGISTER_F(MapFixture, Lookup)->Range(1<<3, 1<<12);

BENCHMARK_MAIN()