diff --git a/README.md b/README.md index 20fab92..95487bf 100644 --- a/README.md +++ b/README.md @@ -158,6 +158,10 @@ Profile arbitrary code execution. wp profile eval [--fields=] [--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** @@ -177,6 +181,39 @@ wp profile eval [--fields=] [--format=] - csv --- + + +### wp profile eval-file + +Profile execution of an arbitrary file. + +~~~ +wp profile eval-file [--fields=] [--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** + + + The path to the PHP file to execute and profile. + + [--fields=] + Display one or more fields. + + [--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. diff --git a/composer.json b/composer.json index 84b368b..241f365 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,8 @@ "commands": [ "profile stage", "profile hook", - "profile eval" + "profile eval", + "profile eval-file" ], "readme": { "shields": [ diff --git a/features/profile-eval-file.feature b/features/profile-eval-file.feature new file mode 100644 index 0000000..aeb3dc9 --- /dev/null +++ b/features/profile-eval-file.feature @@ -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: + """ + @@ -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 + * + * + * : The path to the PHP file to execute and profile. + * + * [--fields=] + * : Display one or more fields. + * + * [--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 ); + } + }