45 parent::__construct();
46 $this->
addOption(
'count',
"How many times to run a benchmark. Default: {$this->defaultCount}",
false,
true );
47 $this->
addOption(
'verbose',
'Verbose logging of resource usage',
false,
false,
'v' );
50 public function bench( array $benchs ) {
52 $count = $this->
getOption(
'count', $this->defaultCount );
59 foreach ( $benchs as $key => $bench ) {
61 if ( is_callable( $bench ) ) {
62 $bench = [
'function' => $bench ];
66 if ( !isset( $bench[
'args'] ) ) {
71 if ( is_string( $key ) ) {
75 if ( is_array( $bench[
'function'] ) ) {
76 $class = $bench[
'function'][0];
77 if ( is_object( $class ) ) {
78 $class = get_class( $class );
80 $name = $class .
'::' . $bench[
'function'][1];
83 $name = strval( $bench[
'function'] );
88 static function ( $a ) {
89 return var_export( $a,
true );
95 $index = $shortNames[$name] = ( $shortNames[$name] ?? 0 ) + 1;
96 $shorten = strlen( $argsText ) > 80 || str_contains( $argsText,
"\n" );
98 $name =
"$name($argsText)";
100 if ( $shorten || $index > 1 ) {
101 $name =
"$name@$index";
105 $normBenchs[$name] = $bench;
108 foreach ( $normBenchs as $name => $bench ) {
110 if ( isset( $bench[
'setup'] ) ) {
111 call_user_func( $bench[
'setup'] );
115 $stat =
new RunningStat();
116 for ( $i = 0; $i < $count; $i++ ) {
118 if ( isset( $bench[
'setupEach'] ) ) {
119 $bench[
'setupEach']();
121 $t = microtime(
true );
123 call_user_func_array( $bench[
'function'], $bench[
'args'] );
124 $t = ( microtime(
true ) - $t ) * 1000;
128 $stat->addObservation( $t );
133 'count' => $stat->getCount(),
135 'rate' => $stat->getMean() == 0 ? INF : ( 1.0 / ( $stat->getMean() / 1000.0 ) ),
136 'total' => $stat->getMean() * $stat->getCount(),
137 'mean' => $stat->getMean(),
139 'stddev' => $stat->getStdDev(),
141 'mem' => memory_get_usage(
true ),
142 'mempeak' => memory_get_peak_usage(
true ),
150 sprintf(
"Running PHP version %s (%s) on %s %s %s\n\n",
161 $ret = sprintf(
"%s\n %' 6s: %d\n",
166 $ret .= sprintf(
" %' 6s: %8.1f/s\n",
170 foreach ( [
'total',
'mean',
'max',
'stddev' ] as $metric ) {
171 $ret .= sprintf(
" %' 6s: %8.2fms\n",
178 'mem' =>
'Current memory usage',
179 'mempeak' =>
'Peak memory usage'
180 ] as $key => $label ) {
181 $ret .= sprintf(
"%' 20s: %s\n",
183 $this->formatSize( $res[
'usage'][$key] )
187 $this->
output(
"$ret\n" );
191 $this->
output( sprintf(
"#%3d - memory: %-10s - peak: %-10s\n",
193 $this->formatSize( memory_get_usage(
true ) ),
194 $this->formatSize( memory_get_peak_usage(
true ) )
208 private function formatSize( $bytes ): string {
209 if ( $bytes >= ( 1024 ** 3 ) ) {
210 return number_format( $bytes / ( 1024 ** 3 ), 2 ) .
' GiB';
212 if ( $bytes >= ( 1024 ** 2 ) ) {
213 return number_format( $bytes / ( 1024 ** 2 ), 2 ) .
' MiB';
215 if ( $bytes >= 1024 ) {
216 return number_format( $bytes / 1024, 1 ) .
' KiB';
218 return $bytes .
' B';
227 $content = file_get_contents( $file );
229 if ( str_starts_with( $content,
"\037\213" ) ) {
230 $content = gzdecode( $content );
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.