44 parent::__construct();
45 $this->
addOption(
'count',
"How many times to run a benchmark. Default: {$this->defaultCount}",
false,
true );
46 $this->
addOption(
'verbose',
'Verbose logging of resource usage',
false,
false,
'v' );
49 public function bench( array $benchs ) {
51 $count = $this->
getOption(
'count', $this->defaultCount );
58 foreach ( $benchs as $key => $bench ) {
60 if ( is_callable( $bench ) ) {
61 $bench = [
'function' => $bench ];
65 if ( !isset( $bench[
'args'] ) ) {
70 if ( is_string( $key ) ) {
74 if ( is_array( $bench[
'function'] ) ) {
75 $class = $bench[
'function'][0];
76 if ( is_object( $class ) ) {
77 $class = get_class( $class );
79 $name = $class .
'::' . $bench[
'function'][1];
82 $name = strval( $bench[
'function'] );
87 static function ( $a ) {
88 return var_export( $a,
true );
94 $index = $shortNames[$name] = ( $shortNames[$name] ?? 0 ) + 1;
95 $shorten = strlen( $argsText ) > 80 || str_contains( $argsText,
"\n" );
97 $name =
"$name($argsText)";
99 if ( $shorten || $index > 1 ) {
100 $name =
"$name@$index";
104 $normBenchs[$name] = $bench;
107 foreach ( $normBenchs as $name => $bench ) {
109 if ( isset( $bench[
'setup'] ) ) {
110 call_user_func( $bench[
'setup'] );
114 $stat =
new RunningStat();
115 for ( $i = 0; $i < $count; $i++ ) {
117 if ( isset( $bench[
'setupEach'] ) ) {
118 $bench[
'setupEach']();
120 $t = microtime(
true );
122 call_user_func_array( $bench[
'function'], $bench[
'args'] );
123 $t = ( microtime(
true ) -
$t ) * 1000;
127 $stat->addObservation(
$t );
132 'count' => $stat->getCount(),
134 'rate' => $stat->getMean() == 0 ? INF : ( 1.0 / ( $stat->getMean() / 1000.0 ) ),
135 'total' => $stat->getMean() * $stat->getCount(),
136 'mean' => $stat->getMean(),
138 'stddev' => $stat->getStdDev(),
140 'mem' => memory_get_usage(
true ),
141 'mempeak' => memory_get_peak_usage(
true ),
149 sprintf(
"Running PHP version %s (%s) on %s %s %s\n\n",
160 $ret = sprintf(
"%s\n %' 6s: %d\n",
165 $ret .= sprintf(
" %' 6s: %8.1f/s\n",
169 foreach ( [
'total',
'mean',
'max',
'stddev' ] as $metric ) {
170 $ret .= sprintf(
" %' 6s: %8.2fms\n",
177 'mem' =>
'Current memory usage',
178 'mempeak' =>
'Peak memory usage'
179 ] as $key => $label ) {
180 $ret .= sprintf(
"%' 20s: %s\n",
182 $this->formatSize(
$res[
'usage'][$key] )
186 $this->
output(
"$ret\n" );
190 $this->
output( sprintf(
"#%3d - memory: %-10s - peak: %-10s\n",
192 $this->formatSize( memory_get_usage(
true ) ),
193 $this->formatSize( memory_get_peak_usage(
true ) )
207 private function formatSize( $bytes ): string {
208 if ( $bytes >= ( 1024 ** 3 ) ) {
209 return number_format( $bytes / ( 1024 ** 3 ), 2 ) .
' GiB';
211 if ( $bytes >= ( 1024 ** 2 ) ) {
212 return number_format( $bytes / ( 1024 ** 2 ), 2 ) .
' MiB';
214 if ( $bytes >= 1024 ) {
215 return number_format( $bytes / 1024, 1 ) .
' KiB';
217 return $bytes .
' B';
228 if ( substr(
$content, 0, 2 ) ===
"\037\213" ) {
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.