Merge pull request #86 from runcommand/37-eval-file

Support profiling arbitrary file execution
This commit is contained in:
Daniel Bachhuber 2016-10-04 14:51:49 -07:00 committed by GitHub
commit e82078d3a1
4 changed files with 164 additions and 1 deletions

View file

@ -158,6 +158,10 @@ Profile arbitrary code execution.
wp profile eval <php-code> [--fields=<fields>] [--format=<format>]
~~~
Code execution happens after WordPress has loaded entirely, which means
you can use any utilities defined in WordPress, active plugins, or the
current theme.
**OPTIONS**
<php-code>
@ -177,6 +181,39 @@ wp profile eval <php-code> [--fields=<fields>] [--format=<format>]
- csv
---
### wp profile eval-file
Profile execution of an arbitrary file.
~~~
wp profile eval-file <file> [--fields=<fields>] [--format=<format>]
~~~
File execution happens after WordPress has loaded entirely, which means
you can use any utilities defined in WordPress, active plugins, or the
current theme.
**OPTIONS**
<file>
The path to the PHP file to execute and profile.
[--fields=<fields>]
Display one or more fields.
[--format=<format>]
Render output in a particular format.
---
default: table
options:
- table
- json
- yaml
- csv
---
## Installing
[Get access to `wp profile` for only $129 per year](https://runcommand.memberful.com/checkout?plan=16079). Purchasing an annual subscription locks you into this price for as long as you stay subscribed. Subscriptions include unlimited downloads of the command, plus support and updates for the length of your subscription.

View file

@ -19,7 +19,8 @@
"commands": [
"profile stage",
"profile hook",
"profile eval"
"profile eval",
"profile eval-file"
],
"readme": {
"shields": [

View file

@ -0,0 +1,55 @@
Feature: Profile arbitary file execution
Scenario: Profile a function that doesn't do anything
Given a WP install
And a lame-function.php file:
"""
<?php
function runcommand_do_nothing() {
}
runcommand_do_nothing();
"""
When I run `wp profile eval-file lame-function.php`
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 |
Scenario: Profile a function that makes one HTTP request
Given a WP install
And a http-request.php file:
"""
<?php
wp_remote_get( "http://apple.com" );
"""
When I run `wp profile eval-file http-request.php --fields=request_count`
Then STDOUT should be a table containing rows:
| request_count |
| 1 |
Scenario: Profile calls to the object cache
Given a WP install
And a one-miss.php file:
"""
<?php
wp_cache_get( "foo" );
"""
And a two-hits.php file:
"""
<?php
wp_cache_set( "foo", "bar" );
wp_cache_get( "foo" );
wp_cache_get( "foo" );
"""
When I run `wp profile eval-file one-miss.php --fields=cache_hits,cache_misses`
Then STDOUT should be a table containing rows:
| cache_hits | cache_misses |
| 0 | 1 |
When I run `wp profile eval-file two-hits.php --fields=cache_hits,cache_misses`
Then STDOUT should be a table containing rows:
| cache_hits | cache_misses |
| 2 | 0 |

View file

@ -145,6 +145,10 @@ class Command {
/**
* Profile arbitrary code execution.
*
* Code execution happens after WordPress has loaded entirely, which means
* you can use any utilities defined in WordPress, active plugins, or the
* current theme.
*
* ## OPTIONS
*
* <php-code>
@ -190,6 +194,63 @@ class Command {
$formatter->display_items( array( $logger ) );
}
/**
* Profile execution of an arbitrary file.
*
* File execution happens after WordPress has loaded entirely, which means
* you can use any utilities defined in WordPress, active plugins, or the
* current theme.
*
* ## OPTIONS
*
* <file>
* : The path to the PHP file to execute and profile.
*
* [--fields=<fields>]
* : Display one or more fields.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - json
* - yaml
* - csv
* ---
*
* @when before_wp_load
* @subcommand eval-file
*/
public function eval_file( $args, $assoc_args ) {
$file = $args[0];
if ( ! file_exists( $file ) ) {
WP_CLI::error( "'$file' does not exist." );
}
$this->run_profiler();
$logger = new Logger( 'eval', 'eval' );
$logger->start();
self::include_file( $file );
$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 ) );
}
/**
* Profiling verbosity at the beginning of every action and filter
*/
@ -454,4 +515,13 @@ class Command {
$this->loggers[ $pseudo_hook ]->start();
}
/**
* Include a file without exposing it to current scope
*
* @param string $file
*/
private static function include_file( $file ) {
include( $file );
}
}