Skip to content

Commit 120a12a

Browse files
committed
util: Add type safe GetTime
1 parent be48766 commit 120a12a

File tree

5 files changed

+93
-10
lines changed

5 files changed

+93
-10
lines changed

src/Makefile.bench.include

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ bench_bench_pivx_SOURCES = \
2020
bench/lockedpool.cpp \
2121
bench/perf.cpp \
2222
bench/perf.h \
23-
bench/prevector.cpp
23+
bench/prevector.cpp \
24+
bench/util_time.cpp
2425

2526
nodist_bench_bench_pivx_SOURCES = $(GENERATED_TEST_FILES)
2627

src/bench/util_time.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) 2019 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include "bench/bench.h"
6+
7+
#include "utiltime.h"
8+
9+
static void BenchTimeDeprecated(benchmark::State& state)
10+
{
11+
while (state.KeepRunning()) {
12+
(void)GetTime();
13+
}
14+
}
15+
16+
static void BenchTimeMock(benchmark::State& state)
17+
{
18+
SetMockTime(111);
19+
while (state.KeepRunning()) {
20+
(void)GetTime<std::chrono::seconds>();
21+
}
22+
SetMockTime(0);
23+
}
24+
25+
static void BenchTimeMillis(benchmark::State& state)
26+
{
27+
while (state.KeepRunning()) {
28+
(void)GetTime<std::chrono::milliseconds>();
29+
}
30+
}
31+
32+
static void BenchTimeMillisSys(benchmark::State& state)
33+
{
34+
while (state.KeepRunning()) {
35+
(void)GetTimeMillis();
36+
}
37+
}
38+
39+
BENCHMARK(BenchTimeDeprecated/*, 100000000*/);
40+
BENCHMARK(BenchTimeMillis/*, 6000000*/);
41+
BENCHMARK(BenchTimeMillisSys/*, 6000000*/);
42+
BENCHMARK(BenchTimeMock/*, 300000000*/);

src/test/util_tests.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,27 @@ BOOST_AUTO_TEST_CASE(gettime)
697697
BOOST_CHECK((GetTime() & ~0xFFFFFFFFLL) == 0);
698698
}
699699

700+
BOOST_AUTO_TEST_CASE(util_time_GetTime)
701+
{
702+
SetMockTime(111);
703+
// Check that mock time does not change after a sleep
704+
for (const auto& num_sleep : {0, 1}) {
705+
MilliSleep(num_sleep);
706+
BOOST_CHECK_EQUAL(111, GetTime()); // Deprecated time getter
707+
BOOST_CHECK_EQUAL(111, GetTime<std::chrono::seconds>().count());
708+
BOOST_CHECK_EQUAL(111000, GetTime<std::chrono::milliseconds>().count());
709+
BOOST_CHECK_EQUAL(111000000, GetTime<std::chrono::microseconds>().count());
710+
}
711+
712+
SetMockTime(0);
713+
// Check that system time changes after a sleep
714+
const auto ms_0 = GetTime<std::chrono::milliseconds>();
715+
const auto us_0 = GetTime<std::chrono::microseconds>();
716+
MilliSleep(1);
717+
BOOST_CHECK(ms_0 < GetTime<std::chrono::milliseconds>());
718+
BOOST_CHECK(us_0 < GetTime<std::chrono::microseconds>());
719+
}
720+
700721
BOOST_AUTO_TEST_CASE(test_IsDigit)
701722
{
702723
BOOST_CHECK_EQUAL(IsDigit('0'), true);

src/utiltime.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ int64_t GetTime()
3131
return now;
3232
}
3333

34+
template <typename T>
35+
T GetTime()
36+
{
37+
const std::chrono::seconds mocktime{nMockTime.load(std::memory_order_relaxed)};
38+
39+
return std::chrono::duration_cast<T>(
40+
mocktime.count() ?
41+
mocktime :
42+
std::chrono::microseconds{GetTimeMicros()});
43+
}
44+
template std::chrono::seconds GetTime();
45+
template std::chrono::milliseconds GetTime();
46+
template std::chrono::microseconds GetTime();
47+
3448
void SetMockTime(int64_t nMockTimeIn)
3549
{
3650
nMockTime.store(nMockTimeIn, std::memory_order_relaxed);

src/utiltime.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) 2009-2010 Satoshi Nakamoto
22
// Copyright (c) 2009-2014 The Bitcoin developers
3-
// Copyright (c) 2016-2017 The PIVX developers
3+
// Copyright (c) 2016-2019 The PIVX developers
44
// Distributed under the MIT software license, see the accompanying
55
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
66

@@ -9,27 +9,32 @@
99

1010
#include <stdint.h>
1111
#include <string>
12+
#include <chrono>
1213

1314
/**
14-
* GetTimeMicros() and GetTimeMillis() both return the system time, but in
15-
* different units. GetTime() returns the sytem time in seconds, but also
16-
* supports mocktime, where the time can be specified by the user, eg for
17-
* testing (eg with the setmocktime rpc, or -mocktime argument).
18-
*
19-
* TODO: Rework these functions to be type-safe (so that we don't inadvertently
20-
* compare numbers with different units, or compare a mocktime to system time).
15+
* DEPRECATED
16+
* Use either GetSystemTimeInSeconds (not mockable) or GetTime<T> (mockable)
2117
*/
22-
2318
int64_t GetTime();
19+
/** Returns the system time (not mockable) */
2420
int64_t GetTimeMillis();
21+
/** Returns the system time (not mockable) */
2522
int64_t GetTimeMicros();
23+
/** Returns the system time (not mockable) */
2624
int64_t GetSystemTimeInSeconds(); // Like GetTime(), but not mockable
25+
/** For testing. Set e.g. with the setmocktime rpc, or -mocktime argument */
2726
void SetMockTime(int64_t nMockTimeIn);
27+
/** For testing */
2828
int64_t GetMockTime();
29+
2930
void MilliSleep(int64_t n);
3031

3132
std::string DurationToDHMS(int64_t nDurationTime);
3233

34+
/** Return system time (or mocked time, if set) */
35+
template <typename T>
36+
T GetTime();
37+
3338
/**
3439
* ISO 8601 formatting is preferred. Use the FormatISO8601{DateTime,Date,Time}
3540
* helper functions if possible.

0 commit comments

Comments
 (0)