Merge pull request #109 from runcommand/round-time-no-name-column

Ensure time is rounded when there's no name column present
This commit is contained in:
Daniel Bachhuber 2016-10-26 06:53:55 -07:00 committed by GitHub
commit c2268124f4
7 changed files with 45 additions and 41 deletions

View file

@ -11,10 +11,10 @@ Feature: Profile arbitary file execution
runcommand_do_nothing();
"""
When I run `wp profile eval-file lame-function.php`
When I run `wp profile eval-file lame-function.php --fields=query_time,query_count,cache_ratio,cache_hits,cache_misses,request_time,request_count`
Then STDOUT should be a table containing rows:
| time | query_time | query_count | cache_ratio | cache_hits | cache_misses | request_time | request_count |
| 0s | 0s | 0 | | 0 | 0 | 0s | 0 |
| query_time | query_count | cache_ratio | cache_hits | cache_misses | request_time | request_count |
| 0s | 0 | | 0 | 0 | 0s | 0 |
Scenario: Profile a function that makes one HTTP request
Given a WP install

View file

@ -10,10 +10,10 @@ Feature: Profile arbitary code execution
}
"""
When I run `wp profile eval 'runcommand_do_nothing();'`
When I run `wp profile eval 'runcommand_do_nothing();' --fields=query_time,query_count,cache_ratio,cache_hits,cache_misses,request_time,request_count`
Then STDOUT should be a table containing rows:
| time | query_time | query_count | cache_ratio | cache_hits | cache_misses | request_time | request_count |
| 0s | 0s | 0 | | 0 | 0 | 0s | 0 |
| query_time | query_count | cache_ratio | cache_hits | cache_misses | request_time | request_count |
| 0s | 0 | | 0 | 0 | 0s | 0 |
Scenario: Profile a function that makes one HTTP request
Given a WP install
@ -26,12 +26,12 @@ Feature: Profile arbitary code execution
Scenario: Profile calls to the object cache
Given a WP install
When I run `wp profile eval 'wp_cache_get( "foo" );' --fields=cache_hits,cache_misses`
When I run `wp profile eval 'wp_cache_get( "foo" );' --fields=time,cache_hits,cache_misses`
Then STDOUT should be a table containing rows:
| cache_hits | cache_misses |
| 0 | 1 |
| time | cache_hits | cache_misses |
| 0s | 0 | 1 |
When I run `wp profile eval 'wp_cache_set( "foo", "bar" ); wp_cache_get( "foo" ); wp_cache_get( "foo" );' --fields=cache_hits,cache_misses`
When I run `wp profile eval 'wp_cache_set( "foo", "bar" ); wp_cache_get( "foo" ); wp_cache_get( "foo" );' --fields=time,cache_hits,cache_misses`
Then STDOUT should be a table containing rows:
| cache_hits | cache_misses |
| 2 | 0 |
| time | cache_hits | cache_misses |
| 0s | 2 | 0 |

View file

@ -48,7 +48,7 @@ Feature: Profile a specific hook
When I run `wp profile hook setup_theme --fields=callback`
Then STDOUT should be a table containing rows:
| callback |
| total |
| total (0) |
And STDERR should be empty
Scenario: Profile a hook that has actions with output
@ -78,7 +78,7 @@ Feature: Profile a specific hook
| callback | cache_hits | cache_misses |
| runcommand_shutdown_hook() | 0 | 1 |
| wp_ob_end_flush_all() | 0 | 0 |
| total | 0 | 1 |
| total (2) | 0 | 1 |
And STDERR should be empty
Scenario: Indicate where a callback is defined with profiling a hook
@ -97,7 +97,7 @@ Feature: Profile a specific hook
Then STDOUT should be a table containing rows:
| callback | location | cache_hits | cache_misses |
| runcommand_custom_action_hook() | mu-plugins/custom-action.php:2 | 0 | 1 |
| total | | 0 | 1 |
| total (1) | | 0 | 1 |
And STDERR should be empty
Scenario: Hooks should only be called once

View file

