diff --git a/features/profile-eval-file.feature b/features/profile-eval-file.feature index aeb3dc9..0d6dd1f 100644 --- a/features/profile-eval-file.feature +++ b/features/profile-eval-file.feature @@ -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 diff --git a/features/profile-eval.feature b/features/profile-eval.feature index a777b5d..883fbeb 100644 --- a/features/profile-eval.feature +++ b/features/profile-eval.feature @@ -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 | diff --git a/features/profile-hook.feature b/features/profile-hook.feature index 32a632c..461a711 100644 --- a/features/profile-hook.feature +++ b/features/profile-hook.feature @@ -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 diff --git a/features/profile-stage.feature b/features/profile-stage.feature index e0f8e57..a91085e 100644 --- a/features/profile-stage.feature +++ b/features/profile-stage.feature @@ -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 diff --git a/features/profile.feature b/features/profile.feature index e6e22b9..9cda037 100644 --- a/features/profile.feature +++ b/features/profile.feature @@ -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 diff --git a/inc/class-command.php b/inc/class-command.php index b3a361e..2d6c830 100644 --- a/inc/class-command.php +++ b/inc/class-command.php @@ -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 ); } /** diff --git a/inc/class-formatter.php b/inc/class-formatter.php index dd82a72..6a73659 100644 --- a/inc/class-formatter.php +++ b/inc/class-formatter.php @@ -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 );