Upgrade to Time-HiRes-1.76
[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
3d0346a5 15BEGIN { $| = 1; print "1..29\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
27my $have_gettimeofday = defined &Time::HiRes::gettimeofday;
28my $have_usleep = defined &Time::HiRes::usleep;
44d3ce20 29my $have_nanosleep = defined &Time::HiRes::nanosleep;
dcf686c9 30my $have_ualarm = defined &Time::HiRes::ualarm;
c9ff6e92 31my $have_time = defined &Time::HiRes::time;
dcf686c9 32
3d0346a5 33printf "# have_gettimeofday = %d\n", $have_gettimeofday;
34printf "# have_usleep = %d\n", $have_usleep;
35printf "# have_nanosleep = %d\n", $have_nanosleep;
36printf "# have_ualarm = %d\n", $have_ualarm;
37printf "# have_time = %d\n", $have_time;
38
dcf686c9 39import Time::HiRes 'gettimeofday' if $have_gettimeofday;
40import Time::HiRes 'usleep' if $have_usleep;
44d3ce20 41import Time::HiRes 'nanosleep' if $have_nanosleep;
dcf686c9 42import Time::HiRes 'ualarm' if $have_ualarm;
43
3c72ec00 44use Config;
45
690f7c5f 46my $have_alarm = $Config{d_alarm};
47my $have_fork = $Config{d_fork};
3d0346a5 48my $waitfor = 60; # 10-20 seconds is normal (load affects this).
49my $timer_pid;
690f7c5f 50
51if ($have_fork) {
3d0346a5 52 print "# I am the main process $$, starting the timer process...\n";
53 $timer_pid = fork();
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";
690f7c5f 57 sleep($waitfor);
3d0346a5 58 warn "\n$0: overall time allowed for tests (${waitfor}s) exceeded!\n";
59 print "# Terminating the main process...\n";
690f7c5f 60 kill('TERM', getppid());
3d0346a5 61 print "# This is the timer process $$, over and out.\n";
690f7c5f 62 exit(0);
3d0346a5 63 } else {
64 print "# Timer process $timer_pid launched, continuing testing...\n";
690f7c5f 65 }
66 } else {
67 warn "$0: fork failed: $!\n";
68 }
69} else {
3d0346a5 70 print "# No timer process (need fork)\n";
690f7c5f 71}
72
12724655 73my $xdefine = '';
3f2ee006 74
75if (open(XDEFINE, "xdefine")) {
76 chomp($xdefine = <XDEFINE>);
77 close(XDEFINE);
78}
79
0740bb5b 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.
84# Try 20%.
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
90my $limit = 0.20; # 20% is acceptable slosh for testing timers
91
dcf686c9 92sub skip {
3f2ee006 93 map { print "ok $_ # skipped\n" } @_;
dcf686c9 94}
95
96sub ok {
97 my ($n, $result, @info) = @_;
98 if ($result) {
99 print "ok $n\n";
100 }
101 else {
102 print "not ok $n\n";
103 print "# @info\n" if @info;
104 }
105}
106
3d0346a5 107unless ($have_gettimeofday) {
dcf686c9 108 skip 2..6;
109}
110else {
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";
114
115 sleep 1;
116
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";
120
c9ff6e92 121 my $f = Time::HiRes::time();
dcf686c9 122 ok 5, $f > 850_000_000, "$f too small";
0740bb5b 123 ok 6, $f - $two[0] < 2, "$f - $two[0] >= 2";
dcf686c9 124}
125
3d0346a5 126unless ($have_usleep) {
dcf686c9 127 skip 7..8;
128}
129else {
130 my $one = time;
131 usleep(10_000);
132 my $two = time;
133 usleep(10_000);
134 my $three = time;
135 ok 7, $one == $two || $two == $three, "slept too long, $one $two $three";
136
3d0346a5 137 unless ($have_gettimeofday) {
dcf686c9 138 skip 8;
139 }
140 else {
c9ff6e92 141 my $f = Time::HiRes::time();
dcf686c9 142 usleep(500_000);
c9ff6e92 143 my $f2 = Time::HiRes::time();
dcf686c9 144 my $d = $f2 - $f;
0740bb5b 145 ok 8, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
dcf686c9 146 }
147}
148
149# Two-arg tv_interval() is always available.
150{
151 my $f = tv_interval [5, 100_000], [10, 500_000];
6cf89afa 152 ok 9, abs($f - 5.4) < 0.001, $f;
dcf686c9 153}
154
3d0346a5 155unless ($have_gettimeofday) {
dcf686c9 156 skip 10;
157}
158else {
159 my $r = [gettimeofday()];
160 my $f = tv_interval $r;
161 ok 10, $f < 2, $f;
162}
163
3d0346a5 164unless ($have_usleep && $have_gettimeofday) {
dcf686c9 165 skip 11;
166}
167else {
168 my $r = [gettimeofday()];
dcf686c9 169 Time::HiRes::sleep( 0.5 );
170 my $f = tv_interval $r;
0740bb5b 171 ok 11, $f > 0.4 && $f < 0.9, "slept $f instead of 0.5 secs.";
dcf686c9 172}
173
3d0346a5 174unless ($have_ualarm && $have_alarm) {
dcf686c9 175 skip 12..13;
176}
177else {
178 my $tick = 0;
64a7a97c 179 local $SIG{ ALRM } = sub { $tick++ };
dcf686c9 180
64a7a97c 181 my $one = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
182 my $two = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
dcf686c9 183 my $three = time;
184 ok 12, $one == $two || $two == $three, "slept too long, $one $two $three";
64a7a97c 185 print "# tick = $tick, one = $one, two = $two, three = $three\n";
dcf686c9 186
64a7a97c 187 $tick = 0; ualarm(10_000, 10_000); while ($tick < 3) { }
dcf686c9 188 ok 13, 1;
189 ualarm(0);
64a7a97c 190 print "# tick = $tick, one = $one, two = $two, three = $three\n";
dcf686c9 191}
192
64a7a97c 193# Did we even get close?
dcf686c9 194
3d0346a5 195unless ($have_time) {
64a7a97c 196 skip 14;
c9ff6e92 197} else {
1fbb4de4 198 my ($s, $n, $i) = (0);
199 for $i (1 .. 100) {
456811e8 200 $s += Time::HiRes::time() - time();
201 $n++;
64cc9c1c 202 }
456811e8 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";
dcf686c9 207}
208
3f2ee006 209my $has_ualarm = $Config{d_ualarm};
210
211$has_ualarm ||= $xdefine =~ /-DHAS_UALARM/;
212
213unless ( defined &Time::HiRes::gettimeofday
dcf686c9 214 && defined &Time::HiRes::ualarm
c7f7de79 215 && defined &Time::HiRes::usleep
3f2ee006 216 && $has_ualarm) {
dcf686c9 217 for (15..17) {
3f2ee006 218 print "ok $_ # Skip: no gettimeofday or no ualarm or no usleep\n";
dcf686c9 219 }
220} else {
221 use Time::HiRes qw (time alarm sleep);
222
cb203acc 223 my ($f, $r, $i, $not, $ok);
dcf686c9 224
dcf686c9 225 $f = time;
add58217 226 print "# time...$f\n";
227 print "ok 15\n";
dcf686c9 228
c9ff6e92 229 $r = [Time::HiRes::gettimeofday()];
dcf686c9 230 sleep (0.5);
add58217 231 print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n";
dcf686c9 232
c9ff6e92 233 $r = [Time::HiRes::gettimeofday()];
dcf686c9 234 $i = 5;
235 $SIG{ALRM} = "tick";
6ea166ef 236 while ($i > 0)
dcf686c9 237 {
0e172911 238 alarm(0.3);
c7f7de79 239 select (undef, undef, undef, 3);
240 my $ival = Time::HiRes::tv_interval ($r);
241 print "# Select returned! $i $ival\n";
cb203acc 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) {
3f2ee006 249 $ok = "Skip: your select() may get restarted by your SIGALRM (or just retry test)";
cb203acc 250 undef $not;
251 last;
252 }
c7f7de79 253 my $exp = 0.3 * (5 - $i);
0740bb5b 254 # This test is more sensitive, so impose a softer limit.
cb203acc 255 if (abs($ival/$exp - 1) > 3*$limit) {
0740bb5b 256 my $ratio = abs($ival/$exp);
257 $not = "while: $exp sleep took $ival ratio $ratio";
c7f7de79 258 last;
259 }
cb203acc 260 $ok = $i;
dcf686c9 261 }
262
263 sub tick
264 {
dcf686c9 265 $i--;
c7f7de79 266 my $ival = Time::HiRes::tv_interval ($r);
267 print "# Tick! $i $ival\n";
268 my $exp = 0.3 * (5 - $i);
0740bb5b 269 # This test is more sensitive, so impose a softer limit.
cb203acc 270 if (abs($ival/$exp - 1) > 3*$limit) {
0740bb5b 271 my $ratio = abs($ival/$exp);
272 $not = "tick: $exp sleep took $ival ratio $ratio";
c7f7de79 273 $i = 0;
274 }
dcf686c9 275 }
c7f7de79 276
d0b83b2b 277 alarm(0); # can't cancel usig %SIG
dcf686c9 278
cb203acc 279 print $not ? "not ok 17 # $not\n" : "ok 17 # $ok\n";
dcf686c9 280}
281
3f2ee006 282unless ( defined &Time::HiRes::setitimer
3c72ec00 283 && defined &Time::HiRes::getitimer
3f2ee006 284 && eval 'Time::HiRes::ITIMER_VIRTUAL'
d0b83b2b 285 && $Config{d_select}
286 && $Config{sig_name} =~ m/\bVTALRM\b/) {
3c72ec00 287 for (18..19) {
0e172911 288 print "ok $_ # Skip: no virtual interval timers\n";
3c72ec00 289 }
290} else {
291 use Time::HiRes qw (setitimer getitimer ITIMER_VIRTUAL);
292
293 my $i = 3;
c9ff6e92 294 my $r = [Time::HiRes::gettimeofday()];
3c72ec00 295
296 $SIG{VTALRM} = sub {
297 $i ? $i-- : setitimer(ITIMER_VIRTUAL, 0);
298 print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
299 };
300
0e172911 301 print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
302
0740bb5b 303 # Assume interval timer granularity of $limit * 0.5 seconds. Too bold?
4ed0e2d4 304 my $virt = getitimer(ITIMER_VIRTUAL);
305 print "not " unless defined $virt && abs($virt / 0.5) - 1 < $limit;
0e172911 306 print "ok 18\n";
3c72ec00 307
308 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
309
1c41c9bf 310 while (getitimer(ITIMER_VIRTUAL)) {
3f2ee006 311 my $j;
312 for (1..1000) { $j++ } # Can't be unbreakable, must test getitimer().
3c72ec00 313 }
314
315 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
316
4ed0e2d4 317 $virt = getitimer(ITIMER_VIRTUAL);
318 print "not " unless defined $virt && $virt == 0;
0e172911 319 print "ok 19\n";
320
3c72ec00 321 $SIG{VTALRM} = 'DEFAULT';
322}
323
9c735d59 324if ($have_gettimeofday) {
325 my ($t0, $td);
52d72fba 326
9c735d59 327 my $sleep = 1.5; # seconds
9c735d59 328 my $msg;
329
330 $t0 = gettimeofday();
331 $a = abs(sleep($sleep) / $sleep - 1.0);
332 $td = gettimeofday() - $t0;
0740bb5b 333 my $ratio = 1.0 + $a;
9c735d59 334
0740bb5b 335 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
9c735d59 336
337 if ($td < $sleep * (1 + $limit)) {
338 print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg";
339 } else {
340 print "ok 20 # Skip: $msg";
341 }
342
343 $t0 = gettimeofday();
344 $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0);
345 $td = gettimeofday() - $t0;
0740bb5b 346 $ratio = 1.0 + $a;
9c735d59 347
0740bb5b 348 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
9c735d59 349
350 if ($td < $sleep * (1 + $limit)) {
351 print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg";
352 } else {
353 print "ok 21 # Skip: $msg";
354 }
355
356} else {
357 for (20..21) {
358 print "ok $_ # Skip: no gettimeofday\n";
359 }
360}
52d72fba 361
3d0346a5 362unless ($have_nanosleep) {
44d3ce20 363 skip 22..23;
364}
365else {
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";
372
3d0346a5 373 unless ($have_gettimeofday) {
44d3ce20 374 skip 23;
375 }
376 else {
377 my $f = Time::HiRes::time();
378 nanosleep(500_000_000);
379 my $f2 = Time::HiRes::time();
380 my $d = $f2 - $f;
381 ok 23, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
382 }
383}
384
f7916ddb 385eval { sleep(-1) };
386print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
44d3ce20 387 "ok 24\n" : "not ok 24\n";
f7916ddb 388
389eval { usleep(-2) };
390print $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
44d3ce20 391 "ok 25\n" : "not ok 25\n";
f7916ddb 392
ec77158c 393if ($have_ualarm) {
c46d5038 394 eval { alarm(-3) };
395 print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
44d3ce20 396 "ok 26\n" : "not ok 26\n";
c46d5038 397
ec77158c 398 eval { ualarm(-4) };
399 print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
44d3ce20 400 "ok 27\n" : "not ok 27\n";
401} else {
402 skip 26;
403 skip 27;
404}
405
406if ($have_nanosleep) {
407 eval { nanosleep(-5) };
408 print $@ =~ /::nanosleep\(-5\): negative time not invented yet/ ?
409 "ok 28\n" : "not ok 28\n";
ec77158c 410} else {
44d3ce20 411 skip 28;
ec77158c 412}
690f7c5f 413
3d0346a5 414if ($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 };
419 alarm(0.01, 0.01);
420 sleep(1);
421 print "ok 29\n"; # Not core dumping by now is considered to be the success.
422} else {
423 skip 29;
424}
425
426END {
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";
432 }
690f7c5f 433}
434