@ -29,7 +29,7 @@ Feature: Profile the template render stage
| wp_loaded:before |
| wp_loaded |
| wp_loaded:after |
| total |
| total (13) |
When I run `wp profile stage main_query --fields=hook`
Then STDOUT should be a table containing rows:
@ -45,7 +45,7 @@ Feature: Profile the template render stage
| wp:before |
| wp |
| wp:after |
| total |
| total (11) |
When I run `wp profile stage template --fields=hook`
Then STDOUT should be a table containing rows:
@ -63,7 +63,7 @@ Feature: Profile the template render stage
| wp_footer:before |
| wp_footer |
| wp_footer:after |
| total |
| total (13) |
Scenario: Use --all flag to profile all stages
Given a WP install
@ -106,7 +106,7 @@ Feature: Profile the template render stage
| wp_footer:before |
| wp_footer |
| wp_footer:after |
| total |
| total (35) |
Scenario: Invalid stage specified
Given a WP install

View file

@ -39,7 +39,7 @@ Feature: Basic profile usage
When I run `wp profile hook setup_theme --fields=callback,time`
Then STDOUT should be a table containing rows:
| callback | time |
| total | |
| total (0) | |
And STDERR should be empty
Scenario: Trailingslash provided URL to avoid canonical redirect
@ -49,7 +49,7 @@ Feature: Basic profile usage
Then STDERR should be empty
And STDOUT should be a table containing rows:
| callback | time |
| total | |
| total (0) | |
Scenario: Don't include 'total' cell when the name column is omitted
Given a WP install

View file

@ -218,7 +218,7 @@ class Command {
'request_count',
);
$formatter = new Formatter( $assoc_args, $fields );
$formatter->display_items( array( $logger ) );
$formatter->display_items( array( $logger ), false );
}
/**
@ -276,7 +276,7 @@ class Command {
'request_count',
);
$formatter = new Formatter( $assoc_args, $fields );
$formatter->display_items( array( $logger ) );
$formatter->display_items( array( $logger ), false );
}
/**

View file

@ -27,7 +27,9 @@ class Formatter {
$format_args['fields'] = explode( ',', $format_args['fields'] );
}
$this->total_cell_index = array_search( $fields[0], $format_args['fields'] );
if ( 'time' !== $fields[0] ) {
$this->total_cell_index = array_search( $fields[0], $format_args['fields'] );
}
$format_args['fields'] = array_map( 'trim', $format_args['fields'] );
@ -40,9 +42,9 @@ class Formatter {
*
* @param array $items
*/
public function display_items( $items ) {
public function display_items( $items, $include_total = true ) {
if ( 'table' === $this->args['format'] && empty( $this->args['field'] ) ) {
$this->show_table( $items, $this->args['fields'] );
$this->show_table( $items, $this->args['fields'], $include_total );
} else {
$this->formatter->display_items( $items );
}
@ -54,7 +56,7 @@ class Formatter {
* @param array $items
* @param array $fields
*/
private function show_table( $items, $fields ) {
private function show_table( $items, $fields, $include_total ) {
$table = new \cli\Table();
$enabled = \cli\Colors::shouldColorize();
@ -66,7 +68,7 @@ class Formatter {
$totals = array_fill( 0, count( $fields ), null );
if ( ! is_null( $this->total_cell_index ) ) {
$totals[ $this->total_cell_index ] = 'total';
$totals[ $this->total_cell_index ] = 'total (' . count( $items ) . ')';
}
$location_index = array_search( 'location', $fields );
foreach ( $items as $item ) {
@ -101,22 +103,24 @@ class Formatter {
}
$table->addRow( $values );
}
foreach( $totals as $i => $value ) {
if ( null === $value ) {
continue;
}
if ( stripos( $fields[ $i ], '_time' ) || 'time' === $fields[ $i ] ) {
$totals[ $i ] = round( $value, 4 ) . 's';
}
if ( is_array( $value ) ) {
if ( ! empty( $value ) ) {
$totals[ $i ] = round( ( array_sum( $value ) / count( $value ) ), 2 ) . '%';
} else {
$totals[ $i ] = null;
if ( $include_total ) {
foreach( $totals as $i => $value ) {
if ( null === $value ) {
continue;
}
if ( stripos( $fields[ $i ], '_time' ) || 'time' === $fields[ $i ] ) {
$totals[ $i ] = round( $value, 4 ) . 's';
}
if ( is_array( $value ) ) {
if ( ! empty( $value ) ) {
$totals[ $i ] = round( ( array_sum( $value ) / count( $value ) ), 2 ) . '%';
} else {
$totals[ $i ] = null;
}
}
}
$table->setFooters( $totals );
}
$table->setFooters( $totals );
foreach( $table->getDisplayLines() as $line ) {
\WP_CLI::line( $line );