@@ -36,13 +36,22 @@ public function __construct(
3636 protected DeviationService $ deviation = new DeviationService ,
3737 ) {}
3838
39+ /**
40+ * Creates a new benchmark instance.
41+ *
42+ * @return static
43+ */
3944 public static function make (): static
4045 {
4146 return new static ;
4247 }
4348
4449 /**
50+ * Sets a callback to be executed before all iterations for each comparison.
51+ *
4552 * @param Closure(int|string $name): mixed $callback
53+ *
54+ * @return $this
4655 */
4756 public function before (Closure $ callback ): static
4857 {
@@ -52,7 +61,11 @@ public function before(Closure $callback): static
5261 }
5362
5463 /**
64+ * Sets a callback to be executed before each iteration.
65+ *
5566 * @param Closure(int|string $name, int<1, max> $iteration): mixed $callback
67+ *
68+ * @return $this
5669 */
5770 public function beforeEach (Closure $ callback ): static
5871 {
@@ -62,7 +75,11 @@ public function beforeEach(Closure $callback): static
6275 }
6376
6477 /**
78+ * Sets a callback to be executed after all iterations for each comparison.
79+ *
6580 * @param Closure(int|string $name): mixed $callback
81+ *
82+ * @return $this
6683 */
6784 public function after (Closure $ callback ): static
6885 {
@@ -72,7 +89,11 @@ public function after(Closure $callback): static
7289 }
7390
7491 /**
92+ * Sets a callback to be executed after each iteration.
93+ *
7594 * @param Closure(int|string $name, int<1, max> $iteration, float $time, float $memory): mixed $callback
95+ *
96+ * @return $this
7697 */
7798 public function afterEach (Closure $ callback ): static
7899 {
@@ -82,7 +103,11 @@ public function afterEach(Closure $callback): static
82103 }
83104
84105 /**
106+ * Sets the number of iterations for each comparison.
107+ *
85108 * @param int<1, max> $count
109+ *
110+ * @return $this
86111 */
87112 public function iterations (int $ count ): static
88113 {
@@ -92,7 +117,11 @@ public function iterations(int $count): static
92117 }
93118
94119 /**
120+ * Enables deviation calculation and sets the number of runs.
121+ *
95122 * @param int<2, max> $count
123+ *
124+ * @return $this
96125 */
97126 public function deviations (int $ count = 2 ): static
98127 {
@@ -104,7 +133,11 @@ public function deviations(int $count = 2): static
104133 }
105134
106135 /**
107- * @param int<0, max>|null $precision
136+ * Sets the rounding precision for time values.
137+ *
138+ * @param int<0, max>|null $precision The number of decimal places. Null means no rounding.
139+ *
140+ * @return $this
108141 */
109142 public function round (?int $ precision ): static
110143 {
@@ -113,13 +146,25 @@ public function round(?int $precision): static
113146 return $ this ;
114147 }
115148
149+ /**
150+ * Disables the progress bar display.
151+ *
152+ * @return $this
153+ */
116154 public function disableProgressBar (): static
117155 {
118156 $ this ->view ->progressBar ()->disable ();
119157
120158 return $ this ;
121159 }
122160
161+ /**
162+ * Registers callback functions for comparison.
163+ *
164+ * @param array|Closure ...$callbacks Callback functions or an array of callback functions for comparison.
165+ *
166+ * @return $this
167+ */
123168 public function compare (array |Closure ...$ callbacks ): static
124169 {
125170 $ this ->clear ();
@@ -130,6 +175,8 @@ public function compare(array|Closure ...$callbacks): static
130175 }
131176
132177 /**
178+ * Returns benchmark results as an array of data.
179+ *
133180 * @return Data\ResultData[]
134181 */
135182 public function toData (): array
@@ -141,6 +188,9 @@ public function toData(): array
141188 return $ this ->mapResult ();
142189 }
143190
191+ /**
192+ * Outputs benchmark results to the console as a table.
193+ */
144194 public function toConsole (): void
145195 {
146196 if (! $ data = $ this ->toData ()) {
@@ -154,6 +204,11 @@ public function toConsole(): void
154204 );
155205 }
156206
207+ /**
208+ * Returns the assertion service for performing result checks.
209+ *
210+ * @return AssertService
211+ */
157212 public function toAssert (): AssertService
158213 {
159214 if (! $ data = $ this ->toData ()) {
@@ -163,20 +218,31 @@ public function toAssert(): AssertService
163218 return new AssertService ($ data );
164219 }
165220
221+ /**
222+ * Performs the benchmark: simple comparison or with deviation calculation.
223+ */
166224 protected function perform (): void
167225 {
168226 $ this ->deviations === 1
169227 ? $ this ->performCompare ()
170228 : $ this ->performDeviation ();
171229 }
172230
231+ /**
232+ * Transforms collected data into an array of results.
233+ *
234+ * @return array
235+ */
173236 protected function mapResult (): array
174237 {
175238 return $ this ->result ->get (
176239 $ this ->collector ->all ()
177240 );
178241 }
179242
243+ /**
244+ * Performs a simple comparison of callback functions.
245+ */
180246 protected function performCompare (): void
181247 {
182248 $ callbacks = $ this ->callbacks ->compare ;
@@ -187,6 +253,9 @@ protected function performCompare(): void
187253 );
188254 }
189255
256+ /**
257+ * Performs a comparison with deviation calculation through multiple runs.
258+ */
190259 protected function performDeviation (): void
191260 {
192261 $ results = [];
@@ -210,6 +279,12 @@ protected function performDeviation(): void
210279 );
211280 }
212281
282+ /**
283+ * Wraps execution in a progress bar.
284+ *
285+ * @param Closure $callback The callback function to execute.
286+ * @param int $total The total number of progress bar steps.
287+ */
213288 protected function withProgress (Closure $ callback , int $ total ): void
214289 {
215290 $ this ->view ->emptyLine ();
@@ -222,11 +297,25 @@ protected function withProgress(Closure $callback, int $total): void
222297 $ this ->view ->emptyLine (2 );
223298 }
224299
300+ /**
301+ * Calculates the total number of steps for the progress bar.
302+ *
303+ * @param array $callbacks An array of callback functions.
304+ * @param int $multiplier The multiplier (number of runs).
305+ *
306+ * @return int
307+ */
225308 protected function steps (array $ callbacks , int $ multiplier = 1 ): int
226309 {
227310 return count ($ callbacks ) * $ this ->iterations * $ multiplier ;
228311 }
229312
313+ /**
314+ * Executes all callback functions with before/after hooks.
315+ *
316+ * @param array $callbacks An array of callback functions.
317+ * @param ProgressBarView $progressBar The progress bar.
318+ */
230319 protected function chunks (array $ callbacks , ProgressBarView $ progressBar ): void
231320 {
232321 foreach ($ callbacks as $ name => $ callback ) {
@@ -238,6 +327,13 @@ protected function chunks(array $callbacks, ProgressBarView $progressBar): void
238327 }
239328 }
240329
330+ /**
331+ * Executes all iterations for a single callback function.
332+ *
333+ * @param mixed $name The callback name.
334+ * @param Closure $callback The callback function to execute.
335+ * @param ProgressBarView $progressBar The progress bar.
336+ */
241337 protected function run (mixed $ name , Closure $ callback , ProgressBarView $ progressBar ): void
242338 {
243339 for ($ i = 1 ; $ i <= $ this ->iterations ; $ i ++) {
@@ -253,16 +349,34 @@ protected function run(mixed $name, Closure $callback, ProgressBarView $progress
253349 }
254350 }
255351
352+ /**
353+ * Calls a callback function and returns the measurement results.
354+ *
355+ * @param Closure $callback The callback function to execute.
356+ * @param array $parameters Parameters to pass to the callback.
357+ *
358+ * @return array An array [time in milliseconds, memory in bytes].
359+ */
256360 protected function call (Closure $ callback , array $ parameters = []): array
257361 {
258362 return $ this ->runner ->call ($ callback , $ parameters );
259363 }
260364
365+ /**
366+ * Stores measurement results in the collector.
367+ *
368+ * @param mixed $name The callback name.
369+ * @param float $time Execution time is specified in milliseconds.
370+ * @param float $memory Memory usage is specified in bytes.
371+ */
261372 protected function push (mixed $ name , float $ time , float $ memory ): void
262373 {
263374 $ this ->collector ->push ($ name , [$ time , $ memory ]);
264375 }
265376
377+ /**
378+ * Clears results and collected data.
379+ */
266380 protected function clear (): void
267381 {
268382 $ this ->result ->clear ();
0 commit comments