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..25\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_ualarm = defined &Time::HiRes::ualarm;
30 my $have_time = defined &Time::HiRes::time;
32 import Time::HiRes 'gettimeofday' if $have_gettimeofday;
33 import Time::HiRes 'usleep' if $have_usleep;
34 import Time::HiRes 'ualarm' if $have_ualarm;
38 my $have_alarm = $Config{d_alarm};
39 my $have_fork = $Config{d_fork};
40 my $waitfor = 60; # 10 seconds is normal.
44 print "# Testing process $$\n";
45 print "# Starting the timer process\n";
46 if (defined ($pid = fork())) {
47 if ($pid == 0) { # We are the kid, set up the timer.
48 print "# Timer process $$\n";
50 warn "\n$0: overall time allowed for tests (${waitfor}s) exceeded\n";
51 print "# Terminating the testing process\n";
52 kill('TERM', getppid());
53 print "# Timer process exiting\n";
57 warn "$0: fork failed: $!\n";
60 print "# No timer process\n";
65 if (open(XDEFINE, "xdefine")) {
66 chomp($xdefine = <XDEFINE>);
70 # Ideally, we'd like to test that the timers are rather precise.
71 # However, if the system is busy, there are no guarantees on how
72 # quickly we will return. This limit used to be 10%, but that
73 # was occasionally triggered falsely.
75 # Another possibility might be to print "ok" if the test completes fine
76 # with (say) 10% slosh, "skip - system may have been busy?" if the test
77 # completes fine with (say) 30% slosh, and fail otherwise. If you do that,
78 # consider changing over to test.pl at the same time.
79 # --A.D., Nov 27, 2001
80 my $limit = 0.20; # 20% is acceptable slosh for testing timers
83 map { print "ok $_ # skipped\n" } @_;
87 my ($n, $result, @info) = @_;
93 print "# @info\n" if @info;
97 if (!$have_gettimeofday) {
101 my @one = gettimeofday();
102 ok 2, @one == 2, 'gettimeofday returned ', 0+@one, ' args';
103 ok 3, $one[0] > 850_000_000, "@one too small";
107 my @two = gettimeofday();
108 ok 4, ($two[0] > $one[0] || ($two[0] == $one[0] && $two[1] > $one[1])),
109 "@two is not greater than @one";
111 my $f = Time::HiRes::time();
112 ok 5, $f > 850_000_000, "$f too small";
113 ok 6, $f - $two[0] < 2, "$f - $two[0] >= 2";
125 ok 7, $one == $two || $two == $three, "slept too long, $one $two $three";
127 if (!$have_gettimeofday) {
131 my $f = Time::HiRes::time();
133 my $f2 = Time::HiRes::time();
135 ok 8, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
139 # Two-arg tv_interval() is always available.
141 my $f = tv_interval [5, 100_000], [10, 500_000];
142 ok 9, abs($f - 5.4) < 0.001, $f;
145 if (!$have_gettimeofday) {
149 my $r = [gettimeofday()];
150 my $f = tv_interval $r;
154 if (!$have_usleep || !$have_gettimeofday) {
158 my $r = [gettimeofday()];
159 Time::HiRes::sleep( 0.5 );
160 my $f = tv_interval $r;
161 ok 11, $f > 0.4 && $f < 0.9, "slept $f instead of 0.5 secs.";
164 if (!$have_ualarm || !$have_alarm) {
169 local $SIG{ ALRM } = sub { $tick++ };
171 my $one = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
172 my $two = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
174 ok 12, $one == $two || $two == $three, "slept too long, $one $two $three";
175 print "# tick = $tick, one = $one, two = $two, three = $three\n";
177 $tick = 0; ualarm(10_000, 10_000); while ($tick < 3) { }
180 print "# tick = $tick, one = $one, two = $two, three = $three\n";
183 # Did we even get close?
188 my ($s, $n, $i) = (0);
190 $s += Time::HiRes::time() - time();
193 # $s should be, at worst, equal to $n
194 # (time() may be rounding down, up, or closest)
195 ok 14, abs($s) / $n <= 1.0, "Time::HiRes::time() not close to time()";
196 print "# s = $s, n = $n, s/n = ", $s/$n, "\n";
199 my $has_ualarm = $Config{d_ualarm};
201 $has_ualarm ||= $xdefine =~ /-DHAS_UALARM/;
203 unless ( defined &Time::HiRes::gettimeofday
204 && defined &Time::HiRes::ualarm
205 && defined &Time::HiRes::usleep
208 print "ok $_ # Skip: no gettimeofday or no ualarm or no usleep\n";
211 use Time::HiRes qw (time alarm sleep);
213 my ($f, $r, $i, $not, $ok);
216 print "# time...$f\n";
219 $r = [Time::HiRes::gettimeofday()];
221 print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n";
223 $r = [Time::HiRes::gettimeofday()];
229 select (undef, undef, undef, 3);
230 my $ival = Time::HiRes::tv_interval ($r);
231 print "# Select returned! $i $ival\n";
232 print "# ", abs($ival/3 - 1), "\n";
233 # Whether select() gets restarted after signals is
234 # implementation dependent. If it is restarted, we
235 # will get about 3.3 seconds: 3 from the select, 0.3
236 # from the alarm. If this happens, let's just skip
237 # this particular test. --jhi
238 if (abs($ival/3.3 - 1) < $limit) {
239 $ok = "Skip: your select() may get restarted by your SIGALRM (or just retry test)";
243 my $exp = 0.3 * (5 - $i);
244 # This test is more sensitive, so impose a softer limit.
245 if (abs($ival/$exp - 1) > 3*$limit) {
246 my $ratio = abs($ival/$exp);
247 $not = "while: $exp sleep took $ival ratio $ratio";
256 my $ival = Time::HiRes::tv_interval ($r);
257 print "# Tick! $i $ival\n";
258 my $exp = 0.3 * (5 - $i);
259 # This test is more sensitive, so impose a softer limit.
260 if (abs($ival/$exp - 1) > 3*$limit) {
261 my $ratio = abs($ival/$exp);
262 $not = "tick: $exp sleep took $ival ratio $ratio";
267 alarm(0); # can't cancel usig %SIG
269 print $not ? "not ok 17 # $not\n" : "ok 17 # $ok\n";
272 unless ( defined &Time::HiRes::setitimer
273 && defined &Time::HiRes::getitimer
274 && eval 'Time::HiRes::ITIMER_VIRTUAL'
276 && $Config{sig_name} =~ m/\bVTALRM\b/) {
278 print "ok $_ # Skip: no virtual interval timers\n";
281 use Time::HiRes qw (setitimer getitimer ITIMER_VIRTUAL);
284 my $r = [Time::HiRes::gettimeofday()];
287 $i ? $i-- : setitimer(ITIMER_VIRTUAL, 0);
288 print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
291 print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
293 # Assume interval timer granularity of $limit * 0.5 seconds. Too bold?
294 my $virt = getitimer(ITIMER_VIRTUAL);
295 print "not " unless defined $virt && abs($virt / 0.5) - 1 < $limit;
298 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
300 while (getitimer(ITIMER_VIRTUAL)) {
302 for (1..1000) { $j++ } # Can't be unbreakable, must test getitimer().
305 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
307 $virt = getitimer(ITIMER_VIRTUAL);
308 print "not " unless defined $virt && $virt == 0;
311 $SIG{VTALRM} = 'DEFAULT';
314 if ($have_gettimeofday) {
317 my $sleep = 1.5; # seconds
320 $t0 = gettimeofday();
321 $a = abs(sleep($sleep) / $sleep - 1.0);
322 $td = gettimeofday() - $t0;
323 my $ratio = 1.0 + $a;
325 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
327 if ($td < $sleep * (1 + $limit)) {
328 print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg";
330 print "ok 20 # Skip: $msg";
333 $t0 = gettimeofday();
334 $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0);
335 $td = gettimeofday() - $t0;
338 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
340 if ($td < $sleep * (1 + $limit)) {
341 print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg";
343 print "ok 21 # Skip: $msg";
348 print "ok $_ # Skip: no gettimeofday\n";
353 print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
354 "ok 22\n" : "not ok 22\n";
357 print $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
358 "ok 23\n" : "not ok 23\n";
362 print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
363 "ok 24\n" : "not ok 24\n";
366 print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
367 "ok 25\n" : "not ok 25\n";
374 print "# Terminating the timer process $pid\n";
375 kill('TERM', $pid); # We are done, the timer can go.
376 unlink("ktrace.out");