In perl.git, the branch blead has been updated <http://perl5.git.perl.org/perl.git/commitdiff/1765a17458779629f77333645777cc05c08e1981?hp=64bb98afbbc7c215df7bc389ce005faffa473132>
- Log ----------------------------------------------------------------- commit 1765a17458779629f77333645777cc05c08e1981 Author: Nicholas Clark <[email protected]> Date: Mon Dec 3 21:12:33 2012 +0100 Don't use a fixed iteration count in dist/threads-shared/t/stress.t Looping 500,000 times takes between 0.025s and 1s depending on hardware and optimisation levels on machines I have access to. For a fixed iteration count, on a particularly slow machine the timeout can fire before all threads have had a realistic chance to complete, but dropping the iteration count will cause fast machines to finish each thread too quickly. So use an initial busy loop (single-thread) to estimate a suitable iteration count to use for the per-thread test loop. ----------------------------------------------------------------------- Summary of changes: dist/threads-shared/t/stress.t | 43 +++++++++++++++++++++++++++++++++++++++- 1 files changed, 42 insertions(+), 1 deletions(-) diff --git a/dist/threads-shared/t/stress.t b/dist/threads-shared/t/stress.t index e126a21..1dd95e3 100644 --- a/dist/threads-shared/t/stress.t +++ b/dist/threads-shared/t/stress.t @@ -42,6 +42,47 @@ use threads::shared; { my $cnt = 50; + # Depending on hardware and compiler options, the time for a busy loop can + # by a factor of (at least) 40, so one size doesn't fit all. + # For a fixed iteration count, on a particularly slow machine the timeout + # can fire before all threads have had a realistic chance to complete, but + # dropping the iteration count will cause fast machines to finish each + # thread too quickly. + # Fastest machine I tested can loop 20,000,000 times a second, slowest + # 500,000 + + my $busycount; + { + my $tries = 1e4; + # Try to align to the start of a second: + my $want = time + 1; + while (time < $want && --$tries) { + my $sum; + for (0..1e4) { + ++$sum; + } + } + + if ($tries) { + $tries = 1e4; + ++$want; + + while (time < $want && --$tries) { + my $sum; + for (0..1e4) { + ++$sum; + } + } + + # This should be about 0.025s + $busycount = (1e4 - $tries) * 250; + } else { + # Fall back to the old default if everything fails + $busycount = 500000; + } + print "# Looping for $busycount iterations should take about 0.025s\n"; + } + my $TIMEOUT = 60; my $mutex = 1; @@ -60,7 +101,7 @@ use threads::shared; # Randomize the amount of work the thread does my $sum; - for (0..(500000+int(rand(500000)))) { + for (0..($busycount+int(rand($busycount)))) { $sum++ } -- Perl5 Master Repository
