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