Skip to content

Commit 03cd1b0

Browse files
Merge pull request #104 from TheDragonCode/4.x
Add SilentProgressBar implementation and integrate progress bar toggle in ViewService
2 parents 05b40c6 + f0b28dd commit 03cd1b0

19 files changed

Lines changed: 177 additions & 191 deletions

src/Benchmark.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace DragonCode\Benchmark;
66

77
use Closure;
8+
use DragonCode\Benchmark\Contracts\ProgressBar;
89
use DragonCode\Benchmark\Exceptions\NoComparisonsException;
910
use DragonCode\Benchmark\Services\AssertService;
1011
use DragonCode\Benchmark\Services\CallbacksService;
@@ -14,7 +15,6 @@
1415
use DragonCode\Benchmark\Services\RunnerService;
1516
use DragonCode\Benchmark\Services\ViewService;
1617
use DragonCode\Benchmark\Transformers\ResultTransformer;
17-
use DragonCode\Benchmark\View\ProgressBarView;
1818

1919
use function abs;
2020
use function count;
@@ -48,6 +48,7 @@ public static function make(): static
4848
* Sets a callback to be executed before all iterations for each comparison.
4949
*
5050
* @param Closure(int|string $name): mixed $callback
51+
*
5152
* @return $this
5253
*/
5354
public function before(Closure $callback): static
@@ -61,6 +62,7 @@ public function before(Closure $callback): static
6162
* Sets a callback to be executed before each iteration.
6263
*
6364
* @param Closure(int|string $name, int<1, max> $iteration): mixed $callback
65+
*
6466
* @return $this
6567
*/
6668
public function beforeEach(Closure $callback): static
@@ -74,6 +76,7 @@ public function beforeEach(Closure $callback): static
7476
* Sets a callback to be executed after all iterations for each comparison.
7577
*
7678
* @param Closure(int|string $name): mixed $callback
79+
*
7780
* @return $this
7881
*/
7982
public function after(Closure $callback): static
@@ -87,6 +90,7 @@ public function after(Closure $callback): static
8790
* Sets a callback to be executed after each iteration.
8891
*
8992
* @param Closure(int|string $name, int<1, max> $iteration, float $time, float $memory): mixed $callback
93+
*
9094
* @return $this
9195
*/
9296
public function afterEach(Closure $callback): static
@@ -100,6 +104,7 @@ public function afterEach(Closure $callback): static
100104
* Sets the number of iterations for each comparison.
101105
*
102106
* @param int<1, max> $count
107+
*
103108
* @return $this
104109
*/
105110
public function iterations(int $count): static
@@ -113,6 +118,7 @@ public function iterations(int $count): static
113118
* Enables deviation calculation and sets the number of runs.
114119
*
115120
* @param int<2, max> $count
121+
*
116122
* @return $this
117123
*/
118124
public function deviations(int $count = 2): static
@@ -128,6 +134,7 @@ public function deviations(int $count = 2): static
128134
* Sets the rounding precision for time values.
129135
*
130136
* @param int<0, max>|null $precision The number of decimal places. Null means no rounding.
137+
*
131138
* @return $this
132139
*/
133140
public function round(?int $precision): static
@@ -144,7 +151,7 @@ public function round(?int $precision): static
144151
*/
145152
public function disableProgressBar(): static
146153
{
147-
$this->view->progressBar()->disable();
154+
$this->view->disable();
148155

149156
return $this;
150157
}
@@ -153,6 +160,7 @@ public function disableProgressBar(): static
153160
* Registers callback functions for comparison.
154161
*
155162
* @param array|Closure ...$callbacks Callback functions or an array of callback functions for comparison.
163+
*
156164
* @return $this
157165
*/
158166
public function compare(array|Closure ...$callbacks): static
@@ -234,7 +242,7 @@ protected function performCompare(): void
234242
$callbacks = $this->callbacks->compare;
235243

236244
$this->withProgress(
237-
callback: fn (ProgressBarView $bar) => $this->chunks($callbacks, $bar),
245+
callback: fn (ProgressBar $bar) => $this->chunks($callbacks, $bar),
238246
total : $this->steps($callbacks)
239247
);
240248
}
@@ -248,7 +256,7 @@ protected function performDeviation(): void
248256

