wordpress-website-lifecycle/debug/debug-memory-usage.php
Viktor Szépe 084c0db171
Some checks failed
Back-end / Syntax errors (push) Has been cancelled
Integrity / Integrity (push) Has been cancelled
Spelling / 文A Typos check (push) Has been cancelled
Improve debugging
2025-11-09 20:36:30 +00:00

34 lines
973 B
PHP

<?php
/*
* Plugin Name: Log memory usage (DBG)
* Plugin URI: https://github.com/szepeviktor/wordpress-website-lifecycle
*/
add_action(
'shutdown',
static function () {
$peakUsage = memory_get_peak_usage(true);
// Report above 20 MB.
if ($peakUsage < 20 * 1024 * 1024) {
return;
}
$uri = isset($_SERVER['REQUEST_URI'])
? wp_json_encode($_SERVER['REQUEST_URI'], JSON_UNESCAPED_SLASHES)
: 'CLI';
$message = sprintf('Peak memory usage = %d bytes, %s', $peakUsage, $uri);
switch (true) {
case defined('WP_CLI') && WP_CLI:
WP_CLI::debug($message, 'memory-usage');
break;
case wp_doing_cron() && php_sapi_name() === 'cli':
// No output during WP-Cron run from CLI.
break;
default:
error_log($message);
break;
}
},
-1,
0
);