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
23 changes: 14 additions & 9 deletions src/Benchmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace DragonCode\Benchmark;

use Closure;
use DragonCode\Benchmark\Exceptions\NoComparisonsException;
use DragonCode\Benchmark\Services\AssertService;
use DragonCode\Benchmark\Services\CallbacksService;
use DragonCode\Benchmark\Services\CollectorService;
Expand Down Expand Up @@ -133,22 +134,26 @@ public function toData(): array
return $this->mapResult();
}

public function toConsole(): static
public function toConsole(): void
{
$table = $this->transformer->toTable(
$this->toData()
);
if (! $data = $this->toData()) {
$this->view->line('[INFO] No comparisons were made.');

$this->view->table($table);
return;
}

return $this;
$this->view->table(
$this->transformer->toTable($data)
);
}

public function toAssert(): AssertService
{
return new AssertService(
$this->toData()
);
if (! $data = $this->toData()) {
throw new NoComparisonsException;
}

return new AssertService($data);
}

protected function perform(): void
Expand Down
15 changes: 15 additions & 0 deletions src/Exceptions/NoComparisonsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace DragonCode\Benchmark\Exceptions;

use RuntimeException;

class NoComparisonsException extends RuntimeException
{
public function __construct()
{
parent::__construct('Method "compare" was not called. No comparisons were made.');
}
}
5 changes: 5 additions & 0 deletions src/Services/ViewService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public function progressBar(): ProgressBarView
return $this->progressBar;
}

public function line(string $text): void
{
$this->line->line($text);
}

public function emptyLine(int $count = 1): void
{
$this->line->newLine($count);
Expand Down
5 changes: 5 additions & 0 deletions src/View/LineView.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

class LineView extends View
{
public function line(string $text): void
{
$this->writeLine($text);
}

public function newLine(int $count = 1): void
{
for ($i = 0; $i < $count; $i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%


[INFO] No comparisons were made.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%


┌───────┬────────────────────┬───────────────────┐
│ # │ 0 │ 1 │
├───────┼────────────────────┼───────────────────┤
│ min │ 16 ms - 202 bytes │ 2 ms - 102 bytes │
│ max │ 113 ms - 209 bytes │ 10 ms - 109 bytes │
│ avg │ 53 ms - 205 bytes │ 6 ms - 105 bytes │
│ total │ 424 ms - 1.61 KB │ 48 ms - 844 bytes │
├───────┼────────────────────┼───────────────────┤
│ order │ 2 │ 1 │
└───────┴────────────────────┴───────────────────┘
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%


┌───────┬───────────────────────┬──────────────────────┐
│ # │ 0 │ 1 │
├───────┼───────────────────────┼──────────────────────┤
│ min │ 15.68 ms - 202 bytes │ 2.35 ms - 102 bytes │
│ max │ 112.79 ms - 209 bytes │ 9.76 ms - 109 bytes │
│ avg │ 53.03 ms - 205 bytes │ 5.94 ms - 105 bytes │
│ total │ 424.2 ms - 1.61 KB │ 47.54 ms - 844 bytes │
├───────┼───────────────────────┼──────────────────────┤
│ order │ 2 │ 1 │
└───────┴───────────────────────┴──────────────────────┘
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%


┌───────┬──────────────────────────┬────────────────────────┐
│ # │ 0 │ 1 │
├───────┼──────────────────────────┼────────────────────────┤
│ min │ 15.6789 ms - 202 bytes │ 2.34568 ms - 102 bytes │
│ max │ 112.78901 ms - 209 bytes │ 9.75679 ms - 109 bytes │
│ avg │ 53.02525 ms - 205 bytes │ 5.9429 ms - 105 bytes │
│ total │ 424.20199 ms - 1.61 KB │ 47.5432 ms - 844 bytes │
├───────┼──────────────────────────┼────────────────────────┤
│ order │ 2 │ 1 │
└───────┴──────────────────────────┴────────────────────────┘
Expand Down
23 changes: 23 additions & 0 deletions tests/Unit/Fails/WithoutComparisonsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use DragonCode\Benchmark\Exceptions\NoComparisonsException;

describe('without comparisons', function () {
test('toData', function () {
$result = benchmark(false)->toData();

expect($result)->toBeEmpty();
});

test('toConsole', function () {
benchmark(false)->toConsole();

expectOutputToMatchSnapshot();
});

test('toAssert', function () {
benchmark(false)->toAssert();
})->throws(NoComparisonsException::class);
});