summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/benchmark/test
diff options
context:
space:
mode:
Diffstat (limited to '3rdparty/benchmark/test')
-rw-r--r--3rdparty/benchmark/test/CMakeLists.txt3
-rw-r--r--3rdparty/benchmark/test/benchmark_test.cc24
-rw-r--r--3rdparty/benchmark/test/fixture_test.cc48
-rw-r--r--3rdparty/benchmark/test/map_test.cc58
4 files changed, 114 insertions, 19 deletions
diff --git a/3rdparty/benchmark/test/CMakeLists.txt b/3rdparty/benchmark/test/CMakeLists.txt
index 7e4f4854710..a10a53a9748 100644
--- a/3rdparty/benchmark/test/CMakeLists.txt
+++ b/3rdparty/benchmark/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/3rdparty/benchmark/test/benchmark_test.cc b/3rdparty/benchmark/test/benchmark_test.cc
index 2d268ce4121..97abb68fdb8 100644
--- a/3rdparty/benchmark/test/benchmark_test.cc
+++ b/3rdparty/benchmark/test/benchmark_test.cc
@@ -150,5 +150,29 @@ static void BM_LongTest(benchmark::State& state) {
}
BENCHMARK(BM_LongTest)->Range(1<<16,1<<28);
+static void BM_ParallelMemset(benchmark::State& state) {
+ int size = state.range_x() / sizeof(int);
+ int thread_size = size / state.threads;
+ int from = thread_size * state.thread_index;
+ int to = from + thread_size;
+
+ if (state.thread_index == 0) {
+ test_vector = new std::vector<int>(size);
+ }
+
+ while (state.KeepRunning()) {
+ for (int i = from; i < to; i++) {
+ // No need to lock test_vector_mu as ranges
+ // do not overlap between threads.
+ benchmark::DoNotOptimize(test_vector->at(i) = 1);
+ }
+ }
+
+ if (state.thread_index == 0) {
+ delete test_vector;
+ }
+}
+BENCHMARK(BM_ParallelMemset)->Arg(10 << 20)->ThreadRange(1, 4);
+
BENCHMARK_MAIN()
diff --git a/3rdparty/benchmark/test/fixture_test.cc b/3rdparty/benchmark/test/fixture_test.cc
index 8aea6ef0601..bf800fda20d 100644
--- a/3rdparty/benchmark/test/fixture_test.cc
+++ b/3rdparty/benchmark/test/fixture_test.cc
@@ -2,41 +2,51 @@
#include "benchmark/benchmark.h"
#include <cassert>
-
-class MyFixture : public ::benchmark::Fixture
-{
-public:
- void SetUp() {
- data = new int(42);
+#include <memory>
+
+class MyFixture : public ::benchmark::Fixture {
+ public:
+ void SetUp(const ::benchmark::State& state) {
+ if (state.thread_index == 0) {
+ assert(data.get() == nullptr);
+ data.reset(new int(42));
}
+ }
- void TearDown() {
- assert(data != nullptr);
- delete data;
- data = nullptr;
+ void TearDown(const ::benchmark::State& state) {
+ if (state.thread_index == 0) {
+ assert(data.get() != nullptr);
+ data.reset();
}
+ }
- ~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) {
+ if (st.thread_index == 0) {
+ assert(data.get() != nullptr);
+ assert(*data == 42);
+ }
while (st.KeepRunning()) {
+ assert(data.get() != nullptr);
+ assert(*data == 42);
}
st.SetItemsProcessed(st.range_x());
}
BENCHMARK_REGISTER_F(MyFixture, Bar)->Arg(42);
-
+BENCHMARK_REGISTER_F(MyFixture, Bar)->Arg(42)->ThreadPerCpu();
BENCHMARK_MAIN()
diff --git a/3rdparty/benchmark/test/map_test.cc b/3rdparty/benchmark/test/map_test.cc
new file mode 100644
index 00000000000..5eccf8d39c4
--- /dev/null
+++ b/3rdparty/benchmark/test/map_test.cc
@@ -0,0 +1,58 @@
+#include "benchmark/benchmark.h"
+
+#include <cstdlib>
+#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(const ::benchmark::State&) {
+ 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()