1 //=============================================================================
3 * @file High_Res_Timer_Test.cpp
5 * Simple test of ACE_High_Res_Timer.
7 * @author David L. Levine <levine@cs.wustl.edu>
9 //=============================================================================
11 #include "test_config.h"
12 #include "ace/Log_Msg.h"
13 #include "ace/High_Res_Timer.h"
14 #include "ace/Sched_Params.h"
15 #include "ace/Get_Opt.h"
16 #include "ace/OS_NS_unistd.h"
20 check (const time_t interval
, const time_t measured
)
22 time_t const threshold
= 25 /* percent */;
24 time_t const difference
=
25 interval
> measured
? interval
- measured
: measured
- interval
;
27 time_t const percentage_difference
= difference
* 100 / interval
;
29 if (percentage_difference
< threshold
)
32 ACE_ERROR_RETURN ((LM_ERROR
,
33 ACE_TEXT ("The measured time of %Q differs from ")
34 ACE_TEXT ("the interval of %Q by %Q percent.\n"),
37 percentage_difference
),
41 // Does a sanity check of the microseconds vs nanoseconds. They're supposed
42 // to represent the same interval.
44 check_micro_nano (ACE_hrtime_t microinterval
, ACE_hrtime_t nanointerval
)
46 ACE_hrtime_t
const threshold
= 8 /* promille */;
48 microinterval
*= 1000u;
49 ACE_hrtime_t
const difference
= (microinterval
> nanointerval
50 ? (microinterval
- nanointerval
)
51 : (nanointerval
- microinterval
));
52 if (nanointerval
== 0)
54 nanointerval
= 1; // Prevent divide-by-zero
56 ACE_hrtime_t
const promille_difference
=
57 difference
* 1000 / nanointerval
;
59 if ((promille_difference
< threshold
) || (difference
< 1500))
64 ACE_ERROR_RETURN ((LM_ERROR
,
65 ACE_TEXT ("The microseconds * 1000 of %Q ")
66 ACE_TEXT ("differs from the nanoseconds of %Q ")
67 ACE_TEXT (" by %Q promille\n"),
76 time_interval (const ACE_Time_Value
&interval
,
77 ACE_hrtime_t
& nanoseconds
,
78 ACE_hrtime_t
& microseconds
)
80 ACE_High_Res_Timer timer
;
83 ACE_OS::sleep (interval
);
86 ACE_Time_Value measured
;
87 timer
.elapsed_time (measured
);
88 timer
.elapsed_time (nanoseconds
);
89 timer
.elapsed_microseconds (microseconds
);
94 static u_int
const intervals
[] =
95 {0, 1, 10, 100, 1000, 10000, 100000, 1000000, 4000000}; /*usec*/
98 run_main (int argc
, ACE_TCHAR
*argv
[])
100 ACE_START_TEST (ACE_TEXT ("High_Res_Timer_Test"));
102 ACE_DEBUG ((LM_DEBUG
,
103 ACE_TEXT ("The ACE_High_Res_Timer scale factor is %u ")
104 ACE_TEXT ("1/microsecond\n"),
105 ACE_High_Res_Timer::global_scale_factor ()));
109 u_int iterations
= 1;
111 ACE_Get_Opt
getoptarg (argc
, argv
, ACE_TEXT ("i:"));
112 for (int c
; (c
= getoptarg ()) != -1; )
117 iterations
= ACE_OS::atoi (getoptarg
.opt_arg ());
122 // We don't check for errors if the interval is shorter than this
123 // value because the OS has a finite resolution anyway.
124 static u_int
const TIMER_RESOLUTION
= 10000;
126 for (u_int i
= 0; i
< sizeof intervals
/ sizeof (u_int
); ++i
)
128 for (u_int j
= 0; j
< iterations
; ++j
)
130 ACE_Time_Value
const interval (0, intervals
[i
]);
131 ACE_hrtime_t nanoseconds
;
132 ACE_hrtime_t microseconds
;
133 ACE_Time_Value
const measured
= time_interval (interval
,
136 time_t const interval_usec
=
137 interval
.sec () * ACE_ONE_SECOND_IN_USECS
+ interval
.usec ();
138 time_t const measured_usec
=
139 measured
.sec () * ACE_ONE_SECOND_IN_USECS
+ measured
.usec ();
141 ACE_DEBUG ((LM_DEBUG
,
142 ACE_TEXT ("interval: %: usec, measured: %: usec %s\n"),
145 (intervals
[i
] <= TIMER_RESOLUTION
146 ? ACE_TEXT (" (interval and measured may differ)")
149 if (intervals
[i
] > TIMER_RESOLUTION
)
151 errors
+= check (interval
.sec () * ACE_ONE_SECOND_IN_USECS
153 measured
.sec () * ACE_ONE_SECOND_IN_USECS
155 // Don't check for error for intervals below 10 msec.
157 // Check the ACE_Timer_Value-calculated microseconds against
158 // the ACE_High_Res_Timer-calculated nanoseconds.
159 ACE_DEBUG ((LM_DEBUG
,
160 ACE_TEXT ("ACE_Time_Value usec: %:, ACE_HR nsec: %Q\n"),
163 // This gives problems -> should be fixed
164 errors
+= check_micro_nano (measured
.sec () * ACE_ONE_SECOND_IN_USECS
167 ACE_DEBUG ((LM_DEBUG
,
168 ACE_TEXT ("ACE_High_Res_Timer usec: %Q, nsec: %Q\n"),
171 // Now check the ACE_High_Res_Timer-calculated values against
173 errors
+= check_micro_nano (microseconds
, nanoseconds
);
178 return errors
== 0 ? 0 : 1;