aboutsummaryrefslogtreecommitdiff
path: root/test/benchmark_test.cc
diff options
context:
space:
mode:
authorDominic Hamon <dominic+github@google.com>2013-12-18 16:55:45 -0800
committerDominic Hamon <dominic+github@google.com>2013-12-18 16:58:23 -0800
commit403f35442375f2ee858981b79421ca321645df08 (patch)
tree74991ad472ebb95c22a16401e78f2389c741892f /test/benchmark_test.cc
downloadgoogle-benchmark-403f35442375f2ee858981b79421ca321645df08.tar.gz
Initial commit
Benchmark library builds and runs but only single-threaded. Multithreaded support needs a bit more love. Currently requires some C++11 support (g++ 4.6.3 seems to work).
Diffstat (limited to 'test/benchmark_test.cc')
-rw-r--r--test/benchmark_test.cc138
1 files changed, 138 insertions, 0 deletions
diff --git a/test/benchmark_test.cc b/test/benchmark_test.cc
new file mode 100644
index 0000000..7c0c6d7
--- /dev/null
+++ b/test/benchmark_test.cc
@@ -0,0 +1,138 @@
+#include "benchmark/benchmark.h"
+
+#include <math.h>
+#include <stdint.h>
+
+#include <limits>
+#include <list>
+#include <map>
+#include <set>
+#include <sstream>
+#include <vector>
+
+namespace {
+
+int ATTRIBUTE_NOINLINE Factorial(uint32_t n) {
+ return (n == 1) ? 1 : n * Factorial(n - 1);
+}
+
+double CalculatePi(int depth) {
+ double pi = 0.0;
+ for (int i = 0; i < depth; ++i) {
+ double numerator = static_cast<double>(((i % 2) * 2) - 1);
+ double denominator = static_cast<double>((2 * i) - 1);
+ pi += numerator / denominator;
+ }
+ return (pi - 1.0) * 4;
+}
+
+std::set<int> ConstructRandomSet(int size) {
+ std::set<int> s;
+ for (int i = 0; i < size; ++i)
+ s.insert(i);
+ return s;
+}
+
+static std::vector<int>* test_vector = NULL;
+
+} // end namespace
+
+#ifdef DEBUG
+static void BM_Factorial(benchmark::State& state) {
+ int fac_42 = 0;
+ while (state.KeepRunning())
+ fac_42 = Factorial(8);
+ // Prevent compiler optimizations
+ CHECK(fac_42 != std::numeric_limits<int>::max());
+}
+BENCHMARK(BM_Factorial);
+#endif
+
+static void BM_CalculatePiRange(benchmark::State& state) {
+ double pi = 0.0;
+ while (state.KeepRunning())
+ pi = CalculatePi(state.range_x());
+ std::stringstream ss;
+ ss << pi;
+ state.SetLabel(ss.str());
+}
+BENCHMARK_RANGE(BM_CalculatePiRange, 1, 1024 * 1024);
+
+static void BM_CalculatePi(benchmark::State& state) {
+ static const int depth = 1024;
+ double pi ATTRIBUTE_UNUSED = 0.0;
+ while (state.KeepRunning()) {
+ pi = CalculatePi(depth);
+ }
+}
+BENCHMARK(BM_CalculatePi)->Threads(8);
+BENCHMARK(BM_CalculatePi)->ThreadRange(1, 32);
+BENCHMARK(BM_CalculatePi)->ThreadPerCpu();
+
+static void BM_SetInsert(benchmark::State& state) {
+ while (state.KeepRunning()) {
+ state.PauseTiming();
+ std::set<int> data = ConstructRandomSet(state.range_x());
+ state.ResumeTiming();
+ for (int j = 0; j < state.range_y(); ++j)
+ data.insert(rand());
+ }
+}
+BENCHMARK(BM_SetInsert)->RangePair(1<<10,8<<10, 1,10);
+
+template<typename Q>
+static void BM_Sequential(benchmark::State& state) {
+ Q q;
+ typename Q::value_type v;
+ while (state.KeepRunning())
+ for (int i = state.range_x(); --i; )
+ q.push_back(v);
+ const int64_t items_processed =
+ static_cast<int64_t>(state.iterations()) * state.range_x();
+ state.SetItemsProcessed(items_processed);
+ state.SetBytesProcessed(items_processed * sizeof(v));
+}
+BENCHMARK_TEMPLATE(BM_Sequential, std::vector<int>)->Range(1 << 0, 1 << 10);
+BENCHMARK_TEMPLATE(BM_Sequential, std::list<int>)->Range(1 << 0, 1 << 10);
+
+static void BM_StringCompare(benchmark::State& state) {
+ std::string s1(state.range_x(), '-');
+ std::string s2(state.range_x(), '-');
+ int r = 0;
+ while (state.KeepRunning())
+ r |= s1.compare(s2);
+ // Prevent compiler optimizations
+ CHECK(r != std::numeric_limits<int>::max());
+}
+BENCHMARK(BM_StringCompare)->Range(1, 1<<20);
+
+static void BM_SetupTeardown(benchmark::State& state) {
+ if (state.thread_index == 0)
+ test_vector = new std::vector<int>();
+ while (state.KeepRunning())
+ test_vector->push_back(0);
+ if (state.thread_index == 0) {
+ delete test_vector;
+ test_vector = NULL;
+ }
+}
+BENCHMARK(BM_SetupTeardown);
+
+static void BM_LongTest(benchmark::State& state) {
+ double tracker = 0.0;
+ while (state.KeepRunning())
+ for (int i = 0; i < state.range_x(); ++i)
+ tracker += i;
+ CHECK(tracker != 0.0);
+}
+BENCHMARK(BM_LongTest)->Range(1<<16,1<<28);
+
+int main(int argc, const char* argv[]) {
+ benchmark::Initialize(&argc, argv);
+
+ CHECK(Factorial(8) == 40320);
+ CHECK(CalculatePi(1) == 0.0);
+
+ benchmark::RunSpecifiedBenchmarks();
+}
+