Classes | Macros | Functions | Variables
cpu_timer_test.cc File Reference
#include "boost/test/unit_test.hpp"
#include "cetlib/cpu_timer.h"
#include <chrono>
#include <cmath>
#include <iostream>
#include <thread>
#include <sys/resource.h>

Go to the source code of this file.

Classes

struct  CPUTimerTtestFixture
 

Macros

#define _GLIBCXX_USE_NANOSLEEP   1
 
#define BOOST_TEST_MODULE   (cpu_timer_test 2)
 

Functions

double time_diff (rusage const &a, rusage const &b)
 
double busy_loop (double dur)
 
 BOOST_AUTO_TEST_CASE (init)
 
 BOOST_AUTO_TEST_CASE (timer1)
 
 BOOST_AUTO_TEST_CASE (timer2)
 
 BOOST_AUTO_TEST_CASE (nullStart)
 
 BOOST_AUTO_TEST_CASE (doubleStop)
 
 BOOST_AUTO_TEST_CASE (reset)
 
 BOOST_AUTO_TEST_CASE (checkUsage)
 

Variables

double const small_cputime = 1.5e-3
 
double const small_realtime = 1.5e-3
 

Macro Definition Documentation

#define _GLIBCXX_USE_NANOSLEEP   1

Definition at line 1 of file cpu_timer_test.cc.

#define BOOST_TEST_MODULE   (cpu_timer_test 2)

Definition at line 2 of file cpu_timer_test.cc.

Function Documentation

BOOST_AUTO_TEST_CASE ( init  )

Definition at line 66 of file cpu_timer_test.cc.

67 {
68  // A newly-constructed timer should have both realTime and cpuTime of
69  // zero.
70  BOOST_TEST(timer().realTime() == 0.0);
71  BOOST_TEST(timer().cpuTime() == 0.0);
72 }
BOOST_AUTO_TEST_CASE ( timer1  )

Definition at line 74 of file cpu_timer_test.cc.

75 {
76  // Run the timer while we sleep, then stop. This should use clock
77  // time, but little CPU time.
78  timer().stop();
79  timer().reset();
80  timer().start();
81  std::this_thread::sleep_for(std::chrono::milliseconds(50));
82  timer().stop();
83 
84  BOOST_TEST(timer().realTime() >= 0.050);
85  std::cout << "timer1 cpu: " << timer().cpuTime()
86  << " real: " << timer().realTime() << std::endl;
87  BOOST_CHECK_SMALL(timer().cpuTime(), small_cputime);
88 }
millisecond milliseconds
Alias for common language habits.
Definition: spacetime.h:105
double const small_cputime
QTextStream & endl(QTextStream &s)
BOOST_AUTO_TEST_CASE ( timer2  )

Definition at line 90 of file cpu_timer_test.cc.

91 {
92  // Run the time while we sleep. Capture the time without stopping the
93  // timer. This should use clock time but little CPU time.
94  timer().stop();
95  timer().reset();
96  timer().start();
97  std::this_thread::sleep_for(std::chrono::milliseconds(50));
98  // We have to capture the times before we do comparisons, or else
99  // we'll have used too much CPU time.
100  double const cpu = timer().cpuTime();
101  double const real = timer().realTime();
102 
103  BOOST_TEST(real >= 0.050);
104  BOOST_CHECK_SMALL(cpu, small_cputime);
105 }
millisecond milliseconds
Alias for common language habits.
Definition: spacetime.h:105
double const small_cputime
BOOST_AUTO_TEST_CASE ( nullStart  )

Definition at line 107 of file cpu_timer_test.cc.

108 {
109  // Get the time immediately after starting. This should give close to
110  // zero for both clock and CPU time.
111  timer().stop();
112  timer().reset();
113  timer().start();
114 
115  // Test
116  BOOST_CHECK_SMALL(timer().realTime(), small_realtime);
117  BOOST_CHECK_SMALL(timer().cpuTime(), small_cputime);
118 }
double const small_realtime
double const small_cputime
BOOST_AUTO_TEST_CASE ( doubleStop  )

Definition at line 120 of file cpu_timer_test.cc.

121 {
122  // Make sure the time between two stop() calls without an intervening
123  // start() or reset() does not change.
124  timer().stop();
125  timer().reset();
126  timer().start();
127  double const dur = 0.150; // seconds
128  std::cout << busy_loop(dur) << "\n";
129  timer().stop();
130  double real = timer().realTime();
131  double cpu = timer().cpuTime();
132  timer().stop();
133 
134  BOOST_TEST(timer().realTime() == real);
135  BOOST_TEST(timer().cpuTime() == cpu);
136 }
double busy_loop(double dur)
BOOST_AUTO_TEST_CASE ( reset  )

Definition at line 138 of file cpu_timer_test.cc.

139 {
140  // Make sure reset() zeros both real and CPU time.
141  timer().start();
142  double const dur = 0.150;
143  std::cout << busy_loop(dur) << "\n";
144  timer().stop();
145  BOOST_TEST(timer().realTime() > 0.0);
146  BOOST_TEST(timer().cpuTime() > 0.0);
147 
148  timer().reset();
149  BOOST_TEST(timer().realTime() == 0.0);
150  BOOST_TEST(timer().cpuTime() == 0.0);
151 }
double busy_loop(double dur)
BOOST_AUTO_TEST_CASE ( checkUsage  )

Definition at line 153 of file cpu_timer_test.cc.

154 {
155  timer().stop();
156  timer().reset();
157  timer().start();
158 
159  double const dur = 0.135;
160  std::cout << busy_loop(dur) << "\n";
161  timer().stop();
162 
163  BOOST_TEST(timer().realTime() > 0.0);
164  BOOST_TEST(timer().cpuTime() > 0.0);
165 }
double busy_loop(double dur)
double busy_loop ( double  dur)

Definition at line 49 of file cpu_timer_test.cc.

50 {
51  double x = 3.14;
52  rusage start_time, ru;
53  getrusage(RUSAGE_SELF, &start_time);
54  do {
55  for (int i = 0; i < 1000; ++i)
56  x = sin(x);
57  getrusage(RUSAGE_SELF, &ru);
58  } while (time_diff(ru, start_time) < dur);
59  return x;
60 }
double time_diff(rusage const &a, rusage const &b)
list x
Definition: train.py:276
double time_diff ( rusage const &  a,
rusage const &  b 
)
inline

Definition at line 38 of file cpu_timer_test.cc.

39 {
40  double const sec = (a.ru_utime.tv_sec - b.ru_utime.tv_sec) +
41  (a.ru_stime.tv_sec - b.ru_stime.tv_sec);
42  double const microsec = (a.ru_utime.tv_usec - b.ru_utime.tv_usec) +
43  (a.ru_stime.tv_usec - b.ru_stime.tv_usec);
44  return sec + 1e-6 * microsec;
45 }
const double e
const double a
static bool * b
Definition: config.cpp:1043

Variable Documentation

double const small_cputime = 1.5e-3

Definition at line 29 of file cpu_timer_test.cc.

double const small_realtime = 1.5e-3

Definition at line 33 of file cpu_timer_test.cc.