66
77use Bakame \Stackwatch \Exporter \CallbackDumper ;
88use Bakame \Stackwatch \Exporter \ViewExporter ;
9+ use Generator ;
910use Throwable ;
1011
1112use function gc_collect_cycles ;
@@ -69,7 +70,7 @@ public static function call(callable $callback, ?string $label = null): Result
6970 *
7071 * @throws InvalidArgument|Throwable
7172 */
72- public static function dumpCall (callable $ callback , ?string $ label = null ): Result
73+ public static function callDump (callable $ callback , ?string $ label = null ): Result
7374 {
7475 $ result = self ::call ($ callback , $ label );
7576 $ result ->span ->dump ();
@@ -84,26 +85,11 @@ public static function dumpCall(callable $callback, ?string $label = null): Resu
8485 *
8586 * @throws InvalidArgument|Throwable
8687 */
87- public static function ddCall (callable $ callback , ?string $ label = null ): never
88+ public static function callDd (callable $ callback , ?string $ label = null ): never
8889 {
8990 self ::call ($ callback , $ label )->span ->dd ();
9091 }
9192
92- /**
93- * Returns aggregated metrics associated with the callback.
94- *
95- * The aggregation mode (average, median, …) is controlled by $aggregatorMode.
96- *
97- * @param int<0, max> $warmup
98- * @param int<1, max> $iterations
99- *
100- * @throws InvalidArgument|Throwable
101- */
102- public static function benchmark (callable $ callback , int $ iterations = 1 , int $ warmup = 0 , AggregationType $ type = AggregationType::Average): AggregatedMetrics
103- {
104- return self ::report ($ callback , $ iterations , $ warmup )->column ($ type );
105- }
106-
10793 /**
10894 * Returns the metrics associated with the callback.
10995 *
@@ -117,12 +103,14 @@ public static function report(callable $callback, int $iterations = 1, int $warm
117103 self ::assertItCanBeRun ($ iterations , $ warmup );
118104 self ::warmup ($ warmup , $ callback );
119105 gc_collect_cycles ();
120- $ metrics = [];
121- for ($ i = 0 ; $ i < $ iterations ; ++$ i ) {
122- $ metrics [] = self ::call ($ callback )->span ->metrics ;
123- }
124106
125- return Report::fromMetrics (...$ metrics );
107+ return Report::fromMetrics (
108+ ...(function () use ($ iterations , $ callback ): Generator {
109+ for ($ i = 0 ; $ i < $ iterations ; ++$ i ) {
110+ yield self ::call ($ callback )->span ->metrics ;
111+ }
112+ })()
113+ );
126114 }
127115
128116 /**
@@ -133,7 +121,7 @@ public static function report(callable $callback, int $iterations = 1, int $warm
133121 *
134122 * @throws Throwable
135123 */
136- public static function dumpReport (callable $ callback , int $ iterations = 1 , int $ warmup = 0 ): Report
124+ public static function reportDump (callable $ callback , int $ iterations = 1 , int $ warmup = 0 ): Report
137125 {
138126 $ stats = self ::report ($ callback , $ iterations , $ warmup );
139127
@@ -146,6 +134,32 @@ public static function dumpReport(callable $callback, int $iterations = 1, int $
146134 return $ stats ;
147135 }
148136
137+ /**
138+ * Profile a callable, dump the stats to console and die.
139+ *
140+ * @param int<1, max> $iterations
141+ * @param int<0, max> $warmup
142+ */
143+ public static function reportDd (callable $ callback , int $ iterations = 1 , int $ warmup = 0 ): never
144+ {
145+ CallbackDumper::dd (fn (): Report => self ::reportDump ($ callback , $ iterations , $ warmup ));
146+ }
147+
148+ /**
149+ * Returns aggregated metrics associated with the callback.
150+ *
151+ * The aggregation mode (average, median, …) is controlled by $aggregatorMode.
152+ *
153+ * @param int<0, max> $warmup
154+ * @param int<1, max> $iterations
155+ *
156+ * @throws InvalidArgument|Throwable
157+ */
158+ public static function bench (callable $ callback , int $ iterations = 1 , int $ warmup = 0 , AggregationType $ type = AggregationType::Average): AggregatedMetrics
159+ {
160+ return self ::report ($ callback , $ iterations , $ warmup )->column ($ type );
161+ }
162+
149163 /**
150164 * Profile a callable and dump the stats to console.
151165 *
@@ -154,9 +168,9 @@ public static function dumpReport(callable $callback, int $iterations = 1, int $
154168 *
155169 * @throws Throwable
156170 */
157- public static function dumpBenchmark (callable $ callback , int $ iterations = 1 , int $ warmup = 0 , AggregationType $ type = AggregationType::Average): AggregatedMetrics
171+ public static function benchDump (callable $ callback , int $ iterations = 1 , int $ warmup = 0 , AggregationType $ type = AggregationType::Average): AggregatedMetrics
158172 {
159- $ stats = self ::benchmark ($ callback , $ iterations , $ warmup , $ type );
173+ $ stats = self ::bench ($ callback , $ iterations , $ warmup , $ type );
160174
161175 (new ViewExporter ())->exportStack (
162176 $ stats ,
@@ -173,19 +187,8 @@ public static function dumpBenchmark(callable $callback, int $iterations = 1, in
173187 * @param int<1, max> $iterations
174188 * @param int<0, max> $warmup
175189 */
176- public static function ddReport (callable $ callback , int $ iterations = 1 , int $ warmup = 0 ): never
177- {
178- CallbackDumper::dd (fn (): Report => self ::dumpReport ($ callback , $ iterations , $ warmup ));
179- }
180-
181- /**
182- * Profile a callable, dump the stats to console and die.
183- *
184- * @param int<1, max> $iterations
185- * @param int<0, max> $warmup
186- */
187- public static function ddBenchmark (callable $ callback , int $ iterations = 1 , int $ warmup = 0 , AggregationType $ type = AggregationType::Average): never
190+ public static function benchDd (callable $ callback , int $ iterations = 1 , int $ warmup = 0 , AggregationType $ type = AggregationType::Average): never
188191 {
189- CallbackDumper::dd (fn (): AggregatedMetrics => self ::dumpBenchmark ($ callback , $ iterations , $ warmup , $ type ));
192+ CallbackDumper::dd (fn (): AggregatedMetrics => self ::benchDump ($ callback , $ iterations , $ warmup , $ type ));
190193 }
191194}
0 commit comments