@@ -27,6 +27,8 @@ class Benchmark
2727
2828 protected int $ deviations = 1 ;
2929
30+ protected int $ warmup = 0 ;
31+
3032 public function __construct (
3133 protected RunnerService $ runner = new RunnerService ,
3234 protected ViewService $ view = new ViewService ,
@@ -57,6 +59,19 @@ public function snapshots(string $directory): static
5759 return $ this ;
5860 }
5961
62+ /**
63+ * Specifies the number of warm-up iterations for each callback.
64+ *
65+ * @param int<1, max> $count
66+ * @return $this
67+ */
68+ public function warmup (int $ count = 1 ): static
69+ {
70+ $ this ->warmup = max (1 , abs ($ count ));
71+
72+ return $ this ;
73+ }
74+
6075 /**
6176 * Sets a callback to be executed before all iterations for each comparison.
6277 *
@@ -292,7 +307,10 @@ protected function withProgress(Closure $callback, int $total): void
292307 */
293308 protected function steps (array $ callbacks , int $ multiplier = 1 ): int
294309 {
295- return count ($ callbacks ) * $ this ->iterations * $ multiplier ;
310+ $ count = count ($ callbacks );
311+ $ warmup = $ count * $ this ->warmup ;
312+
313+ return $ count * $ this ->iterations * $ multiplier + $ warmup ;
296314 }
297315
298316 /**
@@ -321,14 +339,18 @@ protected function chunks(array $callbacks, ProgressBar $progressBar): void
321339 */
322340 protected function run (mixed $ name , Closure $ callback , ProgressBar $ progressBar ): void
323341 {
324- for ($ i = 1 ; $ i <= $ this ->iterations ; $ i ++) {
342+ $ warmedUp = $ this ->warmup === 0 ;
343+
344+ for ($ i = 1 ; $ i <= $ this ->iterations + $ this ->warmup ; $ i ++) {
325345 $ result = $ this ->callbacks ->performBeforeEach ($ name , $ i );
326346
327347 [$ time , $ memory ] = $ this ->call ($ callback , [$ i , $ result ]);
328348
329349 $ this ->callbacks ->performAfterEach ($ name , $ i , $ time , $ memory );
330350
331- $ this ->push ($ name , $ time , $ memory );
351+ if ($ warmedUp ) {
352+ $ this ->push ($ name , $ time , $ memory );
353+ }
332354
333355 $ progressBar ->advance ();
334356 }
0 commit comments