#! perl -slw use Cwd; use Config; use Data::Dumper; use threads; use Benchmark qw(:all); use strict; use warnings; # Change this structure accordingly... my %allScripts = ( submitters => { numRuns => 4, steps => [ q[perl -wle"sleep int rand 3; print 'step1'"], q[perl -wle"sleep int rand 3; print 'step2'"] ] }, reviewers => { numRuns => 1, steps => [ q[perl -wle"sleep int rand 3; print 'step1'"], q[perl -wle"sleep int rand 3; print 'step2'"] ] } ); my $numCompleteTestRuns = 4; my $mscriptInterpreter = "ScriptRunner.exe"; my $currentDir = getcwd(); eval{ timethis( $numCompleteTestRuns, 'runTests' ) }; warn $@ if $@; sub runTests { print "Waiting to spawn processes."; foreach my $userType ( sort{ $allScripts{$a} <=> $allScripts{$b} } keys %allScripts ) { print "The $userType will run $allScripts{$userType}{numRuns} times. The steps are:"; foreach my $i (0 .. $#{ $allScripts{$userType}{steps}}) { print "$i = $allScripts{$userType}{steps}[$i]"; } } my @threadList = (); foreach my $userType ( sort {$allScripts{$a} <=> $allScripts{$b}} keys %allScripts ) { for my $i (0 .. $allScripts{$userType}{numRuns}) { print "About to create $userType thread $i:"; my $thread = threads->new(\&stepThread, \$allScripts{$userType}); push (@threadList, $thread); } } my $numThreads = @threadList; print "There are $numThreads threads"; # wait for each running thread to end foreach my $thread (@threadList) { $thread->join; print "A thread joined"; } print "Processes ended - Goodbye"; } # thread runs a type of users steps once only sub stepThread { print "starting thread"; my $currentUsertype = shift; foreach my $i ( 0 .. $#{ ${$currentUsertype}->{steps} } ) { my $fileName = ${ $currentUsertype }->{steps}[$i]; my $command = $fileName; print( "About to run: $fileName" ); my $time0 = new Benchmark; my $output = `$command`; my $time1 = new Benchmark; my $timeTaken = timestr( timediff( $time1, $time0 ) ); print "BEGIN $fileName\n'$output'\nEND $fileName output (time taken: $timeTaken)"; } print( "Ending thread" ); }