Created
July 1, 2017 12:15
-
-
Save Mamaduka/00222f1873f9fc49c565710ca95701af to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Mamdauka\WpQuerySpeed\Command; | |
use WP_CLI; | |
use WP_CLI\Utils; | |
use WP_CLI_Command; | |
use WP_Query; | |
use Symfony\Component\Stopwatch\Stopwatch; | |
class Test extends WP_CLI_Command { | |
const POST_TYPE = 'post'; | |
const PER_PAGE = 100; | |
/** | |
* Runs query speed tests. | |
*/ | |
public function __invoke( array $args, array $assoc_args ) { | |
$found_posts = $this->found_posts(); | |
$total_pages = (int) ceil( $found_posts / static::PER_PAGE ); | |
$page = 1; | |
$stopwatch = new Stopwatch(); | |
$stopwatch->start( 'indexing' ); | |
do { | |
$this->get_items( $page++ ); | |
Utils\wp_clear_object_cache(); | |
} while ( $page <= $total_pages ); | |
$event = $stopwatch->stop( 'indexing' ); | |
WP_CLI::log( $event ); | |
WP_CLI::success( sprintf( 'Finished processing %d page(s)', $page ) ); | |
} | |
protected function found_posts() { | |
$query = new WP_Query([ | |
'post_type' => static::POST_TYPE, | |
'post_status' => 'any', | |
'suppress_filters' => true, | |
'cache_results' => false, | |
'lazy_load_term_meta' => false | |
]); | |
return (int) $query->found_posts; | |
} | |
protected function get_items( $page = 0 ) { | |
// See Algolia_Index::re_index() - #L197 | |
$found_posts = $this->found_posts(); | |
$query = new WP_Query([ | |
'post_type' => 'post', | |
'posts_per_page' => static::PER_PAGE, | |
'post_status' => 'any', | |
'order' => 'ASC', | |
'orderby' => 'ID', | |
'paged' => $page, | |
'suppress_filters' => true | |
]); | |
} | |
} | |
WP_CLI::add_command( 'mamaduka test', Test::class ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment