-
Notifications
You must be signed in to change notification settings - Fork 20
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
Showing
11 changed files
with
1,210 additions
and
130 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
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
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,99 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Webmozarts Console Parallelization package. | ||
* | ||
* (c) Webmozarts GmbH <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Webmozarts\Console\Parallelization; | ||
|
||
use Webmozart\Assert\Assert; | ||
use function ceil; | ||
use function sprintf; | ||
|
||
final class Configuration | ||
{ | ||
private $segmentSize; | ||
private $rounds; | ||
private $batches; | ||
|
||
public function __construct( | ||
bool $numberOfProcessesDefined, | ||
int $numberOfProcesses, | ||
int $numberOfItems, | ||
int $segmentSize, | ||
int $batchSize | ||
) { | ||
Assert::greaterThan( | ||
$numberOfProcesses, | ||
0, | ||
sprintf( | ||
'Expected the number of processes to be 1 or greater. Got "%s"', | ||
$numberOfProcesses | ||
) | ||
); | ||
Assert::natural( | ||
$numberOfItems, | ||
sprintf( | ||
'Expected the number of items to be 0 or greater. Got "%s"', | ||
$numberOfItems | ||
) | ||
); | ||
Assert::greaterThan( | ||
$segmentSize, | ||
0, | ||
sprintf( | ||
'Expected the segment size to be 1 or greater. Got "%s"', | ||
$segmentSize | ||
) | ||
); | ||
Assert::greaterThan( | ||
$batchSize, | ||
0, | ||
sprintf( | ||
'Expected the batch size to be 1 or greater. Got "%s"', | ||
$batchSize | ||
) | ||
); | ||
|
||
// We always check those (and not the calculated ones) since they come from the command | ||
// configuration so an issue there hints on a misconfiguration which should be fixed. | ||
Assert::greaterThanEq( | ||
$segmentSize, | ||
$batchSize, | ||
sprintf( | ||
'Expected the segment size ("%s") to be greater or equal to the batch size ("%s")', | ||
$segmentSize, | ||
$batchSize | ||
) | ||
); | ||
|
||
$this->segmentSize = 1 === $numberOfProcesses && !$numberOfProcessesDefined | ||
? $numberOfItems | ||
: $segmentSize | ||
; | ||
$this->rounds = (int) (1 === $numberOfProcesses ? 1 : ceil($numberOfItems / $segmentSize)); | ||
$this->batches = (int) (ceil($segmentSize / $batchSize) * $this->rounds); | ||
} | ||
|
||
public function getSegmentSize(): int | ||
{ | ||
return $this->segmentSize; | ||
} | ||
|
||
public function getNumberOfSegments(): int | ||
{ | ||
return $this->rounds; | ||
} | ||
|
||
public function getNumberOfBatches(): int | ||
{ | ||
return $this->batches; | ||
} | ||
} |
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,131 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Webmozarts Console Parallelization package. | ||
* | ||
* (c) Webmozarts GmbH <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Webmozarts\Console\Parallelization; | ||
|
||
use Closure; | ||
use Webmozart\Assert\Assert; | ||
use function array_chunk; | ||
use function array_values; | ||
use function count; | ||
use function get_class; | ||
use function gettype; | ||
use function is_numeric; | ||
use function is_object; | ||
use function sprintf; | ||
|
||
final class ItemBatchIterator | ||
{ | ||
private $items; | ||
private $numberOfItems; | ||
private $batchSize; | ||
private $itemsChunks; | ||
|
||
/** | ||
* @param Closure(): list<string> $fetchItems | ||
*/ | ||
public static function create(?string $item, Closure $fetchItems, int $batchSize): self | ||
{ | ||
if (null !== $item) { | ||
$items = [$item]; | ||
} else { | ||
$items = $fetchItems(); | ||
|
||
Assert::isArray( | ||
$items, | ||
sprintf( | ||
'Expected the fetched items to be a list of strings. Got "%s"', | ||
gettype($items) | ||
) | ||
); | ||
} | ||
|
||
return new self($items, $batchSize); | ||
} | ||
|
||
/** | ||
* @return list<string> | ||
*/ | ||
private static function normalizeItems($items): array | ||
{ | ||
foreach ($items as $index => $item) { | ||
if (is_numeric($item)) { | ||
$items[$index] = (string) $item; | ||
|
||
continue; | ||
} | ||
|
||
Assert::string( | ||
$item, | ||
sprintf( | ||
'The items are potentially passed to the child processes via the STDIN. For this reason they are expected to be string values. Got "%s" for the item "%s"', | ||
is_object($item) ? get_class($item) : gettype($item), | ||
$index | ||
) | ||
); | ||
} | ||
|
||
return array_values($items); | ||
} | ||
|
||
/** | ||
* @param list<string> $items | ||
* @param int $batchSize | ||
*/ | ||
public function __construct(array $items, int $batchSize) | ||
{ | ||
Assert::greaterThan( | ||
$batchSize, | ||
0, | ||
sprintf( | ||
'Expected the batch size to be 1 or greater. Got "%s"', | ||
$batchSize | ||
) | ||
); | ||
|
||
$this->items = self::normalizeItems($items); | ||
$this->itemsChunks = array_chunk( | ||
$this->items, | ||
$batchSize, | ||
false | ||
); | ||
$this->numberOfItems = count($this->items); | ||
$this->batchSize = $batchSize; | ||
} | ||
|
||
/** | ||
* @return list<string> | ||
*/ | ||
public function getItems(): array | ||
{ | ||
return $this->items; | ||
} | ||
|
||
public function getNumberOfItems(): int | ||
{ | ||
return $this->numberOfItems; | ||
} | ||
|
||
public function getBatchSize(): int | ||
{ | ||
return $this->batchSize; | ||
} | ||
|
||
/** | ||
* @return array<list<string>> | ||
*/ | ||
public function getItemBatches(): array | ||
{ | ||
return $this->itemsChunks; | ||
} | ||
} |
Oops, something went wrong.