Skip to content

Instantly share code, notes, and snippets.

@cadic
Last active November 1, 2021 08:45
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 cadic/6e841c626fe5047bbfd569aa2e69237c to your computer and use it in GitHub Desktop.
Save cadic/6e841c626fe5047bbfd569aa2e69237c to your computer and use it in GitHub Desktop.
Testing post__not_in vs filtering
<?php
/**
* Plugin Name: Perf Test
* Text Domain: perf-test
* Domain Path: /languages
* Version: 0.1.0
*
* @package Perf_Test
*/
function use_post__not_in( array $exclude, int $per_page ) {
$q = new WP_Query(
array(
'post_status' => 'publish',
'posts_per_page' => $per_page,
'post__not_in' => $exclude,
)
);
return $q->posts;
}
function use_post__not_in_ids_only( array $exclude, int $per_page ) {
$q = new WP_Query(
array(
'post_status' => 'publish',
'posts_per_page' => $per_page,
'post__not_in' => $exclude,
'fields' => 'ids',
)
);
return $q->posts;
}
function use_custom_filter_ids( array $exclude, int $per_page ) {
$count = count( $exclude );
$posts_per_page = $count + $per_page;
$q = new WP_Query(
array(
'post_status' => 'publish',
'posts_per_page' => $posts_per_page,
'fields' => 'ids',
)
);
$result = array_filter(
$q->posts,
function ( $post ) use ( $exclude ) {
return ! in_array( $post->ID, $exclude );
}
);
$result = array_slice( $result, 0, $per_page );
$q = new WP_Query(
array(
'post_status' => 'publish',
'posts_per_page' => $per_page,
'post__in' => $result,
)
);
return $q->posts;
}
function use_custom_filter( array $exclude, int $per_page ) {
$count = count( $exclude );
$posts_per_page = $count + $per_page;
$q = new WP_Query(
array(
'post_status' => 'publish',
'posts_per_page' => $posts_per_page,
)
);
$result = array_filter(
$q->posts,
function ( $post ) use ( $exclude ) {
return ! in_array( $post->ID, $exclude );
}
);
$result = array_slice( $result, 0, $per_page );
return $result;
}
function use_custom_filter_ids_only( array $exclude, int $per_page ) {
$count = count( $exclude );
$posts_per_page = $count + $per_page;
$q = new WP_Query(
array(
'post_status' => 'publish',
'posts_per_page' => $posts_per_page,
'fields' => 'ids',
)
);
$result = array_filter(
$q->posts,
function ( $post ) use ( $exclude ) {
return ! in_array( $post, $exclude );
}
);
$result = array_slice( $result, 0, $per_page );
return $result;
}
function perform_test( callable $function, $exclude_count = 10, $runs = 100, $per_page = 100, $cache = false ) {
$all_posts = get_posts(
array(
'post_status' => 'publish',
'posts_per_page' => -1,
'fields' => 'ids',
)
);
$start = microtime( true );
for ( $i = 0; $i < $runs; $i++ ) {
// Random post IDs to exclude if no caching.
if ( ! $cache ) {
shuffle( $all_posts );
}
$exclude = array_slice( $all_posts, 0, $exclude_count );
// Execute testing function.
$function( $exclude, $per_page );
}
$end = microtime( true );
$mem = memory_get_peak_usage( true );
$time = $end - $start;
$per = $time / $runs;
return array(
'time' => $time,
'avg' => $per,
'mem' => $mem,
);
}
/**
* Performs full test from 100 to 1000 excluded posts with step of 100
*
* @param callable $function
* @return void
*/
function full_test( callable $function, $runs = 10, $per_page = 100 ) {
for ( $i = 100; $i < 2500; $i += 100 ) {
$results = perform_test( $function, $i, $runs, $per_page, false );
file_put_contents(
$function . '_test_data.tsv',
$runs."\t".$i."\t".$results['time']."\t".$results['avg']."\t".$results['mem']."\n",
FILE_APPEND
);
}
}
function run_all()
{
$methods = array(
'use_post__not_in_ids_only',
'use_custom_filter_ids_only',
'use_post__not_in',
'use_custom_filter_ids',
'use_custom_filter'
);
foreach ( $methods as $method ) {
full_test( $method );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment