1 //=============================================================================
3 * @file NDDS_Timer_Test.cpp
5 * Simple test of NDDS_Timer. Copied from NDDS_Timer_Test.
7 * @author Marcel Smit <msmit@remedy.nl>
9 //=============================================================================
12 #include "test_config.h"
13 #include "ace/Log_Msg.h"
14 #include "ace/Sched_Params.h"
15 #include "ace/Get_Opt.h"
16 #include "ace/OS_NS_unistd.h"
18 #include <ndds/ndds_cpp.h>
19 #include <ndds/clock/clock_highResolution.h>
23 check (const time_t interval
, const time_t measured
)
25 time_t const threshold
= 25 /* percent */;
27 time_t const difference
=
28 interval
> measured
? interval
- measured
: measured
- interval
;
30 time_t const percentage_difference
= difference
* 100 / interval
;
32 if (percentage_difference
< threshold
)
35 ACE_ERROR_RETURN ((LM_ERROR
,
36 ACE_TEXT ("The measured time of %Q differs from ")
37 ACE_TEXT ("the interval of %Q by %Q percent.\n"),
40 percentage_difference
),
44 // Does a sanity check of the microseconds vs nanoseconds. They're supposed
45 // to represent the same interval.
47 check_micro_nano (unsigned long long microinterval
, unsigned long long nanointerval
)
49 unsigned long long const threshold
= 8 /* promille */;
51 microinterval
*= 1000u;
52 unsigned long long const difference
= (microinterval
> nanointerval
53 ? (microinterval
- nanointerval
)
54 : (nanointerval
- microinterval
));
55 if (nanointerval
== 0)
57 nanointerval
= 1; // Prevent divide-by-zero
59 unsigned long long const promille_difference
=
60 difference
* 1000 / nanointerval
;
62 if ((promille_difference
< threshold
) || (difference
< 1500))
67 ACE_ERROR_RETURN ((LM_ERROR
,
68 ACE_TEXT ("The microseconds * 1000 of %Q ")
69 ACE_TEXT ("differs from the nanoseconds of %Q ")
70 ACE_TEXT (" by %Q promille\n"),
79 time_interval (RTIClock
*timer
,
80 const ACE_Time_Value
& interval
,
83 struct RTINtpTime start_time
;
84 struct RTINtpTime finish_time
;
86 RTINtpTime_setZero(&start_time
);
87 RTINtpTime_setZero(&finish_time
);
93 timer
->getTime(timer
, &start_time
);
94 ACE_OS::sleep (interval
);
95 timer
->getTime(timer
, &finish_time
);
98 RTINtpTime_subtract(duration
, finish_time
, start_time
);
102 static u_int
const intervals
[] =
103 {0, 1, 10, 100, 1000, 10000, 100000, 1000000, 4000000}; /*usec*/
106 run_main (int argc
, ACE_TCHAR
*argv
[])
108 ACE_START_TEST (ACE_TEXT ("NDDS_Timer_Test"));
110 RTIClock
*timer
= RTIHighResolutionClock_new();
114 u_int iterations
= 1;
116 ACE_Get_Opt
getoptarg (argc
, argv
, ACE_TEXT ("i:"));
117 for (int c
; (c
= getoptarg ()) != -1; )
122 iterations
= ACE_OS::atoi (getoptarg
.opt_arg ());
127 // We don't check for errors if the interval is shorter than this
128 // value because the OS has a finite resolution anyway.
129 static u_int
const TIMER_RESOLUTION
= 10000;
131 for (u_int i
= 0; i
< sizeof intervals
/ sizeof (u_int
); ++i
)
133 for (u_int j
= 0; j
< iterations
; ++j
)
135 ACE_Time_Value
const interval (0, intervals
[i
]);
138 time_interval (timer
, interval
, duration
);
140 unsigned long long microseconds
, nanoseconds
;
142 RTINtpTime_unpackToMicrosec (sec
, microseconds
, duration
);
143 RTINtpTime_unpackToNanosec (sec
, nanoseconds
, duration
);
145 time_t const interval_usec
=
146 interval
.sec () * ACE_ONE_SECOND_IN_USECS
+ interval
.usec ();
147 time_t const measured_usec
=
148 sec
* ACE_ONE_SECOND_IN_USECS
+ microseconds
;
151 ACE_DEBUG ((LM_DEBUG
,
152 ACE_TEXT ("interval: %: usec, measured: %: usec %s\n"),
155 (intervals
[i
] <= TIMER_RESOLUTION
156 ? ACE_TEXT (" (interval and measured may differ)")
159 if (intervals
[i
] > TIMER_RESOLUTION
)
161 errors
+= check (interval
.sec () * ACE_ONE_SECOND_IN_USECS
163 sec
* ACE_ONE_SECOND_IN_USECS
+ microseconds
);
164 // Don't check for error for intervals below 10 msec.
166 errors
+= check_micro_nano (microseconds
, nanoseconds
);
167 ACE_DEBUG ((LM_DEBUG
,
168 ACE_TEXT ("NDDS_Timer usec: %Q, nsec: %Q\n"),
175 return errors
== 0 ? 0 : 1;