Skip to content

Instantly share code, notes, and snippets.

@afragen
Last active March 17, 2022 20:37
Show Gist options
  • Save afragen/a8a89888c60c4836a21667531469c7a9 to your computer and use it in GitHub Desktop.
Save afragen/a8a89888c60c4836a21667531469c7a9 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: Count Filter Calls
* Plugin URI: https://gist.github.com/afragen/a8a89888c60c4836a21667531469c7a9
* Description: Count number of times certain filters are called and cumulative time spent for the call with almost no computing done.
* Version: 0.3
* Author: Andy Fragen
* License: MIT
* Requires at least: 5.2
* Requires PHP: 7.0
* Gist Plugin URI: https://gist.github.com/afragen/a8a89888c60c4836a21667531469c7a9
*/
/*
* Exit if called directly.
* PHP version check and exit.
*/
if ( ! defined( 'WPINC' ) ) {
die;
}
global $count_pre, $count_site, $time_pre, $time_site;
add_filter(
'pre_set_site_transient_update_plugins',
function() {
$start = microtime( true );
global $count_pre, $time_pre;
$count_pre++;
$time_pre = $time_pre + ( microtime( true ) - $start );
}
);
// get_site_transient() called for every plugin row.
add_filter(
'site_transient_update_plugins',
function() {
$start = microtime( true );
global $count_site, $time_site;
$count_site++;
$time_site = $time_site + ( microtime( true ) - $start );
}
);
add_action(
'shutdown',
function() {
global $pagenow, $count_pre, $count_site, $time_pre, $time_site;
if ( ! empty( $count_pre ) && ! empty( $count_site ) ) {
error_log( $pagenow . ':pre_set_site_transient_update_plugins: ' . $count_pre . ' calls - ' . number_format( $time_pre, 6 ) . ' seconds (cumulative time)' );
error_log( $pagenow . ':site_transient_update_plugins: ' . $count_site . ' calls - ' . number_format( $time_site, 6 ) . ' seconds (cumulative time)' );
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment