Skip to content

Commit e41735e

Browse files
committed
test: improved pipeline tests
1 parent dacb5d1 commit e41735e

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

tests/FilterTest.php

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPUnit\Framework\ExpectationFailedException;
1212
use PHPUnit\Framework\TestCase;
1313
use stdClass;
14+
use Symfony\Component\Process\Process;
1415

1516
final class FilterTest extends TestCase
1617
{
@@ -77,26 +78,20 @@ public static function dataWritesToStreamsAndReturnsExpectedValue(): array
7778

7879
public function testBuildsAndExecutesPipeline(): ProfileInterface
7980
{
80-
$inputSizeInMiB = 16;
81-
$outputSizeInB = 0; # pre-allocated variable
82-
8381
$nullInput = fopen('php://memory', 'r');
84-
$generateData = new Filter('dd', ['if=/dev/random', 'bs=1M', 'count=' . $inputSizeInMiB]);
85-
$forwardData = new Filter('cat');
82+
$generateData = new Filter('dd', ['if=/dev/zero', 'bs=1M', 'count=16']);
83+
$compressData = new Filter('gzip');
8684
$countBytes = new Filter('wc', ['--bytes']);
8785
$pipeline = $generateData
88-
->pipe($forwardData)
89-
->pipe($forwardData)
90-
->pipe($forwardData)
91-
->pipe($forwardData)
92-
->pipe($forwardData)
86+
->pipe($compressData)
9387
->pipe($countBytes);
9488

89+
$pipelineOutput = 0; # pre-allocated variable, must be set as `+=`
9590
$profiling = Profiling::start();
96-
$outputSizeInB += (int) $pipeline->filter($nullInput); # pre-allocated variable, must be set as `+=`
91+
$pipelineOutput += (int) $pipeline->filter($nullInput);
9792
$profile = $profiling->finish();
9893

99-
self::assertSame($inputSizeInMiB * 1024 * 1024, $outputSizeInB);
94+
self::assertSame(16303, $pipelineOutput);
10095

10196
return $profile;
10297
}
@@ -114,17 +109,16 @@ public function testPipelineDoesNotHaveMemoryLeak(ProfileInterface $profile): vo
114109
#[Depends('testBuildsAndExecutesPipeline')]
115110
public function testPipelinePerformanceIsOk(ProfileInterface $profile): void
116111
{
117-
$referenceProfiling = Profiling::start();
118-
(
119-
new Filter('wc', ['--bytes']) # must be same as `$countBytes`
120-
)->filter(
121-
random_bytes(length: 1024 * 1024) # must have same length as `dd bs=`
112+
$referenceProcess = Process::fromShellCommandline(
113+
'dd if=/dev/zero bs=1M count=16 | gzip | wc --bytes', # must be same as $pipeline
122114
);
115+
$referenceProfiling = Profiling::start();
116+
$referenceProcess->mustRun();
123117
$referenceProfile = $referenceProfiling->finish();
124118

125119
try {
126120
self::assertLessThanOrEqual(
127-
$referenceProfile->getDuration() * 7,
121+
$referenceProfile->getDuration() * 1.05, # allow 5 % overhead
128122
$profile->getDuration(),
129123
);
130124
} catch (ExpectationFailedException $expectationFailed) {

0 commit comments

Comments
 (0)