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 );
56 foreach ( $benchs as $key => $bench ) {
58 if ( is_callable( $bench ) ) {
59 $bench = [
'function' => $bench ];
63 if ( !isset( $bench[
'args'] ) ) {
68 if ( is_string( $key ) ) {
72 if ( is_array( $bench[
'function'] ) ) {
73 $class = $bench[
'function'][0];
74 if ( is_object( $class ) ) {
75 $class = get_class( $class );
77 $name = $class .
'::' . $bench[
'function'][1];
80 $name = strval( $bench[
'function'] );
82 $name = sprintf(
"%s(%s)",
87 static function ( $a ) {
88 return var_export( $a,
true );
97 $normBenchs[$name] = $bench;
100 foreach ( $normBenchs as $name => $bench ) {
102 if ( isset( $bench[
'setup'] ) ) {
103 call_user_func( $bench[
'setup'] );
107 $stat =
new RunningStat();
108 for ( $i = 0; $i < $count; $i++ ) {
110 if ( isset( $bench[
'setupEach'] ) ) {
111 $bench[
'setupEach']();
113 $t = microtime(
true );
115 call_user_func_array( $bench[
'function'], $bench[
'args'] );
116 $t = ( microtime(
true ) -
$t ) * 1000;
120 $stat->addObservation(
$t );
125 'count' => $stat->getCount(),
127 'rate' => $stat->getMean() == 0 ? INF : ( 1.0 / ( $stat->getMean() / 1000.0 ) ),
128 'total' => $stat->getMean() * $stat->getCount(),
129 'mean' => $stat->getMean(),
131 'stddev' => $stat->getStdDev(),
133 'mem' => memory_get_usage(
true ),
134 'mempeak' => memory_get_peak_usage(
true ),
142 sprintf(
"Running PHP version %s (%s) on %s %s %s\n\n",
153 $ret = sprintf(
"%s\n %' 6s: %d\n",
158 $ret .= sprintf(
" %' 6s: %8.1f/s\n",
162 foreach ( [
'total',
'mean',
'max',
'stddev' ] as $metric ) {
163 $ret .= sprintf(
" %' 6s: %8.2fms\n",
170 'mem' =>
'Current memory usage',
171 'mempeak' =>
'Peak memory usage'
172 ] as $key => $label ) {
173 $ret .= sprintf(
"%' 20s: %s\n",
175 $this->formatSize(
$res[
'usage'][$key] )
179 $this->
output(
"$ret\n" );
183 $this->
output( sprintf(
"#%3d - memory: %-10s - peak: %-10s\n",
185 $this->formatSize( memory_get_usage(
true ) ),
186 $this->formatSize( memory_get_peak_usage(
true ) )
200 private function formatSize( $bytes ): string {
201 if ( $bytes >= ( 1024 ** 3 ) ) {
202 return number_format( $bytes / ( 1024 ** 3 ), 2 ) .
' GiB';
204 if ( $bytes >= ( 1024 ** 2 ) ) {
205 return number_format( $bytes / ( 1024 ** 2 ), 2 ) .
' MiB';
207 if ( $bytes >= 1024 ) {
208 return number_format( $bytes / 1024, 1 ) .
' KiB';
210 return $bytes .
' B';
221 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.