summaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/benchmark_timer
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
committerJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
commit72231250ed81e10d66bfe70701e64fa5fe50f712 (patch)
tree2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /vespalib/src/tests/benchmark_timer
Publish
Diffstat (limited to 'vespalib/src/tests/benchmark_timer')
-rw-r--r--vespalib/src/tests/benchmark_timer/.gitignore1
-rw-r--r--vespalib/src/tests/benchmark_timer/CMakeLists.txt8
-rw-r--r--vespalib/src/tests/benchmark_timer/benchmark_timer_test.cpp43
3 files changed, 52 insertions, 0 deletions
diff --git a/vespalib/src/tests/benchmark_timer/.gitignore b/vespalib/src/tests/benchmark_timer/.gitignore
new file mode 100644
index 00000000000..6837dcd2afe
--- /dev/null
+++ b/vespalib/src/tests/benchmark_timer/.gitignore
@@ -0,0 +1 @@
+vespalib_benchmark_timer_test_app
diff --git a/vespalib/src/tests/benchmark_timer/CMakeLists.txt b/vespalib/src/tests/benchmark_timer/CMakeLists.txt
new file mode 100644
index 00000000000..605e1723d19
--- /dev/null
+++ b/vespalib/src/tests/benchmark_timer/CMakeLists.txt
@@ -0,0 +1,8 @@
+# Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+vespa_add_executable(vespalib_benchmark_timer_test_app
+ SOURCES
+ benchmark_timer_test.cpp
+ DEPENDS
+ vespalib
+)
+vespa_add_test(NAME vespalib_benchmark_timer_test_app COMMAND vespalib_benchmark_timer_test_app BENCHMARK)
diff --git a/vespalib/src/tests/benchmark_timer/benchmark_timer_test.cpp b/vespalib/src/tests/benchmark_timer/benchmark_timer_test.cpp
new file mode 100644
index 00000000000..42f6af0e1ea
--- /dev/null
+++ b/vespalib/src/tests/benchmark_timer/benchmark_timer_test.cpp
@@ -0,0 +1,43 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+#include <vespa/fastos/fastos.h>
+#include <vespa/vespalib/testkit/test_kit.h>
+#include <vespa/vespalib/util/benchmark_timer.h>
+
+using namespace vespalib;
+
+TEST("require that the benchmark timer can be used as advertised") {
+ BenchmarkTimer timer(1.0);
+ while (timer.has_budget()) {
+ timer.before();
+ FastOS_Thread::Sleep(5);
+ timer.after();
+ }
+ EXPECT_TRUE(timer.min_time() >= 0.0);
+ fprintf(stderr, "5 ms sleep takes: %g ms\n", timer.min_time() * 1000.0);
+}
+
+TEST("require that the benchmark timer all-in-one benchmarking works") {
+ uint32_t sleep_time = 5;
+ double t = BenchmarkTimer::benchmark([sleep_time](){FastOS_Thread::Sleep(sleep_time);}, 1.0);
+ fprintf(stderr, "5 ms sleep takes: %g ms\n", t * 1000.0);
+}
+
+TEST("require that the benchmark timer all-in-one benchmarking with baseline works") {
+ uint32_t work_time = 10;
+ uint32_t baseline_time = 5;
+ double t = BenchmarkTimer::benchmark([&](){FastOS_Thread::Sleep(work_time);},
+ [&](){FastOS_Thread::Sleep(baseline_time);}, 1.0);
+ fprintf(stderr, "10 ms sleep - 5 ms sleep takes: %g ms\n", t * 1000.0);
+}
+
+TEST("require that the benchmark timer all-in-one benchmarking with baseline and specified loop count works") {
+ uint32_t work_time = 2;
+ uint32_t baseline_time = 1;
+ uint32_t loop_cnt = 0;
+ double t = BenchmarkTimer::benchmark([&](){FastOS_Thread::Sleep(work_time); ++loop_cnt;},
+ [&](){FastOS_Thread::Sleep(baseline_time);}, 7, 0.0);
+ EXPECT_EQUAL(loop_cnt, 7u);
+ fprintf(stderr, "2 ms sleep - 1 ms sleep takes: %g ms\n", t * 1000.0);
+}
+
+TEST_MAIN() { TEST_RUN_ALL(); }