Use wp profile eval to profile arbitrary code execution

This commit is contained in:
Daniel Bachhuber 2016-10-04 12:57:10 -07:00
parent da150e2cb8
commit c206a27eea
4 changed files with 116 additions and 1 deletions

View file

@ -148,6 +148,35 @@ wp profile hook <hook> [--url=<url>] [--fields=<fields>] [--format=<format>]
- csv
---
### wp profile eval
Profile arbitrary code execution.
~~~
wp profile eval <php-code> [--fields=<fields>] [--format=<format>]
~~~
**OPTIONS**
<php-code>
The code to execute, as a string.
[--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

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

View file

@ -0,0 +1,37 @@
Feature: Profile arbitary code execution
Scenario: Profile a function that doesn't do anything
Given a WP install
And a wp-content/mu-plugins/lame-function.php file:
"""
<?php
function runcommand_do_nothing() {
}
"""
When I run `wp profile eval 'runcommand_do_nothing();'`
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
When I run `wp profile eval 'wp_remote_get( "http://apple.com" );' --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
When I run `wp profile eval 'wp_cache_get( "foo" );' --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 'wp_cache_set( "foo", "bar" ); wp_cache_get( "foo" ); wp_cache_get( "foo" );' --fields=cache_hits,cache_misses`
Then STDOUT should be a table containing rows:
| cache_hits | cache_misses |
| 2 | 0 |

View file

@ -135,6 +135,54 @@ class Command {
$formatter->display_items( $this->loggers );
}
/**
* Profile arbitrary code execution.
*
* ## OPTIONS
*
* <php-code>
* : The code to execute, as a string.
*
* [--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
*/
public function eval_( $args, $assoc_args ) {
$this->run_profiler();
$logger = new Logger( 'eval', 'eval' );
$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 ) );
}
/**
* Profiling verbosity at the beginning of every action and filter
*/