Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WP Profiler mu-plugin #42

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions plugins/wp-profiler/.wp-env.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"mappings": {
"wp-content/mu-plugins": "./"
},
"config": {
"WP_DEBUG": false,
"WP_DEBUG_LOG": false,
"WP_DEBUG_DISPLAY": false,
"SCRIPT_DEBUG": false
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having this file here seems a bit out of scope to me as it's for the environment, and the plugin folder itself does not make that environment work.

I think it would be great to implement a simple wp-env configuration in this repository for XHProf so that it's standalone. Of course for XHProf that requires us to first merge your PR WordPress/gutenberg#48147.

My suggestion would be to, instead of a general plugins folder and this plugin, add a folder like wp-profiling-env which includes a basic package.json with the necessary dependencies and scripts to run wp-env, and then an mu-plugins folder with the wp-profiler plugin you have here, as that is most useful as part of that environment.

WDYT? We could of course split that work apart, since right now @wordpress/env does not yet support the necessary XHProf work, but I think eventually this would be a better outcome, have a standalone solution in here rather than only the MU plugin and require the rest to be set up manually.

15 changes: 15 additions & 0 deletions plugins/wp-profiler/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# WP Profiler

WP Profiler is an mu-plugin implementation of the [PHP Profiler](https://github.com/perftools/php-profiler) library to make it easier to submit XHProf profiling data to XHGui.

## Installation instructions

After installing this mu-plugin in your application of choice, you'll need to run `composer install` to install the PHP Profiler library dependency. Next, you will need to configure the profiler settings in `plugin.php` to connect to your XHGui application.

To use this as a standalone profiler with WordPress, a sample `.wp-env.json` configuration is included.

See: https://github.com/WordPress/gutenberg/pull/48147 for the status of having XHProf supported directly in the `@wordpress/env` (i.e., `wp-env`) package.

## Disclaimer

This is not an officially supported Google product
5 changes: 5 additions & 0 deletions plugins/wp-profiler/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"perftools/php-profiler": "^1.1"
}
}
34 changes: 34 additions & 0 deletions plugins/wp-profiler/plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
require __DIR__ . '/vendor/autoload.php';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit-pick? Should we have a simple plugin header here?


use Xhgui\Profiler\Profiler;
use Xhgui\Profiler\ProfilingFlags;

// Add this block inside some bootstrapper or other "early central point in execution"
try {
/**
* The constructor will throw an exception if the environment
* isn't fit for profiling (extensions missing, other problems)
*/
$profiler = new Profiler(
array(
'save.handler.upload' => array(
// Use docker's internal networking to connect containers.
'url' => 'http://host.docker.internal:8142/run/import',
),
'profiler.flags' => array(
ProfilingFlags::CPU,
ProfilingFlags::MEMORY,
// Comment out below to include built in PHP functions in profiling output.
ProfilingFlags::NO_BUILTINS,
),
)
);

// The profiler itself checks whether it should be enabled
// for request (executes lambda function from config)
$profiler->start();
} catch (Exception $e){
// throw away or log error about profiling instantiation failure
error_log($e->getMessage());
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit-pick: This file doesn't follow WP coding standards (mostly indentation and spacing-wise).