From: Jarkko Hietaniemi Date: Sun, 4 Nov 2001 17:43:10 +0000 (+0000) Subject: Protect against high load: measure how much wall X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9c735d5936d2e28562f488b19be586dbf4c6d6d2;p=p5sagit%2Fp5-mst-13.2.git Protect against high load: measure how much wall clock time went by while sleeping. Avoids some false negatives, while may introduce some false positives. Life is hard. p4raw-id: //depot/perl@12846 --- diff --git a/ext/Time/HiRes/HiRes.t b/ext/Time/HiRes/HiRes.t index f92fba2..bee3257 100644 --- a/ext/Time/HiRes/HiRes.t +++ b/ext/Time/HiRes/HiRes.t @@ -223,11 +223,42 @@ unless (defined &Time::HiRes::setitimer $SIG{VTALRM} = 'DEFAULT'; } -$a = abs(sleep(1.5) / 1.5 - 1.0); -print $a < 0.1 ? "ok 20 # $a\n" : "not ok 20 # $a\n"; +if ($have_gettimeofday) { + my ($t0, $td); -$a = abs(usleep(1_500_000) / 1_500_000 - 1.0); -print $a < 0.1 ? "ok 21 # $a\n" : "not ok 21 # $a\n"; + my $sleep = 1.5; # seconds + my $limit = 0.1; # 10% is acceptable slosh for timers + my $msg; + + $t0 = gettimeofday(); + $a = abs(sleep($sleep) / $sleep - 1.0); + $td = gettimeofday() - $t0; + + $msg = "$td went by while sleeping $sleep, ratio $a\n"; + + if ($td < $sleep * (1 + $limit)) { + print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg"; + } else { + print "ok 20 # Skip: $msg"; + } + + $t0 = gettimeofday(); + $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0); + $td = gettimeofday() - $t0; + + $msg = "$td went by while sleeping $sleep, ratio $a\n"; + + if ($td < $sleep * (1 + $limit)) { + print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg"; + } else { + print "ok 21 # Skip: $msg"; + } + +} else { + for (20..21) { + print "ok $_ # Skip: no gettimeofday\n"; + } +} eval { sleep(-1) }; print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?