artdaq_core  3.09.13
TimeUtils_t.cc
1 #include "artdaq-core/Utilities/TimeUtils.hh"
2 
3 #define BOOST_TEST_MODULE TimeUtils_t
4 #include <cmath>
5 #include "cetlib/quiet_unit_test.hpp"
6 
7 #define TRACE_NAME "TimeUtils_t"
8 #include "TRACE/tracemf.h"
9 
10 BOOST_AUTO_TEST_SUITE(TimeUtils_test)
11 
12 BOOST_AUTO_TEST_CASE(GetElapsedTime)
13 {
14  auto then = std::chrono::steady_clock::now();
15  auto now = then + std::chrono::seconds(1);
16 
17  BOOST_REQUIRE_EQUAL(artdaq::TimeUtils::GetElapsedTime(then, now), 1);
18  BOOST_REQUIRE_EQUAL(artdaq::TimeUtils::GetElapsedTimeMilliseconds(then, now), 1000);
19  BOOST_REQUIRE_EQUAL(artdaq::TimeUtils::GetElapsedTimeMicroseconds(then, now), 1000000);
20 
21  auto start = std::chrono::steady_clock::now();
22  for (int ii = 0; ii < 1000000; ++ii)
23  {
25  }
26  auto dur = artdaq::TimeUtils::GetElapsedTime(start);
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)
30  {
32  }
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)
37  {
39  }
41  TLOG(TLVL_INFO) << "Time to call GetElapsedTimeMicroseconds 1000000 times: " << dur << " s ( ave: " << dur / 1000000 << " s/call ).";
42 }
43 
44 BOOST_AUTO_TEST_CASE(UnixTime)
45 {
46  time_t t = time(0);
47  struct timeval tv;
48  gettimeofday(&tv, nullptr);
49  struct timespec ts = artdaq::TimeUtils::get_realtime_clock();
50 
51  auto timeString = artdaq::TimeUtils::convertUnixTimeToString(t);
52  TLOG(TLVL_INFO) << "time_t to string: " << timeString;
54  TLOG(TLVL_INFO) << "time_t to seconds: " << timeDouble;
55 
56  auto valString = artdaq::TimeUtils::convertUnixTimeToString(tv);
57  TLOG(TLVL_INFO) << "timeval to string: " << valString;
59  TLOG(TLVL_INFO) << "timeval to seconds: " << valDouble;
60 
61  auto specString = artdaq::TimeUtils::convertUnixTimeToString(ts);
62  TLOG(TLVL_INFO) << "timespec to string: " << specString;
63  auto specDouble = artdaq::TimeUtils::convertUnixTimeToSeconds(ts);
64  TLOG(TLVL_INFO) << "timespec to seconds: " << specDouble;
65 
66  BOOST_REQUIRE_EQUAL(timeDouble, std::floor(valDouble));
67  BOOST_REQUIRE_EQUAL(timeDouble, std::floor(specDouble));
68 }
69 
70 BOOST_AUTO_TEST_CASE(GetTimeOfDayUS)
71 {
73  struct timespec ts = artdaq::TimeUtils::get_realtime_clock();
74  BOOST_REQUIRE_EQUAL(now / 1000000, ts.tv_sec);
75 }
76 
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
Definition: TimeUtils.hh:30
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
Definition: TimeUtils.hh:41
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)
Definition: TimeUtils.cc:58
double convertUnixTimeToSeconds(time_t inputUnixTime)
Converts a Unix time to double.
Definition: TimeUtils.cc:66
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
Definition: TimeUtils.hh:52
std::string convertUnixTimeToString(time_t inputUnixTime)
Converts a Unix time to its string representation, in UTC.
Definition: TimeUtils.cc:7
uint64_t gettimeofday_us()
Get the current time of day in microseconds (from gettimeofday system call)
Definition: TimeUtils.cc:51