249257
$callbacks = $this->callbacks->compare;
250258

251-
$this->withProgress(function (ProgressBarView $bar) use (&$results, $callbacks) {
259+
$this->withProgress(function (ProgressBar $bar) use (&$results, $callbacks) {
252260
for ($i = 1; $i <= $this->deviations; $i++) {
253261
$this->clear();
254262

@@ -298,9 +306,9 @@ protected function steps(array $callbacks, int $multiplier = 1): int
298306
* Executes all callback functions with before/after hooks.
299307
*
300308
* @param array $callbacks An array of callback functions.
301-
* @param ProgressBarView $progressBar The progress bar.
309+
* @param ProgressBar $progressBar The progress bar.
302310
*/
303-
protected function chunks(array $callbacks, ProgressBarView $progressBar): void
311+
protected function chunks(array $callbacks, ProgressBar $progressBar): void
304312
{
305313
foreach ($callbacks as $name => $callback) {
306314
$this->callbacks->performBefore($name);
@@ -316,9 +324,9 @@ protected function chunks(array $callbacks, ProgressBarView $progressBar): void
316324
*
317325
* @param mixed $name The callback name.
318326
* @param Closure $callback The callback function to execute.
319-
* @param ProgressBarView $progressBar The progress bar.
327+
* @param ProgressBar $progressBar The progress bar.
320328
*/
321-
protected function run(mixed $name, Closure $callback, ProgressBarView $progressBar): void
329+
protected function run(mixed $name, Closure $callback, ProgressBar $progressBar): void
322330
{
323331
for ($i = 1; $i <= $this->iterations; $i++) {
324332
$result = $this->callbacks->performBeforeEach($name, $i);
@@ -338,6 +346,7 @@ protected function run(mixed $name, Closure $callback, ProgressBarView $progress
338346
*
339347
* @param Closure $callback The callback function to execute.
340348
* @param array $parameters Parameters to pass to the callback.
349+
*
341350
* @return array An array [time in milliseconds, memory in bytes].
342351
*/
343352
protected function call(Closure $callback, array $parameters = []): array

src/Contracts/ProgressBar.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\Benchmark\Contracts;
6+
7+
interface ProgressBar
8+
{
9+
public function create(int $total): static;
10+
11+
public function advance(int $step = 1): void;
12+
13+
public function finish(): void;
14+
}

src/Services/ViewService.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,33 @@
44

55
namespace DragonCode\Benchmark\Services;
66

7+
use DragonCode\Benchmark\Contracts\ProgressBar;
78
use DragonCode\Benchmark\View\LineView;
89
use DragonCode\Benchmark\View\ProgressBarView;
10+
use DragonCode\Benchmark\View\SilentProgressBarView;
911
use DragonCode\Benchmark\View\TableView;
1012

1113
class ViewService
1214
{
15+
protected bool $enabled = true;
16+
1317
public function __construct(
1418
protected TableView $table = new TableView,
15-
protected ProgressBarView $progressBar = new ProgressBarView,
1619
protected LineView $line = new LineView,
20+
protected ProgressBarView $progressBar = new ProgressBarView,
21+
protected SilentProgressBarView $silentProgressBar = new SilentProgressBarView,
1722
) {}
1823

24+
/**
25+
* @return $this
26+
*/
27+
public function disable(): static
28+
{
29+
$this->enabled = false;
30+
31+
return $this;
32+
}
33+
1934
/**
2035
* Displays a table with benchmark results.
2136
*
@@ -29,9 +44,11 @@ public function table(array $data): void
2944
/**
3045
* Returns the progress bar instance.
3146
*/
32-
public function progressBar(): ProgressBarView
47+
public function progressBar(): ProgressBar
3348
{
34-
return $this->progressBar;
49+
return $this->enabled
50+
? $this->progressBar
51+
: $this->silentProgressBar;
3552
}
3653

3754
/**
@@ -41,7 +58,9 @@ public function progressBar(): ProgressBarView
4158
*/
4259
public function line(string $text): void
4360
{
44-
$this->line->line($text);
61+
if ($this->enabled) {
62+
$this->line->line($text);
63+
}
4564
}
4665

4766
/**
@@ -51,6 +70,8 @@ public function line(string $text): void
5170
*/
5271
public function emptyLine(int $count = 1): void
5372
{
54-
$this->line->newLine($count);
73+
if ($this->enabled) {
74+
$this->line->newLine($count);
75+
}
5576
}
5677
}

src/View/ProgressBarView.php

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,25 @@
44

55
namespace DragonCode\Benchmark\View;
66

7+
use DragonCode\Benchmark\Contracts\ProgressBar;
8+
79
use function floor;
810
use function max;
911
use function str_repeat;
1012

11-
class ProgressBarView extends View
13+
class ProgressBarView extends View implements ProgressBar
1214
{
13-
protected bool $enabled = true;
14-
1515
protected int $current = 0;
1616

1717
protected int $barWidth = 28;
1818

1919
protected int $total = 100;
2020

21-
/**
22-
* Disables the progress bar display.
23-
*
24-
* @return $this
25-
*/
26-
public function disable(): static
27-
{
28-
$this->enabled = false;
29-
30-
return $this;
31-
}
32-
3321
/**
3422
* Creates a progress bar with the specified total number of steps.
3523
*
3624
* @param int $total The total number of steps.
25+
*
3726
* @return $this
3827
*/
3928
public function create(int $total): static
@@ -66,20 +55,14 @@ public function finish(): void
6655

6756
$this->display();
6857

69-
if ($this->enabled) {
70-
$this->write(PHP_EOL);
71-
}
58+
$this->write(PHP_EOL);
7259
}
7360

7461
/**
7562
* Renders the current state of the progress bar.
7663
*/
7764
protected function display(): void
7865
{
79-
if (! $this->enabled) {
80-
return;
81-
}
82-
8366
$percent = $this->current / $this->total;
8467
$filled = (int) floor($percent * $this->barWidth);
8568
$empty = $this->barWidth - $filled;

src/View/SilentProgressBarView.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\Benchmark\View;
6+
7+
use DragonCode\Benchmark\Contracts\ProgressBar;
8+
9+
class SilentProgressBarView extends View implements ProgressBar
10+
{
11+
public function create(int $total): static
12+
{
13+
return $this;
14+
}
15+
16+
public function advance(int $step = 1): void {}
17+
18+
public function finish(): void {}
19+
}
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +0,0 @@
1-
2-
0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
3-
4-
5-
[INFO] No comparisons were made.

tests/.pest/snapshots/Unit/Result/DisabledProgressBar/to_assert.snap

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/.pest/snapshots/Unit/Result/DisabledProgressBar/to_console.snap

Lines changed: 0 additions & 13 deletions
This file was deleted.

tests/.pest/snapshots/Unit/Result/DisabledProgressBar/to_data.snap

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
2-
0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
3-
4-
5-
+-------+-------------------------------+------------------------------+
6-
| # | 0 | 1 |
7-
+-------+-------------------------------+------------------------------+
8-
| min | 15.67890123 ms - 202 bytes | 2.3456789 ms - 102 bytes |
9-
| max | 112.78901234 ms - 209 bytes | 9.75678901 ms - 109 bytes |
10-
| avg | 53.02524845125 ms - 205 bytes | 5.94290024625 ms - 105 bytes |
11-
| total | 424.20198761 ms - 1.61 KB | 47.54320197 ms - 844 bytes |
12-
+-------+-------------------------------+------------------------------+
13-
| order | 2 | 1 |
14-
+-------+-------------------------------+------------------------------+
1+
+-------+-------------------------------+------------------------------+
2+
| # | 0 | 1 |
3+
+-------+-------------------------------+------------------------------+
4+
| min | 15.67890123 ms - 202 bytes | 2.3456789 ms - 102 bytes |
5+
| max | 112.78901234 ms - 209 bytes | 9.75678901 ms - 109 bytes |
6+
| avg | 53.02524845125 ms - 205 bytes | 5.94290024625 ms - 105 bytes |
7+
| total | 424.20198761 ms - 1.61 KB | 47.54320197 ms - 844 bytes |
8+
+-------+-------------------------------+------------------------------+
9+
| order | 2 | 1 |
10+
+-------+-------------------------------+------------------------------+

0 commit comments

Comments
 (0)