LCOV - code coverage report
Current view: top level - /opt/artdaq/srcs/artdaq-core/test/Utilities - TimeUtils_t.cc (source / functions) Coverage Total Hit
Test: artdaq.info.cleaned Lines: 100.0 % 46 46
Test Date: 2025-09-04 00:45:34 Functions: 100.0 % 15 15

            Line data    Source code
       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            2 : BOOST_AUTO_TEST_CASE(GetElapsedTime)
      13              : {
      14            1 :         auto then = std::chrono::steady_clock::now();
      15            1 :         auto now = then + std::chrono::seconds(1);
      16              : 
      17            1 :         BOOST_REQUIRE_EQUAL(artdaq::TimeUtils::GetElapsedTime(then, now), 1);
      18            1 :         BOOST_REQUIRE_EQUAL(artdaq::TimeUtils::GetElapsedTimeMilliseconds(then, now), 1000);
      19            1 :         BOOST_REQUIRE_EQUAL(artdaq::TimeUtils::GetElapsedTimeMicroseconds(then, now), 1000000);
      20              : 
      21            1 :         auto start = std::chrono::steady_clock::now();
      22      1000001 :         for (int ii = 0; ii < 1000000; ++ii)
      23              :         {
      24      1000000 :                 artdaq::TimeUtils::GetElapsedTime(start);
      25              :         }
      26            1 :         auto dur = artdaq::TimeUtils::GetElapsedTime(start);
      27            3 :         TLOG(TLVL_INFO) << "Time to call GetElapsedTime 1000000 times: " << dur << " s ( ave: " << dur / 1000000 << " s/call ).";
      28            1 :         start = std::chrono::steady_clock::now();
      29      1000001 :         for (int ii = 0; ii < 1000000; ++ii)
      30              :         {
      31      1000000 :                 artdaq::TimeUtils::GetElapsedTimeMilliseconds(start);
      32              :         }
      33            1 :         dur = artdaq::TimeUtils::GetElapsedTime(start);
      34            3 :         TLOG(TLVL_INFO) << "Time to call GetElapsedTimeMilliseconds 1000000 times: " << dur << " s ( ave: " << dur / 1000000 << " s/call ).";
      35            1 :         start = std::chrono::steady_clock::now();
      36      1000001 :         for (int ii = 0; ii < 1000000; ++ii)
      37              :         {
      38      1000000 :                 artdaq::TimeUtils::GetElapsedTimeMicroseconds(start);
      39              :         }
      40            1 :         dur = artdaq::TimeUtils::GetElapsedTime(start);
      41            3 :         TLOG(TLVL_INFO) << "Time to call GetElapsedTimeMicroseconds 1000000 times: " << dur << " s ( ave: " << dur / 1000000 << " s/call ).";
      42            1 : }
      43              : 
      44            2 : BOOST_AUTO_TEST_CASE(UnixTime)
      45              : {
      46            1 :         time_t t = time(0);
      47              :         struct timeval tv;
      48            1 :         gettimeofday(&tv, nullptr);
      49            1 :         struct timespec ts = artdaq::TimeUtils::get_realtime_clock();
      50              : 
      51            1 :         auto timeString = artdaq::TimeUtils::convertUnixTimeToString(t);
      52            3 :         TLOG(TLVL_INFO) << "time_t to string: " << timeString;
      53            1 :         auto timeDouble = artdaq::TimeUtils::convertUnixTimeToSeconds(t);
      54            3 :         TLOG(TLVL_INFO) << "time_t to seconds: " << timeDouble;
      55              : 
      56            1 :         auto valString = artdaq::TimeUtils::convertUnixTimeToString(tv);
      57            3 :         TLOG(TLVL_INFO) << "timeval to string: " << valString;
      58            1 :         auto valDouble = artdaq::TimeUtils::convertUnixTimeToSeconds(tv);
      59            3 :         TLOG(TLVL_INFO) << "timeval to seconds: " << valDouble;
      60              : 
      61            1 :         auto specString = artdaq::TimeUtils::convertUnixTimeToString(ts);
      62            3 :         TLOG(TLVL_INFO) << "timespec to string: " << specString;
      63            1 :         auto specDouble = artdaq::TimeUtils::convertUnixTimeToSeconds(ts);
      64            3 :         TLOG(TLVL_INFO) << "timespec to seconds: " << specDouble;
      65              : 
      66            1 :         BOOST_REQUIRE_EQUAL(timeDouble, std::floor(valDouble));
      67            1 :         BOOST_REQUIRE_EQUAL(timeDouble, std::floor(specDouble));
      68            1 : }
      69              : 
      70            2 : BOOST_AUTO_TEST_CASE(GetTimeOfDayUS)
      71              : {
      72            1 :         auto now = artdaq::TimeUtils::gettimeofday_us();
      73            1 :         struct timespec ts = artdaq::TimeUtils::get_realtime_clock();
      74            1 :         BOOST_REQUIRE_EQUAL(now / 1000000, ts.tv_sec);
      75            1 : }
      76              : 
      77              : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1