Last active
June 4, 2024 13:13
-
-
Save apermo/3e3176313e54359aa81dd76c886e1771 to your computer and use it in GitHub Desktop.
Improved xhprof_prepend for Gutenberg Profiling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This file replaces your DDEV generated xpprof_prepend.php and can likely be used by any xhprof setup. | |
// As for DDEV, mind that you need to use `ddev xhprof on` to turn xhprof on. | |
// When xhprof is enabled, It is mounted into the ddev-webserver container | |
// as /usr/local/bin/xhprof/xhprof_prepend.php | |
// In addition it will create a log file with additional information, not covered by xhprof, like the URL opened, parameter etc, | |
// which will help if you want to optimize hidden processes, like saving a post with the WordPress Block editor. | |
// Customize these values as needed. | |
// Path to the xhprof log file. | |
const XHPROF_LOG_LOCATION = '/tmp/xhprof/xhprof.log'; | |
// URL to the xhprof index page. Use index.php here if empty string does not work. | |
const XHPROF_URL_INDEX = ''; | |
$uri = "none"; | |
if (!empty($_SERVER) && array_key_exists('REQUEST_URI', $_SERVER)) { | |
$uri = $_SERVER['REQUEST_URI']; | |
} | |
define('XHPROF_START_TIME', microtime(true)); | |
// Enable xhprof profiling if we're not on an xhprof page | |
if (extension_loaded('xhprof') && strpos($uri, '/xhprof') === false) { | |
// If this is too much information, just use xhprof_enable(), which shows CPU only | |
xhprof_enable(XHPROF_FLAGS_MEMORY); | |
register_shutdown_function('xhprof_completion'); | |
} | |
// Write to the xhprof_html output and latest on completion | |
function xhprof_completion() | |
{ | |
$xhprof_link_dir = "/var/xhprof/xhprof_html/latest/"; | |
$xhprof_data = xhprof_disable(); | |
define('XHPROF_END_TIME', microtime(true)); | |
$appNamespace = "ddev"; | |
include_once '/var/xhprof/xhprof_lib/utils/xhprof_lib.php'; | |
include_once '/var/xhprof/xhprof_lib/utils/xhprof_runs.php'; | |
$xhprof_runs = new XHProfRuns_Default(); | |
$run_id = $xhprof_runs->save_run($xhprof_data, $appNamespace); | |
// Log the request | |
log_request($run_id, $appNamespace); | |
} | |
function log_request($run_id, $appNamespace) | |
{ | |
if (php_sapi_name() === 'cli') { | |
$referrer = 'none'; | |
$request_type = 'CLI'; | |
$base_link = '/xhprof/'; | |
// use the cli command as URI | |
$uri = implode(' ', $_SERVER['argv']); | |
$uri_label = 'Command:'; | |
$referrer = 'none'; | |
} else { | |
$uri = $_SERVER['REQUEST_URI'] ?? 'none'; | |
$uri_label = 'URI:'; | |
$referrer = $_SERVER['HTTP_REFERER'] ?? 'none'; | |
$local_domain = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . "/"; | |
$referrer = str_replace($local_domain, '/', $referrer); | |
$request_type = $_SERVER['REQUEST_METHOD'] ?? 'none'; | |
if ($request_type === 'POST') { | |
$num_post_vars = count($_POST); | |
// Add the number of bytes in the POST body to the log entry | |
$post_body_size = strlen(file_get_contents('php://input')); | |
$request_type .= sprintf('(%d vars, %.1f kb)', $num_post_vars, $post_body_size / 1024); | |
} | |
$base_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . "/xhprof/"; | |
} | |
$profiler_url = sprintf('%s%s?run=%s&source=%s', $base_link, XHPROF_URL_INDEX, $run_id, $appNamespace); | |
$runtime = XHPROF_END_TIME - XHPROF_START_TIME; | |
// Construct the log entry | |
$log_entry = sprintf( | |
"[%s] Runtime: %.2f s | %s | %s %s | %s | Referrer: %s\n", | |
date('Y-m-d H:i:s'), | |
$runtime, | |
$profiler_url, | |
$uri_label, | |
$uri, | |
$request_type, | |
$referrer, | |
); | |
// Write the log entry to the file | |
file_put_contents(XHPROF_LOG_LOCATION, $log_entry, FILE_APPEND); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment