Merge pull request #151 from wojsmol/tov2

Move command over to new v2 structure
This commit is contained in:
Alain Schlesser 2018-09-25 20:29:59 +02:00 committed by GitHub
commit e560986886
8 changed files with 291 additions and 180 deletions

1
.gitignore vendored
View file

@ -5,3 +5,4 @@ node_modules/
vendor/
*.zip
*.tar.gz
*.log

View file

@ -1,25 +1,24 @@
{
"name": "wp-cli/profile-command",
"description": "Quickly identify what's slow with WordPress.",
"type": "wp-cli-package",
"description": "Quickly identify what's slow with WordPress.",
"homepage": "https://runcommand.io/wp/profile/",
"license": "MIT",
"authors": [],
"minimum-stability": "dev",
"prefer-stable": true,
"autoload": {
"files": [ "command.php" ]
},
"require": {
"php": ">=5.4",
"wp-cli/wp-cli": "*"
"wp-cli/wp-cli": "^2"
},
"require-dev": {
"behat/behat": "~2.5"
"wp-cli/wp-cli-tests": "^2.0.7"
},
"config": {
"process-timeout": 7200,
"sort-packages": true
},
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
"dev-master": "2.x-dev"
},
"commands": [
"profile stage",
@ -38,5 +37,26 @@
"post": "bin/readme/overview-body.md"
}
}
},
"autoload": {
"files": [
"command.php"
]
},
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"behat": "run-behat-tests",
"behat-rerun": "rerun-behat-tests",
"lint": "run-linter-tests",
"phpcs": "run-phpcs-tests",
"phpunit": "run-php-unit-tests",
"prepare-tests": "install-package-tests",
"test": [
"@lint",
"@phpcs",
"@phpunit",
"@behat"
]
}
}

View file

@ -3,12 +3,12 @@ Feature: Profile a specific hook
Scenario: Profile all hooks when a specific hook isn't specified
Given a WP install
When I run `wp profile hook --fields=hook,callback_count`
When I run `wp profile hook --fields=hook`
Then STDOUT should be a table containing rows:
| hook | callback_count |
| plugins_loaded | 3 |
| init | 11 |
| template_redirect | 7 |
| hook |
| plugins_loaded |
| init |
| template_redirect |
And STDERR should be empty
Scenario: Profile all callbacks when --all flag is used
@ -116,7 +116,7 @@ Feature: Profile a specific hook
});
"""
When I run `wp profile hook init`
When I try `wp profile hook init`
Then STDERR should be:
"""
Warning: Called 1

View file

