38 public function bench( array $benchs ) {
40 $count = $this->
getOption(
'count', $this->defaultCount );
47 foreach ( $benchs as $key => $bench ) {
49 if ( is_callable( $bench ) ) {
50 $bench = [
'function' => $bench ];
54 if ( !isset( $bench[
'args'] ) ) {
59 if ( is_string( $key ) ) {
62 if ( is_array( $bench[
'function'] ) ) {
63 $class = $bench[
'function'][0];
64 if ( is_object( $class ) ) {
65 $class = get_class( $class );
67 $name = $class .
'::' . $bench[
'function'][1];
69 $name = strval( $bench[
'function'] );
74 static fn ( $a ) => var_export( $a,
true ),
78 $index = $shortNames[$name] = ( $shortNames[$name] ?? 0 ) + 1;
79 $shorten = strlen( $argsText ) > 80 || str_contains( $argsText,
"\n" );
81 $name =
"$name($argsText)";
83 if ( $shorten || $index > 1 ) {
84 $name =
"$name@$index";
88 $normBenchs[$name] = $bench;
91 foreach ( $normBenchs as $name => $bench ) {
93 if ( isset( $bench[
'setup'] ) ) {
98 $stat =
new RunningStat();
99 for ( $i = 0; $i < $count; $i++ ) {
101 if ( isset( $bench[
'setupEach'] ) ) {
102 $bench[
'setupEach']();
104 $t = microtime(
true );
105 $bench[
'function']( ...$bench[
'args'] );
106 $t = ( microtime(
true ) - $t ) * 1000;
110 $stat->addObservation( $t );
115 'count' => $stat->getCount(),
117 'rate' => $stat->getMean() == 0 ? INF : ( 1.0 / ( $stat->getMean() / 1000.0 ) ),
118 'total' => $stat->getMean() * $stat->getCount(),
119 'mean' => $stat->getMean(),
121 'stddev' => $stat->getStdDev(),
123 'mem' => memory_get_usage(
true ),
124 'mempeak' => memory_get_peak_usage(
true ),