mirror of
https://hk.gh-proxy.com/https://github.com/wp-cli/profile-command.git
synced 2025-08-20 06:30:56 +08:00
Use --hook[=<hook>]
to profile all hooks, or callbacks to spec hook
This commit is contained in:
parent
3a51fbb491
commit
fefcc60b0d
4 changed files with 83 additions and 33 deletions
|
@ -53,3 +53,22 @@ Feature: Profile arbitary file execution
|
||||||
Then STDOUT should be a table containing rows:
|
Then STDOUT should be a table containing rows:
|
||||||
| cache_hits | cache_misses |
|
| cache_hits | cache_misses |
|
||||||
| 2 | 0 |
|
| 2 | 0 |
|
||||||
|
|
||||||
|
Scenario: Profile a function calling a hook
|
||||||
|
Given a WP install
|
||||||
|
And a calls-hook.php file:
|
||||||
|
"""
|
||||||
|
<?php
|
||||||
|
add_filter( 'logout_url', function( $url ) { wp_cache_get( 'foo' ); return $url; });
|
||||||
|
wp_logout_url();
|
||||||
|
"""
|
||||||
|
|
||||||
|
When I run `wp profile eval-file calls-hook.php --hook --fields=hook,cache_hits,cache_misses`
|
||||||
|
Then STDOUT should be a table containing rows:
|
||||||
|
| hook | cache_hits | cache_misses |
|
||||||
|
| logout_url | 0 | 1 |
|
||||||
|
|
||||||
|
When I run `wp profile eval-file calls-hook.php --hook=logout_url --fields=callback,cache_hits,cache_misses`
|
||||||
|
Then STDOUT should be a table containing rows:
|
||||||
|
| callback | cache_hits | cache_misses |
|
||||||
|
| function(){} | 0 | 1 |
|
||||||
|
|
|
@ -35,3 +35,16 @@ Feature: Profile arbitary code execution
|
||||||
Then STDOUT should be a table containing rows:
|
Then STDOUT should be a table containing rows:
|
||||||
| cache_hits | cache_misses |
|
| cache_hits | cache_misses |
|
||||||
| 2 | 0 |
|
| 2 | 0 |
|
||||||
|
|
||||||
|
Scenario: Profile a function calling a hook
|
||||||
|
Given a WP install
|
||||||
|
|
||||||
|
When I run `wp profile eval "add_filter( 'logout_url', function( $url ) { wp_cache_get( 'foo' ); return $url; }); wp_logout_url();" --hook --fields=hook,cache_hits,cache_misses`
|
||||||
|
Then STDOUT should be a table containing rows:
|
||||||
|
| hook | cache_hits | cache_misses |
|
||||||
|
| logout_url | 0 | 1 |
|
||||||
|
|
||||||
|
When I run `wp profile eval "add_filter( 'logout_url', function( $url ) { wp_cache_get( 'foo' ); return $url; }); wp_logout_url();" --hook=logout_url --fields=callback,cache_hits,cache_misses`
|
||||||
|
Then STDOUT should be a table containing rows:
|
||||||
|
| callback | cache_hits | cache_misses |
|
||||||
|
| function(){} | 0 | 1 |
|
||||||
|
|
|
@ -221,6 +221,9 @@ class Command {
|
||||||
* <php-code>
|
* <php-code>
|
||||||
* : The code to execute, as a string.
|
* : The code to execute, as a string.
|
||||||
*
|
*
|
||||||
|
* [--hook[=<hook>]]
|
||||||
|
* : Focus on key metrics for all hooks, or callbacks on a specific hook.
|
||||||
|
*
|
||||||
* [--fields=<fields>]
|
* [--fields=<fields>]
|
||||||
* : Display one or more fields.
|
* : Display one or more fields.
|
||||||
*
|
*
|
||||||
|
@ -235,31 +238,13 @@ class Command {
|
||||||
* - csv
|
* - csv
|
||||||
* ---
|
* ---
|
||||||
*
|
*
|
||||||
* @when before_wp_load
|
|
||||||
* @subcommand eval
|
* @subcommand eval
|
||||||
*/
|
*/
|
||||||
public function eval_( $args, $assoc_args ) {
|
public function eval_( $args, $assoc_args ) {
|
||||||
|
$statement = $args[0];
|
||||||
$profiler = new Profiler( false, false );
|
self::profile_eval_ish( $assoc_args, function() use ( $statement ) {
|
||||||
$profiler->run();
|
eval( $statement );
|
||||||
|
});
|
||||||
$logger = new Logger();
|
|
||||||
$logger->start();
|
|
||||||
eval( $args[0] );
|
|
||||||
$logger->stop();
|
|
||||||
|
|
||||||
$fields = array(
|
|
||||||
'time',
|
|
||||||
'query_time',
|
|
||||||
'query_count',
|
|
||||||
'cache_ratio',
|
|
||||||
'cache_hits',
|
|
||||||
'cache_misses',
|
|
||||||
'request_time',
|
|
||||||
'request_count',
|
|
||||||
);
|
|
||||||
$formatter = new Formatter( $assoc_args, $fields );
|
|
||||||
$formatter->display_items( array( $logger ), false );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -274,6 +259,9 @@ class Command {
|
||||||
* <file>
|
* <file>
|
||||||
* : The path to the PHP file to execute and profile.
|
* : The path to the PHP file to execute and profile.
|
||||||
*
|
*
|
||||||
|
* [--hook[=<hook>]]
|
||||||
|
* : Focus on key metrics for all hooks, or callbacks on a specific hook.
|
||||||
|
*
|
||||||
* [--fields=<fields>]
|
* [--fields=<fields>]
|
||||||
* : Display one or more fields.
|
* : Display one or more fields.
|
||||||
*
|
*
|
||||||
|
@ -288,7 +276,6 @@ class Command {
|
||||||
* - csv
|
* - csv
|
||||||
* ---
|
* ---
|
||||||
*
|
*
|
||||||
* @when before_wp_load
|
|
||||||
* @subcommand eval-file
|
* @subcommand eval-file
|
||||||
*/
|
*/
|
||||||
public function eval_file( $args, $assoc_args ) {
|
public function eval_file( $args, $assoc_args ) {
|
||||||
|
@ -298,15 +285,41 @@ class Command {
|
||||||
WP_CLI::error( "'$file' does not exist." );
|
WP_CLI::error( "'$file' does not exist." );
|
||||||
}
|
}
|
||||||
|
|
||||||
$profiler = new Profiler( false, false );
|
self::profile_eval_ish( $assoc_args, function() use ( $file ) {
|
||||||
|
self::include_file( $file );
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Profile an eval or eval-file statement.
|
||||||
|
*/
|
||||||
|
private static function profile_eval_ish( $assoc_args, $profile_callback ) {
|
||||||
|
$hook = Utils\get_flag_value( $assoc_args, 'hook' );
|
||||||
|
$type = $focus = false;
|
||||||
|
$fields = array();
|
||||||
|
if ( $hook ) {
|
||||||
|
$type = 'hook';
|
||||||
|
if ( true !== $hook ) {
|
||||||
|
$focus = $hook;
|
||||||
|
$fields[] = 'callback';
|
||||||
|
$fields[] = 'location';
|
||||||
|
} else {
|
||||||
|
$fields[] = 'hook';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$profiler = new Profiler( $type, $focus );
|
||||||
$profiler->run();
|
$profiler->run();
|
||||||
|
if ( $hook ) {
|
||||||
$logger = new Logger();
|
$profile_callback();
|
||||||
$logger->start();
|
$loggers = $profiler->get_loggers();
|
||||||
self::include_file( $file );
|
} else {
|
||||||
$logger->stop();
|
$logger = new Logger();
|
||||||
|
$logger->start();
|
||||||
$fields = array(
|
$profile_callback();
|
||||||
|
$logger->stop();
|
||||||
|
$loggers = array( $logger );
|
||||||
|
}
|
||||||
|
$fields = array_merge( $fields, array(
|
||||||
'time',
|
'time',
|
||||||
'query_time',
|
'query_time',
|
||||||
'query_count',
|
'query_count',
|
||||||
|
@ -315,9 +328,9 @@ class Command {
|
||||||
'cache_misses',
|
'cache_misses',
|
||||||
'request_time',
|
'request_time',
|
||||||
'request_count',
|
'request_count',
|
||||||
);
|
) );
|
||||||
$formatter = new Formatter( $assoc_args, $fields );
|
$formatter = new Formatter( $assoc_args, $fields );
|
||||||
$formatter->display_items( array( $logger ), false );
|
$formatter->display_items( $loggers, false );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -370,6 +370,11 @@ class Profiler {
|
||||||
*/
|
*/
|
||||||
private function load_wordpress_with_template() {
|
private function load_wordpress_with_template() {
|
||||||
|
|
||||||
|
// WordPress already ran once.
|
||||||
|
if ( function_exists( 'add_filter' ) ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if ( 'stage' === $this->type && true === $this->focus ) {
|
if ( 'stage' === $this->type && true === $this->focus ) {
|
||||||
$hooks = array();
|
$hooks = array();
|
||||||
foreach( $this->stage_hooks as $stage_hook ) {
|
foreach( $this->stage_hooks as $stage_hook ) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue