Integrate:
[p5sagit/p5-mst-13.2.git] / ext / Time / HiRes / 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';
7 }
dcf686c9 8}
9
f7916ddb 10BEGIN { $| = 1; print "1..25\n"; }
dcf686c9 11
12END {print "not ok 1\n" unless $loaded;}
13
14use Time::HiRes qw(tv_interval);
15
16$loaded = 1;
17
18print "ok 1\n";
19
20use strict;
21
22my $have_gettimeofday = defined &Time::HiRes::gettimeofday;
23my $have_usleep = defined &Time::HiRes::usleep;
24my $have_ualarm = defined &Time::HiRes::ualarm;
c9ff6e92 25my $have_time = defined &Time::HiRes::time;
dcf686c9 26
27import Time::HiRes 'gettimeofday' if $have_gettimeofday;
28import Time::HiRes 'usleep' if $have_usleep;
29import Time::HiRes 'ualarm' if $have_ualarm;
30
3c72ec00 31use Config;
32
3f2ee006 33my $xdefine;
34
35if (open(XDEFINE, "xdefine")) {
36 chomp($xdefine = <XDEFINE>);
37 close(XDEFINE);
38}
39
0740bb5b 40# Ideally, we'd like to test that the timers are rather precise.
41# However, if the system is busy, there are no guarantees on how
42# quickly we will return. This limit used to be 10%, but that
43# was occasionally triggered falsely.
44# Try 20%.
45# Another possibility might be to print "ok" if the test completes fine
46# with (say) 10% slosh, "skip - system may have been busy?" if the test
47# completes fine with (say) 30% slosh, and fail otherwise. If you do that,
48# consider changing over to test.pl at the same time.
49# --A.D., Nov 27, 2001
50my $limit = 0.20; # 20% is acceptable slosh for testing timers
51
dcf686c9 52sub skip {
3f2ee006 53 map { print "ok $_ # skipped\n" } @_;
dcf686c9 54}
55
56sub ok {
57 my ($n, $result, @info) = @_;
58 if ($result) {
59 print "ok $n\n";
60 }
61 else {
62 print "not ok $n\n";
63 print "# @info\n" if @info;
64 }
65}
66
67if (!$have_gettimeofday) {
68 skip 2..6;
69}
70else {
71 my @one = gettimeofday();
72 ok 2, @one == 2, 'gettimeofday returned ', 0+@one, ' args';
73 ok 3, $one[0] > 850_000_000, "@one too small";
74
75 sleep 1;
76
77 my @two = gettimeofday();
78 ok 4, ($two[0] > $one[0] || ($two[0] == $one[0] && $two[1] > $one[1])),
79 "@two is not greater than @one";
80
c9ff6e92 81 my $f = Time::HiRes::time();
dcf686c9 82 ok 5, $f > 850_000_000, "$f too small";
0740bb5b 83 ok 6, $f - $two[0] < 2, "$f - $two[0] >= 2";
dcf686c9 84}
85
86if (!$have_usleep) {
87 skip 7..8;
88}
89else {
90 my $one = time;
91 usleep(10_000);
92 my $two = time;
93 usleep(10_000);
94 my $three = time;
95 ok 7, $one == $two || $two == $three, "slept too long, $one $two $three";
96
97 if (!$have_gettimeofday) {
98 skip 8;
99 }
100 else {
c9ff6e92 101 my $f = Time::HiRes::time();
dcf686c9 102 usleep(500_000);
c9ff6e92 103 my $f2 = Time::HiRes::time();
dcf686c9 104 my $d = $f2 - $f;
0740bb5b 105 ok 8, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
dcf686c9 106 }
107}
108
109# Two-arg tv_interval() is always available.
110{
111 my $f = tv_interval [5, 100_000], [10, 500_000];
6cf89afa 112 ok 9, abs($f - 5.4) < 0.001, $f;
dcf686c9 113}
114
115if (!$have_gettimeofday) {
116 skip 10;
117}
118else {
119 my $r = [gettimeofday()];
120 my $f = tv_interval $r;
121 ok 10, $f < 2, $f;
122}
123
c9ff6e92 124if (!$have_usleep || !$have_gettimeofday) {
dcf686c9 125 skip 11;
126}
127else {
128 my $r = [gettimeofday()];
129 #jTime::HiRes::sleep 0.5;
130 Time::HiRes::sleep( 0.5 );
131 my $f = tv_interval $r;
0740bb5b 132 ok 11, $f > 0.4 && $f < 0.9, "slept $f instead of 0.5 secs.";
dcf686c9 133}
134
135if (!$have_ualarm) {
136 skip 12..13;
137}
138else {
139 my $tick = 0;
140 local $SIG{ALRM} = sub { $tick++ };
141
3f2ee006 142 my $one = time; $tick = 0; ualarm(10_000); while ($tick == 0) { sleep }
143 my $two = time; $tick = 0; ualarm(10_000); while ($tick == 0) { sleep }
dcf686c9 144 my $three = time;
145 ok 12, $one == $two || $two == $three, "slept too long, $one $two $three";
146
147 $tick = 0;
148 ualarm(10_000, 10_000);
3f2ee006 149 while ($tick < 3) { sleep }
dcf686c9 150 ok 13, 1;
151 ualarm(0);
152}
153
154# new test: did we even get close?
155
c9ff6e92 156if (!$have_time) {
157 skip 14
158} else {
456811e8 159 my ($s, $n);
160 for my $i (1 .. 100) {
161 $s += Time::HiRes::time() - time();
162 $n++;
64cc9c1c 163 }
456811e8 164 # $s should be, at worst, equal to $n
165 # (time() may be rounding down, up, or closest)
166 ok 14, abs($s) / $n <= 1.0, "Time::HiRes::time() not close to time()";
167 print "# s = $s, n = $n, s/n = ", $s/$n, "\n";
dcf686c9 168}
169
3f2ee006 170my $has_ualarm = $Config{d_ualarm};
171
172$has_ualarm ||= $xdefine =~ /-DHAS_UALARM/;
173
174unless ( defined &Time::HiRes::gettimeofday
dcf686c9 175 && defined &Time::HiRes::ualarm
c7f7de79 176 && defined &Time::HiRes::usleep
3f2ee006 177 && $has_ualarm) {
dcf686c9 178 for (15..17) {
3f2ee006 179 print "ok $_ # Skip: no gettimeofday or no ualarm or no usleep\n";
dcf686c9 180 }
181} else {
182 use Time::HiRes qw (time alarm sleep);
183
cb203acc 184 my ($f, $r, $i, $not, $ok);
dcf686c9 185
dcf686c9 186 $f = time;
add58217 187 print "# time...$f\n";
188 print "ok 15\n";
dcf686c9 189
c9ff6e92 190 $r = [Time::HiRes::gettimeofday()];
dcf686c9 191 sleep (0.5);
add58217 192 print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n";
dcf686c9 193
c9ff6e92 194 $r = [Time::HiRes::gettimeofday()];
dcf686c9 195 $i = 5;
196 $SIG{ALRM} = "tick";
6ea166ef 197 while ($i > 0)
dcf686c9 198 {
0e172911 199 alarm(0.3);
c7f7de79 200 select (undef, undef, undef, 3);
201 my $ival = Time::HiRes::tv_interval ($r);
202 print "# Select returned! $i $ival\n";
cb203acc 203 print "# ", abs($ival/3 - 1), "\n";
204 # Whether select() gets restarted after signals is
205 # implementation dependent. If it is restarted, we
206 # will get about 3.3 seconds: 3 from the select, 0.3
207 # from the alarm. If this happens, let's just skip
208 # this particular test. --jhi
209 if (abs($ival/3.3 - 1) < $limit) {
3f2ee006 210 $ok = "Skip: your select() may get restarted by your SIGALRM (or just retry test)";
cb203acc 211 undef $not;
212 last;
213 }
c7f7de79 214 my $exp = 0.3 * (5 - $i);
0740bb5b 215 # This test is more sensitive, so impose a softer limit.
cb203acc 216 if (abs($ival/$exp - 1) > 3*$limit) {
0740bb5b 217 my $ratio = abs($ival/$exp);
218 $not = "while: $exp sleep took $ival ratio $ratio";
c7f7de79 219 last;
220 }
cb203acc 221 $ok = $i;
dcf686c9 222 }
223
224 sub tick
225 {
dcf686c9 226 $i--;
c7f7de79 227 my $ival = Time::HiRes::tv_interval ($r);
228 print "# Tick! $i $ival\n";
229 my $exp = 0.3 * (5 - $i);
0740bb5b 230 # This test is more sensitive, so impose a softer limit.
cb203acc 231 if (abs($ival/$exp - 1) > 3*$limit) {
0740bb5b 232 my $ratio = abs($ival/$exp);
233 $not = "tick: $exp sleep took $ival ratio $ratio";
c7f7de79 234 $i = 0;
235 }
dcf686c9 236 }
c7f7de79 237
d0b83b2b 238 alarm(0); # can't cancel usig %SIG
dcf686c9 239
cb203acc 240 print $not ? "not ok 17 # $not\n" : "ok 17 # $ok\n";
dcf686c9 241}
242
3f2ee006 243unless ( defined &Time::HiRes::setitimer
3c72ec00 244 && defined &Time::HiRes::getitimer
3f2ee006 245 && eval 'Time::HiRes::ITIMER_VIRTUAL'
d0b83b2b 246 && $Config{d_select}
247 && $Config{sig_name} =~ m/\bVTALRM\b/) {
3c72ec00 248 for (18..19) {
0e172911 249 print "ok $_ # Skip: no virtual interval timers\n";
3c72ec00 250 }
251} else {
252 use Time::HiRes qw (setitimer getitimer ITIMER_VIRTUAL);
253
254 my $i = 3;
c9ff6e92 255 my $r = [Time::HiRes::gettimeofday()];
3c72ec00 256
257 $SIG{VTALRM} = sub {
258 $i ? $i-- : setitimer(ITIMER_VIRTUAL, 0);
259 print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
260 };
261
0e172911 262 print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
263
0740bb5b 264 # Assume interval timer granularity of $limit * 0.5 seconds. Too bold?
265 print "not " unless abs(getitimer(ITIMER_VIRTUAL) / 0.5) - 1 < $limit;
0e172911 266 print "ok 18\n";
3c72ec00 267
268 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
269
1c41c9bf 270 while (getitimer(ITIMER_VIRTUAL)) {
3f2ee006 271 my $j;
272 for (1..1000) { $j++ } # Can't be unbreakable, must test getitimer().
3c72ec00 273 }
274
275 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
276
0e172911 277 print "not " unless getitimer(ITIMER_VIRTUAL) == 0;
278 print "ok 19\n";
279
3c72ec00 280 $SIG{VTALRM} = 'DEFAULT';
281}
282
9c735d59 283if ($have_gettimeofday) {
284 my ($t0, $td);
52d72fba 285
9c735d59 286 my $sleep = 1.5; # seconds
9c735d59 287 my $msg;
288
289 $t0 = gettimeofday();
290 $a = abs(sleep($sleep) / $sleep - 1.0);
291 $td = gettimeofday() - $t0;
0740bb5b 292 my $ratio = 1.0 + $a;
9c735d59 293
0740bb5b 294 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
9c735d59 295
296 if ($td < $sleep * (1 + $limit)) {
297 print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg";
298 } else {
299 print "ok 20 # Skip: $msg";
300 }
301
302 $t0 = gettimeofday();
303 $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0);
304 $td = gettimeofday() - $t0;
0740bb5b 305 $ratio = 1.0 + $a;
9c735d59 306
0740bb5b 307 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
9c735d59 308
309 if ($td < $sleep * (1 + $limit)) {
310 print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg";
311 } else {
312 print "ok 21 # Skip: $msg";
313 }
314
315} else {
316 for (20..21) {
317 print "ok $_ # Skip: no gettimeofday\n";
318 }
319}
52d72fba 320
f7916ddb 321eval { sleep(-1) };
322print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
323 "ok 22\n" : "not ok 22\n";
324
325eval { usleep(-2) };
326print $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
327 "ok 23\n" : "not ok 23\n";
328
ec77158c 329if ($have_ualarm) {
c46d5038 330 eval { alarm(-3) };
331 print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
332 "ok 24\n" : "not ok 24\n";
333
ec77158c 334 eval { ualarm(-4) };
335 print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
f7916ddb 336 "ok 25\n" : "not ok 25\n";
ec77158c 337} else {
c46d5038 338 skip 24;
ec77158c 339 skip 25;
340}