1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
//=============================================================================
/**
* @file High_Res_Timer_Test.cpp
*
* $Id: High_Res_Timer_Test.cpp 93638 2011-03-24 13:16:05Z johnnyw $
*
* Simple test of ACE_High_Res_Timer.
*
*
* @author David L. Levine <levine@cs.wustl.edu>
*/
//=============================================================================
#include "test_config.h"
#include "ace/Log_Msg.h"
#include "ace/High_Res_Timer.h"
#include "ace/Sched_Params.h"
#include "ace/Get_Opt.h"
#include "ace/OS_NS_unistd.h"
static
u_int
check (const time_t interval, const time_t measured)
{
time_t const threshold = 25 /* percent */;
time_t const difference =
interval > measured ? interval - measured : measured - interval;
time_t const percentage_difference = difference * 100 / interval;
if (percentage_difference < threshold)
return 0;
else
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("The measured time of %Q differs from ")
ACE_TEXT ("the interval of %Q by %Q percent.\n"),
measured,
interval,
percentage_difference),
1);
}
// Does a sanity check of the microseconds vs nanoseconds. They're supposed
// to represent the same interval.
static u_int
check_micro_nano (ACE_hrtime_t microinterval, ACE_hrtime_t nanointerval)
{
ACE_hrtime_t const threshold = 8 /* promille */;
microinterval *= 1000u;
ACE_hrtime_t const difference = (microinterval > nanointerval
? (microinterval - nanointerval)
: (nanointerval - microinterval));
if (nanointerval == 0)
{
nanointerval = 1; // Prevent divide-by-zero
}
ACE_hrtime_t const promille_difference =
difference * 1000 / nanointerval;
if ((promille_difference < threshold) || (difference < 1500))
{
return 0;
}
else
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("The microseconds * 1000 of %Q ")
ACE_TEXT ("differs from the nanoseconds of %Q ")
ACE_TEXT (" by %Q promille\n"),
microinterval,
nanointerval,
promille_difference),
1);
}
static
ACE_Time_Value
time_interval (const ACE_Time_Value &interval,
ACE_hrtime_t& nanoseconds,
ACE_hrtime_t& microseconds)
{
ACE_High_Res_Timer timer;
timer.start ();
ACE_OS::sleep (interval);
timer.stop ();
ACE_Time_Value measured;
timer.elapsed_time (measured);
timer.elapsed_time (nanoseconds);
timer.elapsed_microseconds (microseconds);
return measured;
}
static u_int const intervals[] =
{0, 1, 10, 100, 1000, 10000, 100000, 1000000, 4000000}; /*usec*/
int
run_main (int argc, ACE_TCHAR *argv[])
{
ACE_START_TEST (ACE_TEXT ("High_Res_Timer_Test"));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("The ACE_High_Res_Timer scale factor is %u ")
ACE_TEXT ("1/microsecond\n"),
ACE_High_Res_Timer::global_scale_factor ()));
u_int errors = 0;
u_int iterations = 1;
ACE_Get_Opt getoptarg (argc, argv, ACE_TEXT ("i:"));
for (int c; (c = getoptarg ()) != -1; )
{
switch (c)
{
case 'i':
iterations = ACE_OS::atoi (getoptarg.opt_arg ());
break;
}
}
// We don't check for errors if the interval is shorter than this
// value because the OS has a finite resolution anyway.
static u_int const TIMER_RESOLUTION = 10000;
for (u_int i = 0; i < sizeof intervals / sizeof (u_int); ++i)
{
for (u_int j = 0; j < iterations; ++j)
{
ACE_Time_Value const interval (0, intervals[i]);
ACE_hrtime_t nanoseconds;
ACE_hrtime_t microseconds;
ACE_Time_Value const measured = time_interval (interval,
nanoseconds,
microseconds);
time_t const interval_usec =
interval.sec () * ACE_ONE_SECOND_IN_USECS + interval.usec ();
time_t const measured_usec =
measured.sec () * ACE_ONE_SECOND_IN_USECS + measured.usec ();
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("interval: %: usec, measured: %: usec %s\n"),
interval_usec,
measured_usec,
(intervals[i] <= TIMER_RESOLUTION
? ACE_TEXT (" (interval and measured may differ)")
: ACE_TEXT (""))));
if (intervals[i] > TIMER_RESOLUTION)
{
errors += check (interval.sec () * ACE_ONE_SECOND_IN_USECS
+ interval.usec (),
measured.sec () * ACE_ONE_SECOND_IN_USECS
+ measured.usec ());
// Don't check for error for intervals below 10 msec.
}
// Check the ACE_Timer_Value-calculated microseconds against
// the ACE_High_Res_Timer-calculated nanoseconds.
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("ACE_Time_Value usec: %:, ACE_HR nsec: %Q\n"),
measured_usec,
nanoseconds));
// This gives problems -> should be fixed
errors += check_micro_nano (measured.sec () * ACE_ONE_SECOND_IN_USECS
+ measured.usec (),
nanoseconds);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("ACE_High_Res_Timer usec: %Q, nsec: %Q\n"),
microseconds,
nanoseconds));
// Now check the ACE_High_Res_Timer-calculated values against
// each other.
errors += check_micro_nano (microseconds, nanoseconds);
}
}
ACE_END_TEST;
return errors == 0 ? 0 : 1;
}
|