Skip to content
Merged
Show file tree
Hide file tree
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
41 changes: 1 addition & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Or manually update `require-dev` block of `composer.json` and run `composer upda
```json
{
"require-dev": {
"dragon-code/benchmark": "^3.0"
"dragon-code/benchmark": "^4.0"
}
}
```
Expand Down Expand Up @@ -136,45 +136,6 @@ Benchmark::start()
);
```

### Without Data

If you want to see only the summary result of the run time without detailed information for each iteration, then you can
call the `withoutData` method, which will display only the
summary information:

```php
use DragonCode\Benchmark\Benchmark;

Benchmark::start()
->withoutData()
->compare([
'foo' => fn () => /* some code */,
'bar' => fn () => /* some code */,
]);
```

Result example:

```
------- ------------------- -------------------
# foo bar
------- ------------------- -------------------
min 12.02 ms - 58.4Kb 14.71 ms - 55.4Kb
max 15.66 ms - 64.8Kb 15.67 ms - 57.3Kb
avg 14.65 ms - 60.1Kb 15.17 ms - 56.2Kb
total 73.93 ms 76.31 ms
------- ------------------- -------------------
Order - 1 - - 2 -
------- ------------------- -------------------
```

> Note
>
> If the option to display detailed information is enabled (without using the `withoutData` method) and more than 1000
> iterations are requested, then the output of detailed
> information will be forcibly disabled, since there will be absolutely no point in it with a significantly increasing
> load on the computer.

### Round Precision

By default, the script does not round measurement results, but you can specify the number of decimal places to which
Expand Down
5 changes: 5 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Upgrading

## From 3 to 4

- Удалён метод `withoutData`
18 changes: 1 addition & 17 deletions src/Benchmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ class Benchmark

protected int $iterations = 100;

protected bool $withData = true;

protected ?Closure $prepare = null;

protected array $result = [
Expand Down Expand Up @@ -74,13 +72,6 @@ public function round(?int $precision): self
return $this;
}

public function withoutData(): self
{
$this->withData = false;

return $this;
}

public function compare(array|callable ...$callbacks): void
{
$values = is_array($callbacks[0]) ? $callbacks[0] : func_get_args();
Expand Down Expand Up @@ -155,17 +146,10 @@ protected function push(mixed $name, int $iteration, float $time, float $ram): v

protected function show(): void
{
$table = $this->withData() ? $this->transformer->forTime($this->result['each']) : [];

$stats = $this->transformer->forStats($this->result);
$winner = $this->transformer->forWinners($stats);

$this->view->table($this->transformer->merge($table, $stats, $winner));
}

protected function withData(): bool
{
return $this->withData && $this->iterations <= 1000;
$this->view->table($this->transformer->merge($stats, $winner));
}

protected function validate(mixed $callback): void
Expand Down
4 changes: 2 additions & 2 deletions tests/Benchmark/AsArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testIterations(): void
'bar' => fn () => $this->work(),
]);

$this->benchmark()->iterations(500)->withoutData()->compare([
$this->benchmark()->iterations(500)->compare([
'foo' => fn () => $this->work(),
'bar' => fn () => $this->work(),
]);
Expand All @@ -35,7 +35,7 @@ public function testIterations(): void

public function testWithoutData(): void
{
$this->benchmark()->withoutData()->compare([
$this->benchmark()->compare([
'foo' => fn () => $this->work(),
'bar' => fn () => $this->work(),
]);
Expand Down
4 changes: 2 additions & 2 deletions tests/Benchmark/AsCallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testIterations(): void
fn () => $this->work(),
);

$this->benchmark()->iterations(500)->withoutData()->compare(
$this->benchmark()->iterations(500)->compare(
fn () => $this->work(),
fn () => $this->work(),
);
Expand All @@ -35,7 +35,7 @@ public function testIterations(): void

public function testWithoutData(): void
{
$this->benchmark()->withoutData()->compare(
$this->benchmark()->compare(
fn () => $this->work(),
fn () => $this->work(),
);
Expand Down
4 changes: 2 additions & 2 deletions tests/Benchmark/FailureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ public function testAsPropertiesWithoutData(): void
$this->expectException(TypeError::class);
$this->expectExceptionMessage('must be of type callable|array, int given');

$this->benchmark()->withoutData()->compare(123);
$this->benchmark()->compare(123);
}

public function testAsArrayWithoutData(): void
{
$this->expectException(ValueIsNotCallableException::class);
$this->expectExceptionMessage('The array value must be of type callable, integer given.');

$this->benchmark()->withoutData()->compare([
$this->benchmark()->compare([
'first' => 123,
'second' => 123,
]);
Expand Down
4 changes: 0 additions & 4 deletions tests/Benchmark/PrepareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public function testCallback(): void

$this->benchmark()
->iterations(3)
->withoutData()
->prepare(function () use (&$result) {
$result[] = 1;
})
Expand All @@ -32,7 +31,6 @@ public function testParameters(): void

$this->benchmark()
->iterations(3)
->withoutData()
->prepare(function (mixed $name, int $iteration) use (&$result) {
$result[] = sprintf('%s:%d', $name, $iteration);
})
Expand All @@ -59,7 +57,6 @@ public function testName(): void

$this->benchmark()
->iterations(3)
->withoutData()
->prepare(function (mixed $name) use (&$result) {
$result[] = $name;
})
Expand All @@ -84,7 +81,6 @@ public function testPrepareResult(): void
{
$this->benchmark()
->iterations(3)
->withoutData()
->prepare(
fn (mixed $name, int $iteration) => sprintf('%s:%d', $name, $iteration)
)
Expand Down