1 #include "artdaq-core/Utilities/TimeUtils.hh"
3 #define BOOST_TEST_MODULE TimeUtils_t
5 #include "cetlib/quiet_unit_test.hpp"
7 #define TRACE_NAME "TimeUtils_t"
8 #include "TRACE/tracemf.h"
10 BOOST_AUTO_TEST_SUITE(TimeUtils_test)
12 BOOST_AUTO_TEST_CASE(GetElapsedTime)
14 auto then = std::chrono::steady_clock::now();
15 auto now = then + std::chrono::seconds(1);
21 auto start = std::chrono::steady_clock::now();
22 for (
int ii = 0; ii < 1000000; ++ii)
27 TLOG(TLVL_INFO) <<
"Time to call GetElapsedTime 1000000 times: " << dur <<
" s ( ave: " << dur / 1000000 <<
" s/call ).";
28 start = std::chrono::steady_clock::now();
29 for (
int ii = 0; ii < 1000000; ++ii)
34 TLOG(TLVL_INFO) <<
"Time to call GetElapsedTimeMilliseconds 1000000 times: " << dur <<
" s ( ave: " << dur / 1000000 <<
" s/call ).";
35 start = std::chrono::steady_clock::now();
36 for (
int ii = 0; ii < 1000000; ++ii)
41 TLOG(TLVL_INFO) <<
"Time to call GetElapsedTimeMicroseconds 1000000 times: " << dur <<
" s ( ave: " << dur / 1000000 <<
" s/call ).";
44 BOOST_AUTO_TEST_CASE(UnixTime)
48 gettimeofday(&tv,
nullptr);
52 TLOG(TLVL_INFO) <<
"time_t to string: " << timeString;
54 TLOG(TLVL_INFO) <<
"time_t to seconds: " << timeDouble;
57 TLOG(TLVL_INFO) <<
"timeval to string: " << valString;
59 TLOG(TLVL_INFO) <<
"timeval to seconds: " << valDouble;
62 TLOG(TLVL_INFO) <<
"timespec to string: " << specString;
64 TLOG(TLVL_INFO) <<
"timespec to seconds: " << specDouble;
66 BOOST_REQUIRE_EQUAL(timeDouble, std::floor(valDouble));
67 BOOST_REQUIRE_EQUAL(timeDouble, std::floor(specDouble));
70 BOOST_AUTO_TEST_CASE(GetTimeOfDayUS)
74 BOOST_REQUIRE_EQUAL(now / 1000000, ts.tv_sec);
77 BOOST_AUTO_TEST_SUITE_END()
double GetElapsedTime(std::chrono::steady_clock::time_point then, std::chrono::steady_clock::time_point now=std::chrono::steady_clock::now())
Get the number of seconds in the given interval
size_t GetElapsedTimeMicroseconds(std::chrono::steady_clock::time_point then, std::chrono::steady_clock::time_point now=std::chrono::steady_clock::now())
Gets the number of microseconds in the given time interval
struct timespec get_realtime_clock()
Get the current time of day as a pair of seconds and nanoseconds (from clock_gettime(CLOCK_REALTIME, ...) system call)
double convertUnixTimeToSeconds(time_t inputUnixTime)
Converts a Unix time to double.
size_t GetElapsedTimeMilliseconds(std::chrono::steady_clock::time_point then, std::chrono::steady_clock::time_point now=std::chrono::steady_clock::now())
Gets the number of milliseconds in the given time interval
std::string convertUnixTimeToString(time_t inputUnixTime)
Converts a Unix time to its string representation, in UTC.
uint64_t gettimeofday_us()
Get the current time of day in microseconds (from gettimeofday system call)