1 #include "artdaq-core/Utilities/TimeUtils.hh"
3 #define BOOST_TEST_MODULE TimeUtils_t
5 #include "cetlib/quiet_unit_test.hpp"
6 #include "cetlib_except/exception.h"
8 #define TRACE_NAME "TimeUtils_t"
11 BOOST_AUTO_TEST_SUITE(TimeUtils_test)
13 BOOST_AUTO_TEST_CASE(GetElapsedTime)
15 auto then = std::chrono::steady_clock::now();
16 auto now = then + std::chrono::seconds(1);
22 auto start = std::chrono::steady_clock::now();
23 for (
int ii = 0; ii < 1000000; ++ii)
28 TLOG(TLVL_INFO) <<
"Time to call GetElapsedTime 1000000 times: " << dur <<
" s ( ave: " << dur / 1000000 <<
" s/call ).";
29 start = std::chrono::steady_clock::now();
30 for (
int ii = 0; ii < 1000000; ++ii)
35 TLOG(TLVL_INFO) <<
"Time to call GetElapsedTimeMilliseconds 1000000 times: " << dur <<
" s ( ave: " << dur / 1000000 <<
" s/call ).";
36 start = std::chrono::steady_clock::now();
37 for (
int ii = 0; ii < 1000000; ++ii)
42 TLOG(TLVL_INFO) <<
"Time to call GetElapsedTimeMicroseconds 1000000 times: " << dur <<
" s ( ave: " << dur / 1000000 <<
" s/call ).";
45 BOOST_AUTO_TEST_CASE(UnixTime)
49 gettimeofday(&tv,
nullptr);
53 TLOG(TLVL_INFO) <<
"time_t to string: " << timeString;
55 TLOG(TLVL_INFO) <<
"time_t to seconds: " << timeDouble;
58 TLOG(TLVL_INFO) <<
"timeval to string: " << valString;
60 TLOG(TLVL_INFO) <<
"timeval to seconds: " << valDouble;
63 TLOG(TLVL_INFO) <<
"timespec to string: " << specString;
65 TLOG(TLVL_INFO) <<
"timespec to seconds: " << specDouble;
67 BOOST_REQUIRE_EQUAL(timeDouble, std::floor(valDouble));
68 BOOST_REQUIRE_EQUAL(timeDouble, std::floor(specDouble));
71 BOOST_AUTO_TEST_CASE(GetTimeOfDayUS)
75 BOOST_REQUIRE_EQUAL(now / 1000000, ts.tv_sec);
78 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)