Skip to content

Instantly share code, notes, and snippets.

@thenbrent
Last active September 9, 2019 06:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thenbrent/ac866730caffe34fe60ff8a8334855ca to your computer and use it in GitHub Desktop.
Save thenbrent/ac866730caffe34fe60ff8a8334855ca to your computer and use it in GitHub Desktop.
A few handy functions for debugging PHP script execution in WordPress/WooCommerce.
<?php
/**
* Plugin Name: Brent's Debugging Helpers
* Plugin URI:
* Description: bs_backtrace() etc.
* Version: 8.13.21
* Author:
* Author URI:
*/
if ( ! defined( 'bs_backtrace' ) ) {
function bs_backtrace( $method ) {
ob_start();
debug_print_backtrace();
$back_trace = ob_get_clean();
//array_shift( $back_trace );
error_log( 'In ' . $method . '() debug_print_backtrace() = ' . "\n" . print_r( $back_trace, true ) );
}
}
if ( ! defined( 'bs_log_request_actions' ) ) {
function bs_log_request_actions() {
add_action( 'shutdown', function(){
bs_log_actions();
});
}
}
function bs_log_actions() {
error_log( 'wp_actions = ' . print_r( $GLOBALS['wp_actions'], true ) );
}
if ( ! defined( 'bs_log_request_filters' ) ) {
function bs_log_request_filters() {
add_action( 'shutdown', function(){
bs_log_filters();
});
}
}
function bs_log_filters( $filter_name = '' ) {
if ( ! empty( $filter_name ) ) {
error_log( 'wp_filter[ ' . $filter_name . ' = ' . print_r( $GLOBALS['wp_filter'][ $filter_name ], true ) );
} else {
error_log( 'wp_filter = ' . print_r( $GLOBALS['wp_filter'], true ) );
}
}
/**
* Script Timing Functions
*/
function bs_start_script_clock() {
$dat = getrusage();
define( 'BS_PHP_TUSAGE', microtime( true ) );
define( 'BS_PHP_RUSAGE', $dat["ru_utime.tv_sec"] * 1e6 + $dat["ru_utime.tv_usec"] );
}
function bs_get_script_time() {
return ( microtime( true ) - BS_PHP_TUSAGE );
}
function bs_get_script_time_in_seconds() {
return bs_get_script_time() * 1000000;
}
/**
* Code Based on: http://php.webtutor.pl/en/2011/05/13/how-to-calculate-cpu-usage-of-a-php-script/
*/
function bs_get_cpu_usage() {
$dat = getrusage();
$dat["ru_utime.tv_usec"] = ($dat["ru_utime.tv_sec"]*1e6 + $dat["ru_utime.tv_usec"]) - BS_PHP_RUSAGE;
$time = bs_get_script_time_in_seconds();
// cpu per request
if( $time > 0 ) {
$cpu = sprintf( "%01.2f", ( $dat["ru_utime.tv_usec"] / $time ) * 100 );
} else {
$cpu = '0.00';
}
return $cpu;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment