Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 141e5ff
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
PHPUnit Parallel Testing | ||
==================== | ||
This repository contains an attempt to do parallel testing with PHPUnit. | ||
The PHP file will loop over the PHPUnit files in the directory you specify and run PHPUnit in separate processes. | ||
You can specify how many tests you want to run at the same time. | ||
|
||
When all the tests have run, the php file will echo the output of the tests. | ||
|
||
Arguments | ||
--------------------- | ||
Run with php parallel.php | ||
Options are: | ||
-d directory/ | ||
-m maxParallelTests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
$arguments = getopt("d:m:"); | ||
|
||
$directory = (isset($arguments['d']) ? $arguments['d'] : ""); | ||
$maxParallel = (isset($arguments['m']) ? (int) $arguments['m'] : 5);; | ||
|
||
$currentDirectory = dirname(__FILE__); | ||
|
||
$results = $files = $runningProcesses = array(); | ||
|
||
|
||
// grab all the *.php files (phpunit tests) in the directory we specified | ||
foreach (glob($directory . "*.php") as $filename) { | ||
$filepath = realpath($currentDirectory) . "/" . $filename; | ||
|
||
if ($filepath === __FILE__) { | ||
// don't run this script again :) | ||
continue; | ||
} | ||
|
||
$files[] = $filepath; | ||
} | ||
|
||
if (empty($files)) { | ||
die("No test files found"); | ||
} | ||
|
||
$i = 0; | ||
|
||
while ($i < sizeof($files)) { | ||
if ($i >= $maxParallel) { | ||
foreach ($runningProcesses as $key => $process) { | ||
$status = proc_get_status($process['handle']); | ||
if ($status['running'] !== true) { | ||
unset($runningProcesses[$key]); | ||
unset($files[$process['nr']]); | ||
$files = array_values($files); | ||
$i--; | ||
|
||
// close the process | ||
proc_close($process['handle']); | ||
} | ||
} | ||
continue; | ||
} | ||
|
||
$descriptorspec = array( | ||
0 => array("pipe", "r"), // stdin is a pipe that the child will read from | ||
1 => array("pipe", "w"), // stdout is a pipe that the child will write to | ||
2 => array("pipe", "w") // stderr | ||
); | ||
|
||
$pipes = array(); | ||
$process = proc_open('phpunit ' . $files[$i], $descriptorspec, $pipes, null, null); | ||
|
||
if (is_resource($process)) { | ||
$runningProcesses[] = array('handle' => $process, 'nr' => $i); | ||
|
||
$results[$files[$i]] = stream_get_contents($pipes[1]); | ||
|
||
// It is important that you close any pipes before calling | ||
// proc_close in order to avoid a deadlock | ||
fclose($pipes[1]); | ||
} | ||
|
||
$i++; | ||
|
||
if (empty($files)) { | ||
break; | ||
} | ||
} | ||
|
||
// tests ended - display results | ||
foreach ($results as $testFileName => $result) { | ||
echo "*** Results for [" . $testFileName . "]: \n" . $result . "\n\n"; | ||
} |