Skip to content
Paul Bunyan Message Logger by Diamond Sierra

Time your own code with paul_bunyan_scope

Paul Bunyan times functions attached to hooks on its own. Wrap any other block of code in paul_bunyan_scope() to time it too.

Updated · By Diamond Sierra

The Fix first screen: problems ranked by cost, each with its PB code and what to do
On this page

Paul Bunyan sees functions attached to WordPress hooks, database queries, outside calls and email. To time anything else, such as a function your code calls directly or one step of a long job, wrap it in a scope.

$scope = paul_bunyan_scope( 'draw winners', 'raffle' );

// ... the work you want timed ...

$scope->end();

The scope appears in the request timeline with its duration, alongside everything else that request did.

The arguments

ArgumentWhat it isDefault
$nameWhat the block doesRequired
$componentThe part of your code it belongs toapp
$dataAn optional array of values stored with itEmpty

Timing a function that is not on a hook

function my_build_report( $rows ) {
    $scope = paul_bunyan_scope( 'build report', 'reports', array( 'rows' => count( $rows ) ) );

    $report = my_render_rows( $rows );

    $scope->end();
    return $report;
}

In the custom WooCommerce case study, Paul Bunyan showed a 453 ms block of CPU time inside a page shortcode; a two-line timing check like this one found the 300 ms loop inside it.

Watching a hook from code

To time every function on a hook, function by function, add it to the watch list in Settings, or from code:

paul_bunyan_watch( 'woocommerce_email_order_details' );
paul_bunyan_watch( 'my_plugin_*' ); // every hook that starts with my_plugin_