Skip to content

Commit 02a4bd7

Browse files
committed
refactor: pipelines switched to native shell pipelines
1 parent 8d935a0 commit 02a4bd7

File tree

2 files changed

+15
-28
lines changed

2 files changed

+15
-28
lines changed

src/Filter.php

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,15 @@ public function filter(mixed $input, mixed $output = null, mixed $error = null):
4646
};
4747
}
4848

49-
$process = $this->startFilter($input, static function (string $type, string $data) use ($output, $error): void {
49+
$process = Process::fromShellCommandline($this->buildShellCommandLine());
50+
$process->setInput($input);
51+
$process->run(static function (string $type, string $data) use ($output, $error): void {
5052
/** @var Process::OUT|Process::ERR $type */
5153
match ($type) {
5254
Process::OUT => $output === null or fwrite($output, $data),
5355
Process::ERR => $error === null or fwrite($error, $data),
5456
};
5557
});
56-
$process->wait();
5758
if (!$process->isSuccessful()) {
5859
throw new class ($process) extends ProcessFailedException implements Exception\FilterException {
5960
};
@@ -80,22 +81,12 @@ public function pipe(self $to): self
8081
return $base;
8182
}
8283

83-
/**
84-
* @param string|resource $input
85-
*/
86-
private function startFilter(mixed $input, callable|null $callback): Process
84+
private function buildShellCommandLine(): string
8785
{
88-
if ($this->previous !== null) {
89-
$input = $this->previous->startFilter($input, null);
90-
}
91-
92-
$process = new Process([
93-
$this->command,
94-
...$this->options,
95-
]);
96-
$process->setInput($input);
97-
$process->start($callback);
98-
99-
return $process;
86+
return ($this->previous === null ? '' : "{$this->previous->buildShellCommandLine()} | ")
87+
. (new Process([
88+
$this->command,
89+
...$this->options,
90+
]))->getCommandLine();
10091
}
10192
}

tests/FilterTest.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use PetrKnap\Profiler\ProfilerInterface;
1010
use PHPUnit\Framework\Attributes\DataProvider;
1111
use PHPUnit\Framework\Attributes\Depends;
12-
use PHPUnit\Framework\ExpectationFailedException;
1312
use PHPUnit\Framework\TestCase;
1413
use stdClass;
1514
use Symfony\Component\Process\Process;
@@ -104,7 +103,7 @@ public function testPipelineDoesNotHaveMemoryLeak(ProfileInterface $profile): vo
104103
[$referenceProfile, $pipelineProfile] = $profile->getChildren();
105104

106105
self::assertLessThanOrEqual(
107-
$referenceProfile->getMemoryUsageChange() * 1.05, # allow 5% overhead
106+
$referenceProfile->getMemoryUsageChange() * 1.1, # allow 10% overhead
108107
$pipelineProfile->getMemoryUsageChange(),
109108
);
110109

@@ -117,14 +116,11 @@ public function testPipelineDoesNotHaveMemoryLeak(ProfileInterface $profile): vo
117116
public function testPipelinePerformanceIsOk(ProfileInterface $profile): void
118117
{
119118
[$referenceProfile, $pipelineProfile] = $profile->getChildren();
120-
try { # @todo fix performance and remove try / catch
121-
self::assertLessThanOrEqual(
122-
$referenceProfile->getDuration() * 1.05, # allow 5% overhead
123-
$pipelineProfile->getDuration(),
124-
);
125-
} catch (ExpectationFailedException $expectationFailed) {
126-
self::markTestIncomplete($expectationFailed->getMessage());
127-
}
119+
120+
self::assertLessThanOrEqual(
121+
$referenceProfile->getDuration() * 1.1, # allow 10% overhead
122+
$pipelineProfile->getDuration(),
123+
);
128124
}
129125

130126
#[DataProvider('dataThrows')]

0 commit comments

Comments
 (0)