summaryrefslogtreecommitdiff
path: root/dist/threads-shared
diff options
context:
space:
mode:
authorNicholas Clark <nick@ccl4.org>2012-12-03 21:12:33 +0100
committerNicholas Clark <nick@ccl4.org>2013-01-11 11:21:31 +0100
commit1765a17458779629f77333645777cc05c08e1981 (patch)
tree703434f6bf095c07afa0afc2fb457fcac8cb1cfa /dist/threads-shared
parent64bb98afbbc7c215df7bc389ce005faffa473132 (diff)
downloadperl-1765a17458779629f77333645777cc05c08e1981.tar.gz
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.
Diffstat (limited to 'dist/threads-shared')
-rw-r--r--dist/threads-shared/t/stress.t43
1 files changed, 42 insertions, 1 deletions
diff --git a/dist/threads-shared/t/stress.t b/dist/threads-shared/t/stress.t
index e126a21379..1dd95e3959 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++
}