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 ) ) {
63 if ( is_array( $bench[
'function'] ) ) {
64 $class = $bench[
'function'][0];
65 if ( is_object( $class ) ) {
66 $class = get_class( $class );
68 $name = $class .
'::' . $bench[
'function'][1];
71 $name = strval( $bench[
'function'] );
76 static function ( $a ) {
77 return var_export( $a,
true );
83 $index = $shortNames[$name] = ( $shortNames[$name] ?? 0 ) + 1;
84 $shorten = strlen( $argsText ) > 80 || str_contains( $argsText,
"\n" );
86 $name =
"$name($argsText)";
88 if ( $shorten || $index > 1 ) {
89 $name =
"$name@$index";
93 $normBenchs[$name] = $bench;
96 foreach ( $normBenchs as $name => $bench ) {
98 if ( isset( $bench[
'setup'] ) ) {
103 $stat =
new RunningStat();
104 for ( $i = 0; $i < $count; $i++ ) {
106 if ( isset( $bench[
'setupEach'] ) ) {
107 $bench[
'setupEach']();
109 $t = microtime(
true );
111 $bench[
'function']( ...$bench[
'args'] );
112 $t = ( microtime(
true ) - $t ) * 1000;
116 $stat->addObservation( $t );
121 'count' => $stat->getCount(),
123 'rate' => $stat->getMean() == 0 ? INF : ( 1.0 / ( $stat->getMean() / 1000.0 ) ),
124 'total' => $stat->getMean() * $stat->getCount(),
125 'mean' => $stat->getMean(),
127 'stddev' => $stat->getStdDev(),
129 'mem' => memory_get_usage(
true ),
130 'mempeak' => memory_get_peak_usage(
true ),