test_GaudiTimer.cpp 1.21 KB
Newer Older
Frank Winklmeier's avatar
Frank Winklmeier committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE test_GaudiTimer
#include <boost/test/unit_test.hpp>

#include <chrono>
#include <string>
#include <thread>

#include "Gaudi/Timers.h"

// Measure same time interval with different clocks
BOOST_AUTO_TEST_CASE( timer ) {
  Gaudi::Timer     timerA;
  Gaudi::FastTimer timerB;
  {
    auto t1 = timerA();
    auto t2 = timerB();
    std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
  }
  {
    auto t1 = timerA();
    auto t2 = timerB();
    std::this_thread::sleep_for( std::chrono::milliseconds( 20 ) );
  }

  std::cout << "timerA: " << timerA.stats() << std::endl;
  std::cout << "timerB: " << timerB.stats() << std::endl;
}

// Measure timer overhead
template <typename T>
void perfTimer( const std::string& label, int N ) {
  T            timerA;
  Gaudi::Timer timeit;
  {
    auto twall = timeit();
    for ( int i = 0; i < N; ++i ) { auto t = timerA(); }
  }
  std::cout << std::left << std::setw( 20 ) << label << timeit.stats().mean() << " us per " << N << " calls"
            << std::endl;
}

BOOST_AUTO_TEST_CASE( timer_overhead ) {
  const int N = 10000;
  perfTimer<Gaudi::Timer>( "Gaudi::Timer", N );
  perfTimer<Gaudi::FastTimer>( "Gaudi::FastTimer", N );
}