55namespace DragonCode \Benchmark ;
66
77use Closure ;
8- use DragonCode \Benchmark \Exceptions \ValueIsNotCallableException ;
98use DragonCode \Benchmark \Services \AssertService ;
109use DragonCode \Benchmark \Services \CallbacksService ;
1110use DragonCode \Benchmark \Services \CollectorService ;
1615use DragonCode \Benchmark \View \ProgressBarView ;
1716
1817use function abs ;
19- use function array_first ;
2018use function count ;
21- use function is_array ;
22- use function is_callable ;
2319use function max ;
2420
2521class Benchmark
@@ -102,11 +98,7 @@ public function round(?int $precision): static
10298
10399 public function compare (array |Closure ...$ callbacks ): static
104100 {
105- $ this ->clear ();
106-
107- $ values = $ this ->resolveCallbacks ($ callbacks );
108-
109- $ this ->withProgress ($ values , $ this ->steps ($ values ));
101+ $ this ->callbacks ->compare (...$ callbacks );
110102
111103 return $ this ;
112104 }
@@ -116,6 +108,8 @@ public function compare(array|Closure ...$callbacks): static
116108 */
117109 public function toData (): array
118110 {
111+ $ this ->performCallbacks ();
112+
119113 return $ this ->result ->get (
120114 $ this ->collector ->all ()
121115 );
@@ -139,13 +133,25 @@ public function toAssert(): AssertService
139133 );
140134 }
141135
142- protected function withProgress (array $ callbacks , int $ count ): void
136+ protected function performCallbacks (): void
137+ {
138+ $ this ->clear ();
139+
140+ $ callbacks = $ this ->callbacks ->compare ;
141+
142+ $ this ->withProgress (
143+ callback: fn (ProgressBarView $ bar ) => $ this ->chunks ($ callbacks , $ bar ),
144+ total : $ this ->steps ($ callbacks )
145+ );
146+ }
147+
148+ protected function withProgress (Closure $ callback , int $ total ): void
143149 {
144150 $ this ->view ->emptyLine ();
145151
146- $ bar = $ this ->view ->progressBar ()->create ($ count );
152+ $ bar = $ this ->view ->progressBar ()->create ($ total );
147153
148- $ this -> chunks ( $ callbacks , $ bar );
154+ $ callback ( $ bar );
149155
150156 $ bar ->finish ();
151157 $ this ->view ->emptyLine (2 );
@@ -159,8 +165,6 @@ protected function steps(array $callbacks): int
159165 protected function chunks (array $ callbacks , ProgressBarView $ progressBar ): void
160166 {
161167 foreach ($ callbacks as $ name => $ callback ) {
162- $ this ->validate ($ callback );
163-
164168 $ this ->callbacks ->performBefore ($ name );
165169
166170 $ this ->run ($ name , $ callback , $ progressBar );
@@ -194,20 +198,6 @@ protected function push(mixed $name, float $time, float $memory): void
194198 $ this ->collector ->push ($ name , [$ time , $ memory ]);
195199 }
196200
197- protected function resolveCallbacks (array $ callbacks ): array
198- {
199- $ first = array_first ($ callbacks );
200-
201- return is_array ($ first ) ? $ first : $ callbacks ;
202- }
203-
204- protected function validate (mixed $ callback ): void
205- {
206- if (! is_callable ($ callback )) {
207- throw new ValueIsNotCallableException ($ callback );
208- }
209- }
210-
211201 protected function clear (): void
212202 {
213203 $ this ->result ->clear ();
0 commit comments