Upgrade to Time::HiRes 1.83
[p5sagit/p5-mst-13.2.git] / ext / Time / HiRes / t / HiRes.t
CommitLineData
88cb3de1 1#!./perl -w
2
dcf686c9 3BEGIN {
3f2ee006 4 if ($ENV{PERL_CORE}) {
5 chdir 't' if -d 't';
6 @INC = '../lib';
df2f6354 7 require Config; import Config;
8 if (" $Config{'extensions'} " !~ m[ Time/HiRes ]) {
9 print "1..0 # Skip -- Perl configured without Time::HiRes module\n";
10 exit 0;
11 }
3f2ee006 12 }
dcf686c9 13}
14
ced84e60 15BEGIN { $| = 1; print "1..31\n"; }
dcf686c9 16
0cf8ddea 17END { print "not ok 1\n" unless $loaded }
dcf686c9 18
19use Time::HiRes qw(tv_interval);
20
21$loaded = 1;
22
23print "ok 1\n";
24
25use strict;
26
ced84e60 27my $have_gettimeofday = &Time::HiRes::d_gettimeofday;
28my $have_usleep = &Time::HiRes::d_usleep;
29my $have_nanosleep = &Time::HiRes::d_nanosleep;
30my $have_ualarm = &Time::HiRes::d_ualarm;
31my $have_clock_gettime = &Time::HiRes::d_clock_gettime;
32my $have_clock_getres = &Time::HiRes::d_clock_getres;
dcf686c9 33
b311af62 34sub has_symbol {
35 my $symbol = shift;
a5929ff3 36 eval "use Time::HiRes qw($symbol)";
b311af62 37 return 0 unless $@ eq '';
a5929ff3 38 eval "my \$a = $symbol";
39 return $@ eq '';
b311af62 40}
41
ced84e60 42printf "# have_gettimeofday = %d\n", $have_gettimeofday;
43printf "# have_usleep = %d\n", $have_usleep;
44printf "# have_nanosleep = %d\n", $have_nanosleep;
45printf "# have_ualarm = %d\n", $have_ualarm;
46printf "# have_clock_gettime = %d\n", $have_clock_gettime;
47printf "# have_clock_getres = %d\n", $have_clock_getres;
3d0346a5 48
dcf686c9 49import Time::HiRes 'gettimeofday' if $have_gettimeofday;
50import Time::HiRes 'usleep' if $have_usleep;
44d3ce20 51import Time::HiRes 'nanosleep' if $have_nanosleep;
dcf686c9 52import Time::HiRes 'ualarm' if $have_ualarm;
ced84e60 53import Time::HiRes 'clock_gettime' if $have_clock_gettime;
54import Time::HiRes 'clock_getres' if $have_clock_getres;
dcf686c9 55
3c72ec00 56use Config;
57
82cbdcc3 58use Time::HiRes qw(gettimeofday);
59
690f7c5f 60my $have_alarm = $Config{d_alarm};
61my $have_fork = $Config{d_fork};
3d0346a5 62my $waitfor = 60; # 10-20 seconds is normal (load affects this).
63my $timer_pid;
b311af62 64my $TheEnd;
690f7c5f 65
66if ($have_fork) {
3d0346a5 67 print "# I am the main process $$, starting the timer process...\n";
68 $timer_pid = fork();
69 if (defined $timer_pid) {
70 if ($timer_pid == 0) { # We are the kid, set up the timer.
71 print "# I am the timer process $$, sleeping for $waitfor seconds...\n";
690f7c5f 72 sleep($waitfor);
3d0346a5 73 warn "\n$0: overall time allowed for tests (${waitfor}s) exceeded!\n";
74 print "# Terminating the main process...\n";
690f7c5f 75 kill('TERM', getppid());
3d0346a5 76 print "# This is the timer process $$, over and out.\n";
690f7c5f 77 exit(0);
3d0346a5 78 } else {
b311af62 79 print "# The timer process $timer_pid launched, continuing testing...\n";
80 $TheEnd = time() + $waitfor;
690f7c5f 81 }
82 } else {
83 warn "$0: fork failed: $!\n";
84 }
85} else {
3d0346a5 86 print "# No timer process (need fork)\n";
690f7c5f 87}
88
12724655 89my $xdefine = '';
3f2ee006 90
91if (open(XDEFINE, "xdefine")) {
92 chomp($xdefine = <XDEFINE>);
93 close(XDEFINE);
94}
95
0740bb5b 96# Ideally, we'd like to test that the timers are rather precise.
97# However, if the system is busy, there are no guarantees on how
98# quickly we will return. This limit used to be 10%, but that
99# was occasionally triggered falsely.
100# Try 20%.
101# Another possibility might be to print "ok" if the test completes fine
102# with (say) 10% slosh, "skip - system may have been busy?" if the test
103# completes fine with (say) 30% slosh, and fail otherwise. If you do that,
104# consider changing over to test.pl at the same time.
105# --A.D., Nov 27, 2001
106my $limit = 0.20; # 20% is acceptable slosh for testing timers
107
dcf686c9 108sub skip {
3f2ee006 109 map { print "ok $_ # skipped\n" } @_;
dcf686c9 110}
111
112sub ok {
113 my ($n, $result, @info) = @_;
114 if ($result) {
115 print "ok $n\n";
116 }
117 else {
118 print "not ok $n\n";
119 print "# @info\n" if @info;
120 }
121}
122
3d0346a5 123unless ($have_gettimeofday) {
dcf686c9 124 skip 2..6;
125}
126else {
127 my @one = gettimeofday();
128 ok 2, @one == 2, 'gettimeofday returned ', 0+@one, ' args';
129 ok 3, $one[0] > 850_000_000, "@one too small";
130
131 sleep 1;
132
133 my @two = gettimeofday();
134 ok 4, ($two[0] > $one[0] || ($two[0] == $one[0] && $two[1] > $one[1])),
135 "@two is not greater than @one";
136
c9ff6e92 137 my $f = Time::HiRes::time();
dcf686c9 138 ok 5, $f > 850_000_000, "$f too small";
0740bb5b 139 ok 6, $f - $two[0] < 2, "$f - $two[0] >= 2";
dcf686c9 140}
141
3d0346a5 142unless ($have_usleep) {
dcf686c9 143 skip 7..8;
144}
145else {
82cbdcc3 146 use Time::HiRes qw(usleep);
dcf686c9 147 my $one = time;
148 usleep(10_000);
149 my $two = time;
150 usleep(10_000);
151 my $three = time;
152 ok 7, $one == $two || $two == $three, "slept too long, $one $two $three";
153
3d0346a5 154 unless ($have_gettimeofday) {
dcf686c9 155 skip 8;
156 }
157 else {
c9ff6e92 158 my $f = Time::HiRes::time();
dcf686c9 159 usleep(500_000);
c9ff6e92 160 my $f2 = Time::HiRes::time();
dcf686c9 161 my $d = $f2 - $f;
0740bb5b 162 ok 8, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
dcf686c9 163 }
164}
165
166# Two-arg tv_interval() is always available.
167{
168 my $f = tv_interval [5, 100_000], [10, 500_000];
6cf89afa 169 ok 9, abs($f - 5.4) < 0.001, $f;
dcf686c9 170}
171
3d0346a5 172unless ($have_gettimeofday) {
dcf686c9 173 skip 10;
174}
175else {
176 my $r = [gettimeofday()];
177 my $f = tv_interval $r;
178 ok 10, $f < 2, $f;
179}
180
3d0346a5 181unless ($have_usleep && $have_gettimeofday) {
dcf686c9 182 skip 11;
183}
184else {
82cbdcc3 185 my $r = [ gettimeofday() ];
dcf686c9 186 Time::HiRes::sleep( 0.5 );
187 my $f = tv_interval $r;
0740bb5b 188 ok 11, $f > 0.4 && $f < 0.9, "slept $f instead of 0.5 secs.";
dcf686c9 189}
190
3d0346a5 191unless ($have_ualarm && $have_alarm) {
dcf686c9 192 skip 12..13;
193}
194else {
195 my $tick = 0;
64a7a97c 196 local $SIG{ ALRM } = sub { $tick++ };
dcf686c9 197
64a7a97c 198 my $one = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
199 my $two = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
dcf686c9 200 my $three = time;
201 ok 12, $one == $two || $two == $three, "slept too long, $one $two $three";
64a7a97c 202 print "# tick = $tick, one = $one, two = $two, three = $three\n";
dcf686c9 203
64a7a97c 204 $tick = 0; ualarm(10_000, 10_000); while ($tick < 3) { }
dcf686c9 205 ok 13, 1;
206 ualarm(0);
64a7a97c 207 print "# tick = $tick, one = $one, two = $two, three = $three\n";
dcf686c9 208}
209
64a7a97c 210# Did we even get close?
dcf686c9 211
ced84e60 212unless ($have_gettimeofday) {
64a7a97c 213 skip 14;
c9ff6e92 214} else {
1fbb4de4 215 my ($s, $n, $i) = (0);
216 for $i (1 .. 100) {
456811e8 217 $s += Time::HiRes::time() - time();
218 $n++;
64cc9c1c 219 }
456811e8 220 # $s should be, at worst, equal to $n
221 # (time() may be rounding down, up, or closest)
222 ok 14, abs($s) / $n <= 1.0, "Time::HiRes::time() not close to time()";
223 print "# s = $s, n = $n, s/n = ", $s/$n, "\n";
dcf686c9 224}
225
3f2ee006 226my $has_ualarm = $Config{d_ualarm};
227
228$has_ualarm ||= $xdefine =~ /-DHAS_UALARM/;
229
230unless ( defined &Time::HiRes::gettimeofday
dcf686c9 231 && defined &Time::HiRes::ualarm
c7f7de79 232 && defined &Time::HiRes::usleep
3f2ee006 233 && $has_ualarm) {
dcf686c9 234 for (15..17) {
3f2ee006 235 print "ok $_ # Skip: no gettimeofday or no ualarm or no usleep\n";
dcf686c9 236 }
237} else {
ced84e60 238 use Time::HiRes qw(time alarm sleep);
dcf686c9 239
cb203acc 240 my ($f, $r, $i, $not, $ok);
dcf686c9 241
dcf686c9 242 $f = time;
add58217 243 print "# time...$f\n";
244 print "ok 15\n";
dcf686c9 245
c9ff6e92 246 $r = [Time::HiRes::gettimeofday()];
dcf686c9 247 sleep (0.5);
add58217 248 print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n";
dcf686c9 249
c9ff6e92 250 $r = [Time::HiRes::gettimeofday()];
dcf686c9 251 $i = 5;
252 $SIG{ALRM} = "tick";
6ea166ef 253 while ($i > 0)
dcf686c9 254 {
0e172911 255 alarm(0.3);
c7f7de79 256 select (undef, undef, undef, 3);
257 my $ival = Time::HiRes::tv_interval ($r);
258 print "# Select returned! $i $ival\n";
cb203acc 259 print "# ", abs($ival/3 - 1), "\n";
260 # Whether select() gets restarted after signals is
261 # implementation dependent. If it is restarted, we
262 # will get about 3.3 seconds: 3 from the select, 0.3
263 # from the alarm. If this happens, let's just skip
264 # this particular test. --jhi
265 if (abs($ival/3.3 - 1) < $limit) {
3f2ee006 266 $ok = "Skip: your select() may get restarted by your SIGALRM (or just retry test)";
cb203acc 267 undef $not;
268 last;
269 }
c7f7de79 270 my $exp = 0.3 * (5 - $i);
0740bb5b 271 # This test is more sensitive, so impose a softer limit.
cb203acc 272 if (abs($ival/$exp - 1) > 3*$limit) {
0740bb5b 273 my $ratio = abs($ival/$exp);
274 $not = "while: $exp sleep took $ival ratio $ratio";
c7f7de79 275 last;
276 }
cb203acc 277 $ok = $i;
dcf686c9 278 }
279
280 sub tick
281 {
dcf686c9 282 $i--;
c7f7de79 283 my $ival = Time::HiRes::tv_interval ($r);
284 print "# Tick! $i $ival\n";
285 my $exp = 0.3 * (5 - $i);
0740bb5b 286 # This test is more sensitive, so impose a softer limit.
cb203acc 287 if (abs($ival/$exp - 1) > 3*$limit) {
0740bb5b 288 my $ratio = abs($ival/$exp);
289 $not = "tick: $exp sleep took $ival ratio $ratio";
c7f7de79 290 $i = 0;
291 }
dcf686c9 292 }
c7f7de79 293
d0b83b2b 294 alarm(0); # can't cancel usig %SIG
dcf686c9 295
cb203acc 296 print $not ? "not ok 17 # $not\n" : "ok 17 # $ok\n";
dcf686c9 297}
298
3f2ee006 299unless ( defined &Time::HiRes::setitimer
3c72ec00 300 && defined &Time::HiRes::getitimer
b311af62 301 && has_symbol('ITIMER_VIRTUAL')
d0b83b2b 302 && $Config{sig_name} =~ m/\bVTALRM\b/) {
3c72ec00 303 for (18..19) {
0e172911 304 print "ok $_ # Skip: no virtual interval timers\n";
3c72ec00 305 }
306} else {
b311af62 307 use Time::HiRes qw(setitimer getitimer ITIMER_VIRTUAL);
3c72ec00 308
309 my $i = 3;
c9ff6e92 310 my $r = [Time::HiRes::gettimeofday()];
3c72ec00 311
312 $SIG{VTALRM} = sub {
b311af62 313 $i ? $i-- : setitimer(&ITIMER_VIRTUAL, 0);
3c72ec00 314 print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
315 };
316
0e172911 317 print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
318
0740bb5b 319 # Assume interval timer granularity of $limit * 0.5 seconds. Too bold?
b311af62 320 my $virt = getitimer(&ITIMER_VIRTUAL);
4ed0e2d4 321 print "not " unless defined $virt && abs($virt / 0.5) - 1 < $limit;
0e172911 322 print "ok 18\n";
3c72ec00 323
324 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
325
b311af62 326 while (getitimer(&ITIMER_VIRTUAL)) {
3f2ee006 327 my $j;
328 for (1..1000) { $j++ } # Can't be unbreakable, must test getitimer().
3c72ec00 329 }
330
331 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
332
b311af62 333 $virt = getitimer(&ITIMER_VIRTUAL);
4ed0e2d4 334 print "not " unless defined $virt && $virt == 0;
0e172911 335 print "ok 19\n";
336
3c72ec00 337 $SIG{VTALRM} = 'DEFAULT';
338}
339
82cbdcc3 340if ($have_gettimeofday &&
341 $have_usleep) {
342 use Time::HiRes qw(usleep);
343
9c735d59 344 my ($t0, $td);
52d72fba 345
9c735d59 346 my $sleep = 1.5; # seconds
9c735d59 347 my $msg;
348
349 $t0 = gettimeofday();
350 $a = abs(sleep($sleep) / $sleep - 1.0);
351 $td = gettimeofday() - $t0;
0740bb5b 352 my $ratio = 1.0 + $a;
9c735d59 353
0740bb5b 354 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
9c735d59 355
356 if ($td < $sleep * (1 + $limit)) {
357 print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg";
358 } else {
359 print "ok 20 # Skip: $msg";
360 }
361
362 $t0 = gettimeofday();
363 $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0);
364 $td = gettimeofday() - $t0;
0740bb5b 365 $ratio = 1.0 + $a;
9c735d59 366
0740bb5b 367 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
9c735d59 368
369 if ($td < $sleep * (1 + $limit)) {
370 print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg";
371 } else {
372 print "ok 21 # Skip: $msg";
373 }
374
375} else {
376 for (20..21) {
377 print "ok $_ # Skip: no gettimeofday\n";
378 }
379}
52d72fba 380
3d0346a5 381unless ($have_nanosleep) {
44d3ce20 382 skip 22..23;
383}
384else {
385 my $one = CORE::time;
386 nanosleep(10_000_000);
387 my $two = CORE::time;
388 nanosleep(10_000_000);
389 my $three = CORE::time;
390 ok 22, $one == $two || $two == $three, "slept too long, $one $two $three";
391
3d0346a5 392 unless ($have_gettimeofday) {
44d3ce20 393 skip 23;
394 }
395 else {
396 my $f = Time::HiRes::time();
397 nanosleep(500_000_000);
398 my $f2 = Time::HiRes::time();
399 my $d = $f2 - $f;
400 ok 23, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
401 }
402}
403
f7916ddb 404eval { sleep(-1) };
405print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
44d3ce20 406 "ok 24\n" : "not ok 24\n";
f7916ddb 407
408eval { usleep(-2) };
409print $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
44d3ce20 410 "ok 25\n" : "not ok 25\n";
f7916ddb 411
ec77158c 412if ($have_ualarm) {
c46d5038 413 eval { alarm(-3) };
414 print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
44d3ce20 415 "ok 26\n" : "not ok 26\n";
c46d5038 416
ec77158c 417 eval { ualarm(-4) };
418 print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
44d3ce20 419 "ok 27\n" : "not ok 27\n";
420} else {
421 skip 26;
422 skip 27;
423}
424
425if ($have_nanosleep) {
426 eval { nanosleep(-5) };
427 print $@ =~ /::nanosleep\(-5\): negative time not invented yet/ ?
428 "ok 28\n" : "not ok 28\n";
ec77158c 429} else {
44d3ce20 430 skip 28;
ec77158c 431}
690f7c5f 432
3d0346a5 433if ($have_ualarm && $] >= 5.008001) {
434 # http://groups.google.com/group/perl.perl5.porters/browse_thread/thread/adaffaaf939b042e/20dafc298df737f0%2320dafc298df737f0?sa=X&oi=groupsr&start=0&num=3
435 # Perl changes [18765] and [18770], perl bug [perl #20920]
ced84e60 436
437 # First we will find the loop size N (a for() loop 0..N-1)
438 # that will take more than T seconds.
439
440 my $T = 0.01;
441 use Time::HiRes qw(time);
442 my $N = 1024;
443 my $i;
444 N: {
445 do {
446 my $t0 = time();
447 for ($i = 0; $i < $N; $i++) { }
448 my $t1 = time();
449 my $dt = $t1 - $t0;
450 print "# N = $N, t1 = $t1, t0 = $t0, dt = $dt\n";
451 last N if $dt > $T;
452 $N *= 2;
453 } while (1);
454 }
455
456 # The time-burner which takes at least T seconds.
457 my $F = sub {
458 my $c = @_ ? shift : 1;
459 my $n = $c * $N;
460 my $i;
461 for ($i = 0; $i < $n; $i++) { }
462 };
463
464 # Then we will setup a periodic timer (the two-argument alarm() of
465 # Time::HiRes, behind the curtains the libc ualarm()) which has
466 # a signal handler that takes so much time (on the first initial
467 # invocation) that the first periodic invocation (second invocation)
468 # will happen before the first invocation has finished. In Perl 5.8.0
469 # the "safe signals" concept was implemented, with unfortunately at least
470 # one bug that caused a core dump on reentering the handler. This bug
471 # was fixed by the time of Perl 5.8.1.
472
b311af62 473 # Do not try mixing sleep() and alarm() for testing this.
474
ced84e60 475 my $a = 0; # Number of alarms we receive.
476 my $A = 2; # Number of alarms we will handle before disarming.
477 # (We may well get $A + 1 alarms.)
478
479 $SIG{ALRM} = sub {
480 $a++;
481 print "# Alarm $a - ", time(), "\n";
482 alarm(0) if $a >= $A; # Disarm the alarm.
483 $F->(2); # Try burning CPU at least for 2T seconds.
484 };
485
3d0346a5 486 use Time::HiRes qw(alarm);
ced84e60 487 alarm($T, $T); # Arm the alarm.
488
489 $F->(10); # Try burning CPU at least for 10T seconds.
490
3d0346a5 491 print "ok 29\n"; # Not core dumping by now is considered to be the success.
492} else {
493 skip 29;
494}
495
b311af62 496if ($have_clock_gettime &&
497 # All implementations of clock_gettime()
498 # are SUPPOSED TO support CLOCK_REALTIME.
499 has_symbol('CLOCK_REALTIME')) {
a5929ff3 500 my $ok = 0;
501 TRY: {
502 for my $try (1..3) {
503 print "# CLOCK_REALTIME: try = $try\n";
504 my $t0 = clock_gettime(&CLOCK_REALTIME);
505 use Time::HiRes qw(sleep);
506 my $T = 1.5;
507 sleep($T);
508 my $t1 = clock_gettime(&CLOCK_REALTIME);
509 if ($t0 > 0 && $t1 > $t0) {
510 print "# t1 = $t1, t0 = $t0\n";
511 my $dt = $t1 - $t0;
512 my $rt = abs(1 - $dt / $T);
513 print "# dt = $dt, rt = $rt\n";
514 if ($rt <= 2 * $limit) {
515 $ok = 1;
516 last TRY;
517 }
518 } else {
519 print "# Error: t0 = $t0, t1 = $t1\n";
520 }
521 my $r = rand() + rand();
522 printf "# Sleeping for %.6f seconds...\n";
523 sleep($r);
ced84e60 524 }
a5929ff3 525 }
526 if ($ok) {
527 print "ok 30\n";
ced84e60 528 } else {
a5929ff3 529 print "not ok 30\n";
ced84e60 530 }
531} else {
532 print "# No clock_gettime\n";
533 skip 30;
534}
535
536if ($have_clock_getres) {
537 my $tr = clock_getres();
538 if ($tr > 0) {
539 print "ok 31 # tr = $tr\n";
540 } else {
541 print "not ok 31 # tr = $tr\n";
542 }
543} else {
544 print "# No clock_getres\n";
545 skip 31;
546}
547
3d0346a5 548END {
549 if (defined $timer_pid) {
b311af62 550 my $left = $TheEnd - time();
551 printf "# I am the main process $$, terminating the timer process $timer_pid\n# before it terminates me in %d seconds (testing took %d seconds).\n", $left, $waitfor - $left;
3d0346a5 552 kill('TERM', $timer_pid); # We are done, the timer can go.
553 unlink("ktrace.out"); # Used in BSD system call tracing.
554 print "# All done.\n";
555 }
690f7c5f 556}
557