52 public function bench( array $benchs ) {
54 $count = $this->
getOption(
'count', $this->defaultCount );
61 foreach ( $benchs as $key => $bench ) {
63 if ( is_callable( $bench ) ) {
64 $bench = [
'function' => $bench ];
68 if ( !isset( $bench[
'args'] ) ) {
73 if ( is_string( $key ) ) {
77 if ( is_array( $bench[
'function'] ) ) {
78 $class = $bench[
'function'][0];
79 if ( is_object( $class ) ) {
80 $class = get_class( $class );
82 $name = $class .
'::' . $bench[
'function'][1];
85 $name = strval( $bench[
'function'] );
90 static function ( $a ) {
91 return var_export( $a,
true );
97 $index = $shortNames[$name] = ( $shortNames[$name] ?? 0 ) + 1;
98 $shorten = strlen( $argsText ) > 80 || str_contains( $argsText,
"\n" );
100 $name =
"$name($argsText)";
102 if ( $shorten || $index > 1 ) {
103 $name =
"$name@$index";
107 $normBenchs[$name] = $bench;
110 foreach ( $normBenchs as $name => $bench ) {
112 if ( isset( $bench[
'setup'] ) ) {
113 call_user_func( $bench[
'setup'] );
117 $stat =
new RunningStat();
118 for ( $i = 0; $i < $count; $i++ ) {
120 if ( isset( $bench[
'setupEach'] ) ) {
121 $bench[
'setupEach']();
123 $t = microtime(
true );
125 call_user_func_array( $bench[
'function'], $bench[
'args'] );
126 $t = ( microtime(
true ) - $t ) * 1000;
130 $stat->addObservation( $t );
135 'count' => $stat->getCount(),
137 'rate' => $stat->getMean() == 0 ? INF : ( 1.0 / ( $stat->getMean() / 1000.0 ) ),
138 'total' => $stat->getMean() * $stat->getCount(),
139 'mean' => $stat->getMean(),
141 'stddev' => $stat->getStdDev(),
143 'mem' => memory_get_usage(
true ),
144 'mempeak' => memory_get_peak_usage(
true ),