66
77use Closure ;
88use DragonCode \Benchmark \Exceptions \ValueIsNotCallableException ;
9+ use DragonCode \Benchmark \Services \Callbacks ;
910use DragonCode \Benchmark \Services \Runner ;
1011use DragonCode \Benchmark \Services \View ;
1112use DragonCode \Benchmark \Transformers \Transformer ;
12- use Symfony \Component \Console \Helper \ProgressBar as ProgressBarService ;
13- use Symfony \Component \Console \Input \ArgvInput ;
14- use Symfony \Component \Console \Output \ConsoleOutput ;
15- use Symfony \Component \Console \Style \SymfonyStyle ;
13+ use DragonCode \Benchmark \View \ProgressBarView ;
1614
1715use function count ;
1816use function func_get_args ;
2321
2422class Benchmark
2523{
26- protected View $ view ;
27-
2824 protected int $ iterations = 100 ;
2925
30- protected ?Closure $ beforeEach = null ;
31-
32- protected ?Closure $ afterEach = null ;
33-
3426 protected array $ result = [
3527 'each ' => [],
3628 'total ' => [],
3729 ];
3830
3931 public function __construct (
4032 protected Runner $ runner = new Runner ,
41- protected Transformer $ transformer = new Transformer
42- ) {
43- $ this ->view = new View (
44- new SymfonyStyle (
45- new ArgvInput ,
46- new ConsoleOutput
47- )
48- );
49- }
33+ protected Transformer $ transformer = new Transformer ,
34+ protected View $ view = new View ,
35+ protected Callbacks $ callbacks = new Callbacks ,
36+ ) {}
5037
5138 public static function make (): static
5239 {
@@ -55,14 +42,14 @@ public static function make(): static
5542
5643 public function beforeEach (Closure $ callback ): self
5744 {
58- $ this ->beforeEach = $ callback ;
45+ $ this ->callbacks -> beforeEach = $ callback ;
5946
6047 return $ this ;
6148 }
6249
6350 public function afterEach (Closure $ callback ): self
6451 {
65- $ this ->afterEach = $ callback ;
52+ $ this ->callbacks -> afterEach = $ callback ;
6653
6754 return $ this ;
6855 }
@@ -81,14 +68,23 @@ public function round(?int $precision): self
8168 return $ this ;
8269 }
8370
84- public function compare (array |Closure ...$ callbacks ): void
71+ public function compare (array |Closure ...$ callbacks ): static
8572 {
8673 $ values = is_array ($ callbacks [0 ]) ? $ callbacks [0 ] : func_get_args ();
8774
8875 $ this ->withProgress ($ values , $ this ->stepsCount ($ values ));
8976 $ this ->show ();
77+
78+ return $ this ;
9079 }
9180
81+ /**
82+ * @return \DragonCode\Benchmark\Data\ResultData[]
83+ */
84+ public function toData (): array {}
85+
86+ public function toConsole (): void {}
87+
9288 protected function withProgress (array $ callbacks , int $ count ): void
9389 {
9490 $ bar = $ this ->view ->progressBar ()->create ($ count );
@@ -104,7 +100,7 @@ protected function stepsCount(array $callbacks): int
104100 return count ($ callbacks ) * $ this ->iterations ;
105101 }
106102
107- protected function chunks (array $ callbacks , ProgressBarService $ progressBar ): void
103+ protected function chunks (array $ callbacks , ProgressBarView $ progressBar ): void
108104 {
109105 foreach ($ callbacks as $ name => $ callback ) {
110106 $ this ->validate ($ callback );
@@ -113,37 +109,28 @@ protected function chunks(array $callbacks, ProgressBarService $progressBar): vo
113109 }
114110 }
115111
116- protected function each (mixed $ name , Closure $ callback , ProgressBarService $ progressBar ): void
112+ protected function each (mixed $ name , Closure $ callback , ProgressBarView $ progressBar ): void
117113 {
118114 $ this ->result ['total ' ][$ name ] = $ this ->call (
119115 fn () => $ this ->run ($ name , $ callback , $ progressBar )
120116 );
121117 }
122118
123- protected function run (mixed $ name , Closure $ callback , ProgressBarService $ progressBar ): void
119+ protected function run (mixed $ name , Closure $ callback , ProgressBarView $ progressBar ): void
124120 {
125121 for ($ i = 1 ; $ i <= $ this ->iterations ; ++$ i ) {
126- $ result = $ this ->runCallback ( $ this -> beforeEach , $ name , $ i );
122+ $ result = $ this ->callbacks -> performBeforeEach ( $ name , $ i );
127123
128124 [$ time , $ ram ] = $ this ->call ($ callback , [$ i , $ result ]);
129125
130- $ this ->runCallback ( $ this -> afterEach , $ name , $ i , $ time , $ ram );
126+ $ this ->callbacks -> performAfterEach ( $ name , $ i , $ time , $ ram );
131127
132128 $ this ->push ($ name , $ i , $ time , $ ram );
133129
134130 $ progressBar ->advance ();
135131 }
136132 }
137133
138- protected function runCallback (?Closure $ callback , mixed ...$ arguments ): mixed
139- {
140- if (! $ callback ) {
141- return null ;
142- }
143-
144- return $ callback (...$ arguments );
145- }
146-
147134 protected function call (Closure $ callback , array $ parameters = []): array
148135 {
149136 return $ this ->runner ->call ($ callback , $ parameters );
0 commit comments