diff options
Diffstat (limited to 'vespalib')
-rw-r--r-- | vespalib/src/tests/time/CMakeLists.txt | 8 | ||||
-rw-r--r-- | vespalib/src/tests/time/time_test.cpp | 39 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/CMakeLists.txt | 3 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/time.cpp | 9 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/time.h | 53 |
5 files changed, 111 insertions, 1 deletions
diff --git a/vespalib/src/tests/time/CMakeLists.txt b/vespalib/src/tests/time/CMakeLists.txt index 4fa8bad5eed..e43bd9097e5 100644 --- a/vespalib/src/tests/time/CMakeLists.txt +++ b/vespalib/src/tests/time/CMakeLists.txt @@ -6,3 +6,11 @@ vespa_add_executable(vespalib_time_box_test_app TEST vespalib ) vespa_add_test(NAME vespalib_time_box_test_app COMMAND vespalib_time_box_test_app) +vespa_add_executable(vespalib_time_test_app TEST + SOURCES + time_test.cpp + DEPENDS + vespalib + gtest +) +vespa_add_test(NAME vespalib_time_test_app COMMAND vespalib_time_test_app) diff --git a/vespalib/src/tests/time/time_test.cpp b/vespalib/src/tests/time/time_test.cpp new file mode 100644 index 00000000000..667511f6a94 --- /dev/null +++ b/vespalib/src/tests/time/time_test.cpp @@ -0,0 +1,39 @@ +// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include <vespa/vespalib/util/time.h> +#include <vespa/vespalib/gtest/gtest.h> +#include <thread> + +using namespace vespalib; + +TEST(TimeTest, steady_time_is_compatible_with_steady_clock) { + steady_time t = steady_clock::now(); + (void) t; +} + +TEST(TimeTest, system_time_is_compatible_with_system_clock) { + system_time t = system_clock::now(); + (void) t; +} + +TEST(TimeTest, timer_can_measure_elapsed_time) { + Timer timer; + std::this_thread::sleep_for(10ms); + auto elapsed = timer.elapsed(); + EXPECT_GE(elapsed, 10ms); + fprintf(stderr, "sleep(10ms) took %ld us\n", count_us(elapsed)); +} + +TEST(TimeTest, double_conversion_works_as_expected) { + EXPECT_EQ(to_s(10ms), 0.010); + EXPECT_EQ(10ms, from_s(0.010)); +} + +TEST(TimeTest, unit_counting_works_as_expected) { + auto d = 3ms + 5us + 7ns; + EXPECT_EQ(count_ns(d), 3005007); + EXPECT_EQ(count_us(d), 3005); + EXPECT_EQ(count_ms(d), 3); +} + +GTEST_MAIN_RUN_ALL_TESTS() diff --git a/vespalib/src/vespa/vespalib/util/CMakeLists.txt b/vespalib/src/vespa/vespalib/util/CMakeLists.txt index c6fb5f572d1..da81556bb61 100644 --- a/vespalib/src/vespa/vespalib/util/CMakeLists.txt +++ b/vespalib/src/vespa/vespalib/util/CMakeLists.txt @@ -11,8 +11,8 @@ vespa_add_library(vespalib_vespalib_util OBJECT barrier.cpp benchmark_timer.cpp blockingthreadstackexecutor.cpp - bufferwriter.cpp box.cpp + bufferwriter.cpp classname.cpp closuretask.cpp compress.cpp @@ -52,6 +52,7 @@ vespa_add_library(vespalib_vespalib_util OBJECT thread_bundle.cpp threadstackexecutor.cpp threadstackexecutorbase.cpp + time.cpp valgrind.cpp zstdcompressor.cpp DEPENDS diff --git a/vespalib/src/vespa/vespalib/util/time.cpp b/vespalib/src/vespa/vespalib/util/time.cpp new file mode 100644 index 00000000000..d38e40a6d6a --- /dev/null +++ b/vespalib/src/vespa/vespalib/util/time.cpp @@ -0,0 +1,9 @@ +// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include "time.h" + +namespace vespalib { + +Timer::~Timer() = default; + +} diff --git a/vespalib/src/vespa/vespalib/util/time.h b/vespalib/src/vespa/vespalib/util/time.h new file mode 100644 index 00000000000..bab410f040d --- /dev/null +++ b/vespalib/src/vespa/vespalib/util/time.h @@ -0,0 +1,53 @@ +// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#pragma once + +#include <chrono> + +using namespace std::literals::chrono_literals; + +namespace vespalib { + +using steady_clock = std::chrono::steady_clock; +using steady_time = std::chrono::steady_clock::time_point; + +using system_clock = std::chrono::system_clock; +using system_time = std::chrono::system_clock::time_point; + +using duration = std::chrono::nanoseconds; + +constexpr double to_s(duration d) { + return std::chrono::duration_cast<std::chrono::duration<double>>(d).count(); +} + +constexpr duration from_s(double seconds) { + return std::chrono::duration_cast<duration>(std::chrono::duration<double>(seconds)); +} + +constexpr int64_t count_ms(duration d) { + return std::chrono::duration_cast<std::chrono::milliseconds>(d).count(); +} + +constexpr int64_t count_us(duration d) { + return std::chrono::duration_cast<std::chrono::microseconds>(d).count(); +} + +constexpr int64_t count_ns(duration d) { + return std::chrono::duration_cast<std::chrono::nanoseconds>(d).count(); +} + +/** + * Simple utility class used to measure how much time has elapsed + * since it was constructed. + **/ +class Timer +{ +private: + steady_time _start; +public: + Timer() : _start(steady_clock::now()) {} + ~Timer(); + duration elapsed() const { return (steady_clock::now() - _start); } +}; + +} |