7 require Config; import Config;
8 if (" $Config{'extensions'} " !~ m[ Time/HiRes ]) {
9 print "1..0 # Skip -- Perl configured without Time::HiRes module\n";
15 BEGIN { $| = 1; print "1..29\n"; }
17 END { print "not ok 1\n" unless $loaded }
19 use Time::HiRes qw(tv_interval);
27 my $have_gettimeofday = defined &Time::HiRes::gettimeofday;
28 my $have_usleep = defined &Time::HiRes::usleep;
29 my $have_nanosleep = defined &Time::HiRes::nanosleep;
30 my $have_ualarm = defined &Time::HiRes::ualarm;
31 my $have_time = defined &Time::HiRes::time;
33 printf "# have_gettimeofday = %d\n", $have_gettimeofday;
34 printf "# have_usleep = %d\n", $have_usleep;
35 printf "# have_nanosleep = %d\n", $have_nanosleep;
36 printf "# have_ualarm = %d\n", $have_ualarm;
37 printf "# have_time = %d\n", $have_time;
39 import Time::HiRes 'gettimeofday' if $have_gettimeofday;
40 import Time::HiRes 'usleep' if $have_usleep;
41 import Time::HiRes 'nanosleep' if $have_nanosleep;
42 import Time::HiRes 'ualarm' if $have_ualarm;
46 my $have_alarm = $Config{d_alarm};
47 my $have_fork = $Config{d_fork};
48 my $waitfor = 60; # 10-20 seconds is normal (load affects this).
52 print "# I am the main process $$, starting the timer process...\n";
54 if (defined $timer_pid) {
55 if ($timer_pid == 0) { # We are the kid, set up the timer.
56 print "# I am the timer process $$, sleeping for $waitfor seconds...\n";
58 warn "\n$0: overall time allowed for tests (${waitfor}s) exceeded!\n";
59 print "# Terminating the main process...\n";
60 kill('TERM', getppid());
61 print "# This is the timer process $$, over and out.\n";
64 print "# Timer process $timer_pid launched, continuing testing...\n";
67 warn "$0: fork failed: $!\n";
70 print "# No timer process (need fork)\n";
75 if (open(XDEFINE, "xdefine")) {
76 chomp($xdefine = <XDEFINE>);
80 # Ideally, we'd like to test that the timers are rather precise.
81 # However, if the system is busy, there are no guarantees on how
82 # quickly we will return. This limit used to be 10%, but that
83 # was occasionally triggered falsely.
85 # Another possibility might be to print "ok" if the test completes fine
86 # with (say) 10% slosh, "skip - system may have been busy?" if the test
87 # completes fine with (say) 30% slosh, and fail otherwise. If you do that,
88 # consider changing over to test.pl at the same time.
89 # --A.D., Nov 27, 2001
90 my $limit = 0.20; # 20% is acceptable slosh for testing timers
93 map { print "ok $_ # skipped\n" } @_;
97 my ($n, $result, @info) = @_;
103 print "# @info\n" if @info;
107 unless ($have_gettimeofday) {
111 my @one = gettimeofday();
112 ok 2, @one == 2, 'gettimeofday returned ', 0+@one, ' args';
113 ok 3, $one[0] > 850_000_000, "@one too small";
117 my @two = gettimeofday();
118 ok 4, ($two[0] > $one[0] || ($two[0] == $one[0] && $two[1] > $one[1])),
119 "@two is not greater than @one";
121 my $f = Time::HiRes::time();
122 ok 5, $f > 850_000_000, "$f too small";
123 ok 6, $f - $two[0] < 2, "$f - $two[0] >= 2";
126 unless ($have_usleep) {
135 ok 7, $one == $two || $two == $three, "slept too long, $one $two $three";
137 unless ($have_gettimeofday) {
141 my $f = Time::HiRes::time();
143 my $f2 = Time::HiRes::time();
145 ok 8, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
149 # Two-arg tv_interval() is always available.
151 my $f = tv_interval [5, 100_000], [10, 500_000];
152 ok 9, abs($f - 5.4) < 0.001, $f;
155 unless ($have_gettimeofday) {
159 my $r = [gettimeofday()];
160 my $f = tv_interval $r;
164 unless ($have_usleep && $have_gettimeofday) {
168 my $r = [gettimeofday()];
169 Time::HiRes::sleep( 0.5 );
170 my $f = tv_interval $r;
171 ok 11, $f > 0.4 && $f < 0.9, "slept $f instead of 0.5 secs.";
174 unless ($have_ualarm && $have_alarm) {
179 local $SIG{ ALRM } = sub { $tick++ };
181 my $one = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
182 my $two = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
184 ok 12, $one == $two || $two == $three, "slept too long, $one $two $three";
185 print "# tick = $tick, one = $one, two = $two, three = $three\n";
187 $tick = 0; ualarm(10_000, 10_000); while ($tick < 3) { }
190 print "# tick = $tick, one = $one, two = $two, three = $three\n";
193 # Did we even get close?
195 unless ($have_time) {
198 my ($s, $n, $i) = (0);
200 $s += Time::HiRes::time() - time();
203 # $s should be, at worst, equal to $n
204 # (time() may be rounding down, up, or closest)
205 ok 14, abs($s) / $n <= 1.0, "Time::HiRes::time() not close to time()";
206 print "# s = $s, n = $n, s/n = ", $s/$n, "\n";
209 my $has_ualarm = $Config{d_ualarm};
211 $has_ualarm ||= $xdefine =~ /-DHAS_UALARM/;
213 unless ( defined &Time::HiRes::gettimeofday
214 && defined &Time::HiRes::ualarm
215 && defined &Time::HiRes::usleep
218 print "ok $_ # Skip: no gettimeofday or no ualarm or no usleep\n";
221 use Time::HiRes qw (time alarm sleep);
223 my ($f, $r, $i, $not, $ok);
226 print "# time...$f\n";
229 $r = [Time::HiRes::gettimeofday()];
231 print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n";
233 $r = [Time::HiRes::gettimeofday()];
239 select (undef, undef, undef, 3);
240 my $ival = Time::HiRes::tv_interval ($r);
241 print "# Select returned! $i $ival\n";
242 print "# ", abs($ival/3 - 1), "\n";
243 # Whether select() gets restarted after signals is
244 # implementation dependent. If it is restarted, we
245 # will get about 3.3 seconds: 3 from the select, 0.3
246 # from the alarm. If this happens, let's just skip
247 # this particular test. --jhi
248 if (abs($ival/3.3 - 1) < $limit) {
249 $ok = "Skip: your select() may get restarted by your SIGALRM (or just retry test)";
253 my $exp = 0.3 * (5 - $i);
254 # This test is more sensitive, so impose a softer limit.
255 if (abs($ival/$exp - 1) > 3*$limit) {
256 my $ratio = abs($ival/$exp);
257 $not = "while: $exp sleep took $ival ratio $ratio";
266 my $ival = Time::HiRes::tv_interval ($r);
267 print "# Tick! $i $ival\n";
268 my $exp = 0.3 * (5 - $i);
269 # This test is more sensitive, so impose a softer limit.
270 if (abs($ival/$exp - 1) > 3*$limit) {
271 my $ratio = abs($ival/$exp);
272 $not = "tick: $exp sleep took $ival ratio $ratio";
277 alarm(0); # can't cancel usig %SIG
279 print $not ? "not ok 17 # $not\n" : "ok 17 # $ok\n";
282 unless ( defined &Time::HiRes::setitimer
283 && defined &Time::HiRes::getitimer
284 && eval 'Time::HiRes::ITIMER_VIRTUAL'
286 && $Config{sig_name} =~ m/\bVTALRM\b/) {
288 print "ok $_ # Skip: no virtual interval timers\n";
291 use Time::HiRes qw (setitimer getitimer ITIMER_VIRTUAL);
294 my $r = [Time::HiRes::gettimeofday()];
297 $i ? $i-- : setitimer(ITIMER_VIRTUAL, 0);
298 print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
301 print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
303 # Assume interval timer granularity of $limit * 0.5 seconds. Too bold?
304 my $virt = getitimer(ITIMER_VIRTUAL);
305 print "not " unless defined $virt && abs($virt / 0.5) - 1 < $limit;
308 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
310 while (getitimer(ITIMER_VIRTUAL)) {
312 for (1..1000) { $j++ } # Can't be unbreakable, must test getitimer().
315 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
317 $virt = getitimer(ITIMER_VIRTUAL);
318 print "not " unless defined $virt && $virt == 0;
321 $SIG{VTALRM} = 'DEFAULT';
324 if ($have_gettimeofday) {
327 my $sleep = 1.5; # seconds
330 $t0 = gettimeofday();
331 $a = abs(sleep($sleep) / $sleep - 1.0);
332 $td = gettimeofday() - $t0;
333 my $ratio = 1.0 + $a;
335 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
337 if ($td < $sleep * (1 + $limit)) {
338 print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg";
340 print "ok 20 # Skip: $msg";
343 $t0 = gettimeofday();
344 $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0);
345 $td = gettimeofday() - $t0;
348 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
350 if ($td < $sleep * (1 + $limit)) {
351 print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg";
353 print "ok 21 # Skip: $msg";
358 print "ok $_ # Skip: no gettimeofday\n";
362 unless ($have_nanosleep) {
366 my $one = CORE::time;
367 nanosleep(10_000_000);
368 my $two = CORE::time;
369 nanosleep(10_000_000);
370 my $three = CORE::time;
371 ok 22, $one == $two || $two == $three, "slept too long, $one $two $three";
373 unless ($have_gettimeofday) {
377 my $f = Time::HiRes::time();
378 nanosleep(500_000_000);
379 my $f2 = Time::HiRes::time();
381 ok 23, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
386 print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
387 "ok 24\n" : "not ok 24\n";
390 print $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
391 "ok 25\n" : "not ok 25\n";
395 print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
396 "ok 26\n" : "not ok 26\n";
399 print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
400 "ok 27\n" : "not ok 27\n";
406 if ($have_nanosleep) {
407 eval { nanosleep(-5) };
408 print $@ =~ /::nanosleep\(-5\): negative time not invented yet/ ?
409 "ok 28\n" : "not ok 28\n";
414 if ($have_ualarm && $] >= 5.008001) {
415 # http://groups.google.com/group/perl.perl5.porters/browse_thread/thread/adaffaaf939b042e/20dafc298df737f0%2320dafc298df737f0?sa=X&oi=groupsr&start=0&num=3
416 # Perl changes [18765] and [18770], perl bug [perl #20920]
417 use Time::HiRes qw(alarm);
418 $SIG{ALRM} = sub { 1 for 1..100000 };
421 print "ok 29\n"; # Not core dumping by now is considered to be the success.
427 if (defined $timer_pid) {
428 print "# I am the main process $$, terminating the timer process $timer_pid.\n";
429 kill('TERM', $timer_pid); # We are done, the timer can go.
430 unlink("ktrace.out"); # Used in BSD system call tracing.
431 print "# All done.\n";