Skip to content

Instantly share code, notes, and snippets.

@peterwilsoncc
Created April 13, 2022 01:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterwilsoncc/df70e801e0ec2c8997607c0afd13b5cf to your computer and use it in GitHub Desktop.
Save peterwilsoncc/df70e801e0ec2c8997607c0afd13b5cf to your computer and use it in GitHub Desktop.
<?php
// add_filter( 'wp_is_large_user_count', '__return_true' );
// add_filter( 'split_the_query', '__return_true' );
// return;
function pwcc_test() {
global $wpdb;
$non_cached_ids = range( 1035, 1044 );
$new = sprintf( "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE ID IN (%s) LIMIT %d", implode( ',', $non_cached_ids ), count( $non_cached_ids ) );
$old = sprintf( "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE ID IN (%s)", implode( ',', $non_cached_ids ) );
$time = ['patched'=>0, 'unpatched'=>0];
$queries = ['patched'=>0, 'unpatched'=>0];
$tests = 10000;
foreach (['cached'=>true/*, 'uncached'=>false*/] as $cached => $cbool ) {
wp_cache_flush();
$loop = $tests;
while( $loop > 0 ) {
$loop--;
if ( ! $cbool ) wp_cache_flush();
// New Query.
$n = get_num_queries();
$a = microtime(true);
$wpdb->get_results( $new );
$time['patched'] = $time['patched'] + ( microtime(true) - $a );
$queries['patched'] = $queries['patched'] + ( get_num_queries() - $n );
// }
//
// wp_cache_flush();
// $loop = $tests;
// while( $loop > 0 ) {
// $loop--;
//
// if ( ! $cbool ) wp_cache_flush();
//
// Old Query.
$n = get_num_queries();
$a = microtime( true );
$wpdb->get_results( $old );
$time['unpatched'] = $time['unpatched'] + ( microtime(true) - $a );
$queries['unpatched'] = $queries['unpatched'] + ( get_num_queries() - $n );
}
var_dump(
[$cached,
$size,
$time,
$queries]
);
$time = ['patched'=>0, 'unpatched'=>0];
$queries = ['patched'=>0, 'unpatched'=>0];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment