aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDominic Hamon <dominichamon@users.noreply.github.com>2016-02-16 02:08:29 -0800
committerDominic Hamon <dominichamon@users.noreply.github.com>2016-02-16 02:08:29 -0800
commit31e71be77c0f45400628cf618c0360d544a522c5 (patch)
tree48bdb6802db68153b754b7775ce95743e681f377
parent7fd3fa8e3c67c550a98e35a6d22962edd8460db9 (diff)
parent53068f974c0d69849b6437ffe0d545aec2932309 (diff)
downloadgoogle-benchmark-31e71be77c0f45400628cf618c0360d544a522c5.tar.gz
Merge pull request #181 from google/map_test
Pass const state to Fixture::SetUp. Add map_test.
-rw-r--r--include/benchmark/benchmark_api.h4
-rw-r--r--test/CMakeLists.txt3
-rw-r--r--test/fixture_test.cc39
-rw-r--r--test/map_test.cc57
4 files changed, 81 insertions, 22 deletions
diff --git a/include/benchmark/benchmark_api.h b/include/benchmark/benchmark_api.h
index 10063f6..0c5115d 100644
--- a/include/benchmark/benchmark_api.h
+++ b/include/benchmark/benchmark_api.h
@@ -489,12 +489,12 @@ public:
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:
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 7e4f485..a10a53a 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -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}")
diff --git a/test/fixture_test.cc b/test/fixture_test.cc
index 8aea6ef..92fbc4c 100644
--- a/test/fixture_test.cc
+++ b/test/fixture_test.cc
@@ -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) {
@@ -38,5 +38,4 @@ BENCHMARK_DEFINE_F(MyFixture, Bar)(benchmark::State& st) {
}
BENCHMARK_REGISTER_F(MyFixture, Bar)->Arg(42);
-
BENCHMARK_MAIN()
diff --git a/test/map_test.cc b/test/map_test.cc
new file mode 100644
index 0000000..8d5f6ec
--- /dev/null
+++ b/test/map_test.cc
@@ -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()