Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / tests / High_Res_Timer_Test.cpp
blob4c90d8197bd56a43db75c7fb765ecd4feaeb4850
1 //=============================================================================
2 /**
3 * @file High_Res_Timer_Test.cpp
5 * Simple test of ACE_High_Res_Timer.
7 * @author David L. Levine <levine@cs.wustl.edu>
8 */
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"
18 static
19 u_int
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)
30 return 0;
31 else
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"),
35 measured,
36 interval,
37 percentage_difference),
38 1);
41 // Does a sanity check of the microseconds vs nanoseconds. They're supposed
42 // to represent the same interval.
43 static u_int
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))
61 return 0;
63 else
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"),
68 microinterval,
69 nanointerval,
70 promille_difference),
71 1);
74 static
75 ACE_Time_Value
76 time_interval (const ACE_Time_Value &interval,
77 ACE_hrtime_t& nanoseconds,
78 ACE_hrtime_t& microseconds)
80 ACE_High_Res_Timer timer;
82 timer.start ();
83 ACE_OS::sleep (interval);
84 timer.stop ();
86 ACE_Time_Value measured;
87 timer.elapsed_time (measured);
88 timer.elapsed_time (nanoseconds);
89 timer.elapsed_microseconds (microseconds);
90 return measured;
94 static u_int const intervals[] =
95 {0, 1, 10, 100, 1000, 10000, 100000, 1000000, 4000000}; /*usec*/
97 int
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 ()));
107 u_int errors = 0;
109 u_int iterations = 1;
111 ACE_Get_Opt getoptarg (argc, argv, ACE_TEXT ("i:"));
112 for (int c; (c = getoptarg ()) != -1; )
114 switch (c)
116 case 'i':
117 iterations = ACE_OS::atoi (getoptarg.opt_arg ());
118 break;
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,
134 nanoseconds,
135 microseconds);
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"),
143 interval_usec,
144 measured_usec,
145 (intervals[i] <= TIMER_RESOLUTION
146 ? ACE_TEXT (" (interval and measured may differ)")
147 : ACE_TEXT (""))));
149 if (intervals[i] > TIMER_RESOLUTION)
151 errors += check (interval.sec () * ACE_ONE_SECOND_IN_USECS
152 + interval.usec (),
153 measured.sec () * ACE_ONE_SECOND_IN_USECS
154 + measured.usec ());
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"),
161 measured_usec,
162 nanoseconds));
163 // This gives problems -> should be fixed
164 errors += check_micro_nano (measured.sec () * ACE_ONE_SECOND_IN_USECS
165 + measured.usec (),
166 nanoseconds);
167 ACE_DEBUG ((LM_DEBUG,
168 ACE_TEXT ("ACE_High_Res_Timer usec: %Q, nsec: %Q\n"),
169 microseconds,
170 nanoseconds));
171 // Now check the ACE_High_Res_Timer-calculated values against
172 // each other.
173 errors += check_micro_nano (microseconds, nanoseconds);
177 ACE_END_TEST;
178 return errors == 0 ? 0 : 1;