@ -289,9 +289,14 @@ class Command {
$order = Utils\get_flag_value( $assoc_args, 'order', 'ASC' );
$orderby = Utils\get_flag_value( $assoc_args, 'orderby', null );
self::profile_eval_ish( $assoc_args, function() use ( $statement ) {
eval( $statement );
}, $order, $orderby );
self::profile_eval_ish(
$assoc_args,
function() use ( $statement ) {
eval( $statement ); // phpcs:ignore Squiz.PHP.Eval.Discouraged -- no other way oround here
},
$order,
$orderby
);
}
/**
@ -348,15 +353,20 @@ class Command {
WP_CLI::error( "'$file' does not exist." );
}
self::profile_eval_ish( $assoc_args, function() use ( $file ) {
self::profile_eval_ish(
$assoc_args,
function() use ( $file ) {
self::include_file( $file );
}, $order, $orderby );
},
$order,
$orderby
);
}
/**
* Profile an eval or eval-file statement.
*/
private static function profile_eval_ish( $assoc_args, $profile_callback ) {
private static function profile_eval_ish( $assoc_args, $profile_callback, $order = 'ASC', $orderby = null ) {
$hook = Utils\get_flag_value( $assoc_args, 'hook' );
$type = $focus = false;
$fields = array();
@ -382,7 +392,9 @@ class Command {
$logger->stop();
$loggers = array( $logger );
}
$fields = array_merge( $fields, array(
$fields = array_merge(
$fields,
array(
'time',
'query_time',
'query_count',
@ -391,7 +403,8 @@ class Command {
'cache_misses',
'request_time',
'request_count',
) );
)
);
$formatter = new Formatter( $assoc_args, $fields );
$formatter->display_items( $loggers, false, $order, $orderby );
}

View file

@ -14,7 +14,7 @@ class Formatter {
$format_args = array(
'format' => 'table',
'fields' => $fields,
'field' => null
'field' => null,
);
foreach ( array( 'format', 'fields', 'field' ) as $key ) {
@ -90,7 +90,9 @@ class Formatter {
}
if ( $orderby ) {
usort( $items, function( $a, $b ) use ( $order, $orderby ) {
usort(
$items,
function( $a, $b ) use ( $order, $orderby ) {
list( $first, $second ) = 'ASC' === $order ? array( $a, $b ) : array( $b, $a );
if ( is_numeric( $first->$orderby ) && is_numeric( $second->$orderby ) ) {
@ -98,7 +100,8 @@ class Formatter {
}
return strcmp( $first->$orderby, $second->$orderby );
});
}
);
}
$location_index = array_search( 'location', $fields );

View file

@ -61,7 +61,7 @@ class Logger {
if ( ! is_null( $this->start_time ) ) {
$this->time += microtime( true ) - $this->start_time;
}
if ( ! is_null( $this->query_offset ) && isset( $wpdb ) ) {
if ( ! is_null( $this->query_offset ) && isset( $wpdb ) && ! empty( $wpdb->queries ) ) {
for ( $i = $this->query_offset; $i < count( $wpdb->queries ); $i++ ) {
$this->query_time += $wpdb->queries[ $i ][1];
$this->query_count++;

View file

@ -75,21 +75,27 @@ class Profiler {
* Run the profiler against WordPress
*/
public function run() {
WP_CLI::add_wp_hook( 'muplugins_loaded', function(){
WP_CLI::add_wp_hook(
'muplugins_loaded',
function() {
if ( $url = WP_CLI::get_runner()->config['url'] ) {
WP_CLI::set_url( trailingslashit( $url ) );
} else {
WP_CLI::set_url( home_url( '/' ) );
}
});
WP_CLI::add_hook( 'after_wp_config_load', function() {
}
);
WP_CLI::add_hook(
'after_wp_config_load',
function() {
if ( defined( 'SAVEQUERIES' ) && ! SAVEQUERIES ) {
WP_CLI::error( "'SAVEQUERIES' is defined as false, and must be true. Please check your wp-config.php" );
}
if ( ! defined( 'SAVEQUERIES' ) ) {
define( 'SAVEQUERIES', true );
}
});
}
);
if ( 'hook' === $this->type
&& ':before' === substr( $this->focus, -7, 7 ) ) {
$stage_hooks = array();
@ -123,24 +129,24 @@ class Profiler {
public function wp_tick_profile_begin( $value = null ) {
if ( version_compare( PHP_VERSION, '7.0.0' ) >= 0 ) {
WP_CLI::error( "Profiling intermediate hooks is broken in PHP 7, see https://bugs.php.net/bug.php?id=72966" );
WP_CLI::error( 'Profiling intermediate hooks is broken in PHP 7, see https://bugs.php.net/bug.php?id=72966' );
}
// Disable opcode optimizers. These "optimize" calls out of the stack
// and hide calls from the tick handler and backtraces.
// Copied from P3 Profiler
if ( extension_loaded( 'xcache' ) ) {
@ini_set( 'xcache.optimizer', false );
@ini_set( 'xcache.optimizer', false ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- ini_set can be disabled on server.
} elseif ( extension_loaded( 'apc' ) ) {
@ini_set( 'apc.optimization', 0 );
@ini_set( 'apc.optimization', 0 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- ini_set can be disabled on server.
apc_clear_cache();
} elseif ( extension_loaded( 'eaccelerator' ) ) {
@ini_set( 'eaccelerator.optimizer', 0 );
@ini_set( 'eaccelerator.optimizer', 0 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- ini_set can be disabled on server.
if ( function_exists( 'eaccelerator_optimizer' ) ) {
@eaccelerator_optimizer( false );
@eaccelerator_optimizer( false ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- disabling eaccelerator on runtime can faild
}
} elseif ( extension_loaded( 'Zend Optimizer+' ) ) {
@ini_set( 'zend_optimizerplus.optimization_level', 0 );
@ini_set( 'zend_optimizerplus.optimization_level', 0 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- ini_set can be disabled on server.
}
register_tick_function( array( $this, 'handle_function_tick' ) );
@ -180,7 +186,12 @@ class Profiler {
$callback_count += count( $cbs );
}
}
$this->loggers[ $current_filter ] = new Logger( array( 'hook' => $current_filter, 'callback_count' => $callback_count ) );
$this->loggers[ $current_filter ] = new Logger(
array(
'hook' => $current_filter,
'callback_count' => $callback_count,
)
);
$this->loggers[ $current_filter ]->start();
}
@ -218,9 +229,11 @@ class Profiler {
$callbacks[ $priority ][ $i ] = array(
'function' => function() use ( $the_, $i ) {
if ( ! isset( $this->loggers[ $i ] ) ) {
$this->loggers[ $i ] = new Logger( array(
$this->loggers[ $i ] = new Logger(
array(
'callback' => $the_['function'],
) );
)
);
}
$this->loggers[ $i ]->start();
$value = call_user_func_array( $the_['function'], func_get_args() );
@ -252,7 +265,7 @@ class Profiler {
if ( false !== $key && isset( $this->current_stage_hooks[ $key + 1 ] ) ) {
$pseudo_hook = "{$this->current_stage_hooks[$key+1]}:before";
} else {
$pseudo_hook = "{$this->current_stage_hooks[$key]}:after";;
$pseudo_hook = "{$this->current_stage_hooks[$key]}:after";
$this->running_hook = $pseudo_hook;
}
$this->loggers[ $pseudo_hook ] = new Logger( array( 'hook' => $pseudo_hook ) );
@ -434,7 +447,7 @@ class Profiler {
// Template is normally loaded in global scope, so we need to replicate
foreach ( $GLOBALS as $key => $value ) {
global $$key;
global ${$key}; // phpcs:ignore PHPCompatibility.PHP.ForbiddenGlobalVariableVariable.NonBareVariableFound -- Syntax is updated to compatible with php 5 and 7.
}
// Load the theme template.

61
phpcs.xml.dist Normal file
View file

@ -0,0 +1,61 @@
<?xml version="1.0"?>
<ruleset name="WP-CLI">
<description>Custom ruleset for WP-CLI</description>
<!-- For help in understanding this file: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml -->
<!-- For help in using PHPCS: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage -->
<!-- What to scan -->
<file>.</file>
<!-- Ignoring Files and Folders:
https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#ignoring-files-and-folders -->
<exclude-pattern>*/.git/*</exclude-pattern>
<exclude-pattern>*/ci/*</exclude-pattern>
<exclude-pattern>*/features/*</exclude-pattern>
<exclude-pattern>*/packages/*</exclude-pattern>
<exclude-pattern>*/tests/*</exclude-pattern>
<exclude-pattern>*/utils/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>
<!-- How to scan -->
<arg value="sp"/> <!-- Show sniff and progress -->
<arg name="colors"/> <!-- Show results with colors -->
<arg name="extensions" value="php"/> <!-- Limit to PHP files -->
<!-- Rules: Check PHP version compatibility - see
https://github.com/PHPCompatibility/PHPCompatibilityWP -->
<rule ref="PHPCompatibilityWP">
<!-- Polyfill package is used so array_column() is available for PHP 5.4- -->
<exclude name="PHPCompatibility.PHP.NewFunctions.array_columnFound"/>
<!-- Both magic quotes directives set in wp-settings-cli.php to provide consistent starting point. -->
<exclude name="PHPCompatibility.PHP.DeprecatedIniDirectives.magic_quotes_runtimeDeprecatedRemoved"/>
<exclude name="PHPCompatibility.PHP.DeprecatedIniDirectives.magic_quotes_sybaseDeprecatedRemoved"/>
</rule>
<!-- For help in understanding this testVersion:
https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
<config name="testVersion" value="5.4-"/>
<!-- Ignore php_uname mode issue, we're conditionally providing a callback -->
<rule ref="PHPCompatibility.PHP.NewFunctionParameters.php_uname_modeFound">
<exclude-pattern>*/php/commands/src/CLI_Command.php</exclude-pattern>
</rule>
<!-- Rules: WordPress Coding Standards - see
https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards -->
<rule ref="WordPress-Core">
<exclude name="Squiz.PHP.DisallowMultipleAssignments.Found" />
<exclude name="WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar" />
<exclude name="WordPress.NamingConventions.ValidVariableName.MemberNotSnakeCase" />
<exclude name="WordPress.NamingConventions.ValidVariableName.NotSnakeCase" />
</rule>
<!-- For help in understanding these custom sniff properties:
https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties -->
<rule ref="WordPress.Files.FileName">
<properties>
<property name="strict_class_file_names" value="false"/>
</properties>
<exclude name="WordPress.Files.FileName.NotHyphenatedLowercase" />
</rule>
</ruleset>