Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions src/Benchmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,28 @@ class Benchmark

protected ?Closure $beforeEach = null;

protected ?Closure $afterEach = null;

protected array $result = [
'each' => [],
'total' => [],
];

public function __construct(
protected Runner $runner = new Runner(),
protected Transformer $transformer = new Transformer()
protected Runner $runner = new Runner,
protected Transformer $transformer = new Transformer
) {
$this->view = new View(
new SymfonyStyle(
new ArgvInput(),
new ConsoleOutput()
new ArgvInput,
new ConsoleOutput
)
);
}

public static function start(): static
{
return new static();
return new static;
}

public function beforeEach(callable $callback): self
Expand All @@ -58,6 +60,13 @@ public function beforeEach(callable $callback): self
return $this;
}

public function afterEach(callable $callback): self
{
$this->afterEach = $callback;

return $this;
}

public function iterations(int $count): self
{
$this->iterations = max(1, $count);
Expand Down Expand Up @@ -114,23 +123,25 @@ protected function each(mixed $name, callable $callback, ProgressBarService $pro
protected function run(mixed $name, callable $callback, ProgressBarService $progressBar): void
{
for ($i = 1; $i <= $this->iterations; ++$i) {
$result = $this->runBeforeEach($name, $i);
$result = $this->runCallback($this->beforeEach, $name, $i);

[$time, $ram] = $this->call($callback, [$i, $result]);

$this->runCallback($this->afterEach, $name, $i, $time, $ram);

$this->push($name, $i, $time, $ram);

$progressBar->advance();
}
}

protected function runBeforeEach(mixed $name, int $iteration): mixed
protected function runCallback(?Closure $callback, mixed ...$arguments): mixed
{
if ($callback = $this->beforeEach) {
return $callback($name, $iteration);
if (! $callback) {
return null;
}

return null;
return $callback(...$arguments);
}

protected function call(callable $callback, array $parameters = []): array
Expand Down