Main Page
Related Pages
Modules
Namespaces
Classes
Files
Examples
File List
File Members
cetlib
cetlib
test
cpu_timer_test.cc
Go to the documentation of this file.
1
#define _GLIBCXX_USE_NANOSLEEP 1
2
#define BOOST_TEST_MODULE (cpu_timer_test 2)
3
#include "boost/test/unit_test.hpp"
4
5
#include "
cetlib/cpu_timer.h
"
6
7
#include <chrono>
8
#include <cmath>
9
#include <iostream>
10
#include <thread>
11
12
#include <sys/resource.h>
13
14
struct
CPUTimerTtestFixture
{
15
cet::cpu_timer
&
16
timer
()
17
{
18
static
cet::cpu_timer
t_s;
19
return
t_s;
20
}
21
};
22
23
//----------------------------------------------------------------------
24
// Constants used for controlling the tests of the CPUTimer.
25
//
26
27
// The smallest amount of CPU time we can reliably measure, from two
28
// consecutive calls to the timer. Time in seconds.
29
double
const
small_cputime
= 1.5e-3;
30
31
// The smallest amount of real (wallclock) time we can reliably measure,
32
// from two consecutive calls to the timer. Time in seconds.
33
double
const
small_realtime
= 1.5e-3;
34
35
// Return the difference between the total time (user+system) in the two
36
// rusage structs.
37
inline
double
38
time_diff
(rusage
const
&
a
, rusage
const
&
b
)
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 + 1
e
-6 * microsec;
45
}
46
47
// Make a busy-loop for 'dur' seconds.
48
double
49
busy_loop
(
double
dur)
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
}
61
62
//----------------------------------------------------------------------
63
64
BOOST_FIXTURE_TEST_SUITE(CPUTimer_t,
CPUTimerTtestFixture
)
65
66
BOOST_AUTO_TEST_CASE
(
init
)
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
}
73
74
BOOST_AUTO_TEST_CASE
(timer1)
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
}
89
90
BOOST_AUTO_TEST_CASE
(timer2)
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
}
106
107
BOOST_AUTO_TEST_CASE
(nullStart)
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
}
119
120
BOOST_AUTO_TEST_CASE
(doubleStop)
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
}
137
138
BOOST_AUTO_TEST_CASE
(
reset
)
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
}
152
153
BOOST_AUTO_TEST_CASE
(checkUsage)
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
}
166
167
BOOST_AUTO_TEST_SUITE_END()
cet::cpu_timer::reset
void reset()
Definition:
cpu_timer.cc:103
small_realtime
double const small_realtime
Definition:
cpu_timer_test.cc:33
cet::cpu_timer
Definition:
cpu_timer.h:18
train.init
init
Definition:
train.py:42
time_diff
double time_diff(rusage const &a, rusage const &b)
Definition:
cpu_timer_test.cc:38
busy_loop
double busy_loop(double dur)
Definition:
cpu_timer_test.cc:49
reset
QTextStream & reset(QTextStream &s)
Definition:
qtextstream.cpp:2048
e
const double e
Definition:
gUpMuFluxGen.cxx:165
a
const double a
Definition:
gUpMuFluxGen.cxx:164
cet::cpu_timer::realTime
double realTime() const
Definition:
cpu_timer.h:39
cpu_timer.h
util::quantities::milliseconds
millisecond milliseconds
Alias for common language habits.
Definition:
spacetime.h:105
cet::cpu_timer::stop
void stop()
Definition:
cpu_timer.cc:93
small_cputime
double const small_cputime
Definition:
cpu_timer_test.cc:29
b
static bool * b
Definition:
config.cpp:1043
CPUTimerTtestFixture
Definition:
cpu_timer_test.cc:14
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(init)
Definition:
cpu_timer_test.cc:66
train.x
list x
Definition:
train.py:276
CPUTimerTtestFixture::timer
cet::cpu_timer & timer()
Definition:
cpu_timer_test.cc:16
cet::cpu_timer::start
void start()
Definition:
cpu_timer.cc:83
cet::cpu_timer::cpuTime
double cpuTime() const
Definition:
cpu_timer.h:44
endl
QTextStream & endl(QTextStream &s)
Definition:
qtextstream.cpp:2030
Generated by
1.8.11