use Acme::Comment type => 'C++', own_line => 1, one_line => 1; use Cwd; use Run; use Config; use Data::Dumper; use threads; //use Async; use Benchmark qw(:all); use strict; use warnings; $Config{useithreads} or die("Recompile Perl with threads to run this program."); # Change this structure accordingly... my %allScripts = ( submitters => { numRuns => 4, steps => ["tssubmitter_1.script", "tssubmitter_2.script"] }, reviewers => { numRuns => 1, steps => ["tsreviewer_1.script", "tsreviewer_2.script"] } ); my $numCompleteTestRuns = 100; my $scriptInterpreter = "ScriptRunner.exe"; my $currentDir = getcwd(); my @threadList = (); timethis($numCompleteTestRuns, 'runTests'); sub runTests { print "Waiting to spawn processes.\n"; foreach my $userType (sort {$allScripts{$a} <=> $allScripts{$b}} keys %allScripts) { // print Dumper($allScripts{$userType}{steps}); print "\nThe $userType will run $allScripts{$userType}{numRuns} times. The steps are:\n"; foreach my $i (0 .. $#{ $allScripts{$userType}{steps}}) { print "$i = $allScripts{$userType}{steps}[$i]\n"; } } print("\n\n"); # key-value pairs in a Perl hash are not always stored in the order in which they were defined! // foreach my $userType (keys %allScripts) foreach my $userType (sort {$allScripts{$a} <=> $allScripts{$b}} keys %allScripts) { for my $i (0 .. $allScripts{$userType}{numRuns}) { print "\nAbout to create $userType thread $i:\n"; my $thread = threads->new(\&stepThread, \$allScripts{$userType}); push (@threadList, $thread); } } my $numThreads = $#threadList + 1; print("There are $numThreads threads\n\n"); # wait for each running thread to end foreach my $thread (@threadList) { $thread->join; print("A thread joined\n\n"); } print "Processes ended - Goodbye.\n"; } # thread runs a type of users steps once only sub stepThread { print("starting thread\n\n"); my $currentUsertype = shift; // print Dumper(${$currentUsertype}->{steps}); foreach my $i (0 .. $#{${$currentUsertype}->{steps}}) { my $fileName = ${$currentUsertype}->{steps}[$i]; my $command = $scriptInterpreter . " \"" . $currentDir . "/" . $fileName . "\"\n"; print("About to run: $fileName\n"); my $time0 = new Benchmark; my $output = `$command`; my $time1 = new Benchmark; my $timeTaken = timestr(timediff($time1, $time0)); my $printOutput = "******** BEGIN $fileName output ********\n$output\n******** END $fileName output (time taken: $timeTaken) ********\n"; print($printOutput); } print("Ending thread\n\n"); }