Skip to content

Commit 7f9fc79

Browse files
committed
Getting there
1 parent 0bfe0de commit 7f9fc79

2 files changed

Lines changed: 90 additions & 3 deletions

File tree

benchpress.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ function benchpress_render_settings_page() {
189189
update_option( 'benchpress_orderby', sanitize_text_field( $_POST['benchpress_orderby'] ) );
190190
update_option( 'benchpress_order', in_array( $_POST['benchpress_order'], ['ASC', 'DESC'] ) ? $_POST['benchpress_order'] : 'ASC' );
191191
update_option( 'benchpress_enable_transient_vs_query', isset( $_POST['benchpress_enable_transient_vs_query'] ) ? 1 : 0 );
192+
update_option( 'benchpress_enable_meta_query_test', isset( $_POST['benchpress_enable_meta_query_test'] ) ? 1 : 0 );
192193
}
193194

194195
// Retrieve saved settings.
@@ -227,7 +228,9 @@ function benchpress_render_settings_page() {
227228
echo '<td><input type="checkbox" name="benchpress_enable_switch_vs_match" ' . checked( 1, $enable_switch_vs_match, false ) . ' /></td></tr>';
228229
echo '<tr><th>' . esc_html__( 'Enable Transient vs Direct Query Benchmark', 'benchpress' ) . '</th>';
229230
echo '<td><input type="checkbox" name="benchpress_enable_transient_vs_query" ' . checked( 1, get_option( 'benchpress_enable_transient_vs_query', 1 ), false ) . ' /></td></tr>';
230-
231+
echo '<tr><th>' . esc_html__( 'Enable Post Meta Access Benchmark', 'benchpress' ) . '</th>';
232+
echo '<td><input type="checkbox" name="benchpress_enable_meta_query_test" ' . checked( 1, get_option( 'benchpress_enable_meta_query_test', 1 ), false ) . ' /></td></tr>';
233+
231234
echo '</table>';
232235

233236
// WP_Query Customization Section.

includes/helper-functions.php

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,16 +247,21 @@ function benchpress_run_all_benchmarks() {
247247
$benchmarks[] = $switch_vs_match;
248248
}
249249

250-
// Run Transient vs Direct Query Benchmark only if enabled.
251250
if ( get_option( 'benchpress_enable_transient_vs_query', 1 ) ) {
252251
$benchmarks[] = benchpress_benchmark_transient_vs_direct_query();
253252
}
254253

254+
if ( get_option( 'benchpress_enable_meta_query_test', 1 ) ) {
255+
$benchmarks[] = benchpress_benchmark_post_meta_access();
256+
}
257+
255258
$benchmarks[] = benchpress_benchmark_wp_query_by_id();
256259
$benchmarks[] = benchpress_benchmark_array_merge();
257260
$benchmarks[] = benchpress_benchmark_string_concatenation();
258261

259-
// Return the benchmarks array directly
262+
$benchmarks = apply_filters( 'benchpress_run_all_benchmarks', $benchmarks );
263+
264+
// Return the benchmarks array directly.
260265
return $benchmarks;
261266
}
262267

@@ -310,3 +315,82 @@ function benchpress_benchmark_transient_vs_direct_query() {
310315
),
311316
];
312317
}
318+
319+
/**
320+
* Benchmark Post Meta Access Methods.
321+
*
322+
* Compares the performance of retrieving post meta using `get_post_meta()` vs. `WP_Meta_Query`.
323+
*
324+
* @since 1.0.0
325+
* @return array|null Benchmark data showing performance difference, or null if settings are missing.
326+
*/
327+
function benchpress_benchmark_post_meta_access() {
328+
if ( ! get_option( 'benchpress_enable_meta_query_test', 1 ) ) {
329+
return;
330+
}
331+
332+
$loop_count = get_option( 'benchpress_loop_count', 10 );
333+
$meta_key = get_option( 'benchpress_meta_key', '_sample_meta_key' );
334+
$query_type = get_option( 'benchpress_query_type', 'single' );
335+
$post_ids = get_option( 'benchpress_post_id', [] );
336+
337+
// Handle case where there are no post IDs.
338+
if ( empty( $post_ids ) || empty( $meta_key ) ) {
339+
return;
340+
}
341+
342+
// Prepare the post ID(s) for single or multiple post queries.
343+
$post_ids = (array) $post_ids;
344+
345+
// Benchmark `get_post_meta`.
346+
$start_meta = microtime( true );
347+
for ( $i = 0; $i < $loop_count; $i++ ) {
348+
foreach ( $post_ids as $post_id ) {
349+
$meta_value = get_post_meta( $post_id, $meta_key, true );
350+
}
351+
}
352+
$end_meta = microtime( true );
353+
$get_post_meta_time = $end_meta - $start_meta;
354+
355+
// Benchmark `WP_Meta_Query`.
356+
$start_query = microtime( true );
357+
for ( $i = 0; $i < $loop_count; $i++ ) {
358+
$meta_query = new WP_Meta_Query( [
359+
'relation' => 'OR', // Use 'OR' to handle multiple posts.
360+
array_map( function( $post_id ) use ( $meta_key, $meta_value ) {
361+
return [
362+
'key' => $meta_key,
363+
'value' => $meta_value ?? '',
364+
'compare' => '=',
365+
];
366+
}, $post_ids ),
367+
] );
368+
}
369+
$end_query = microtime( true );
370+
$meta_query_time = $end_query - $start_query;
371+
372+
// Calculate the difference.
373+
$difference = $get_post_meta_time - $meta_query_time;
374+
$faster_or_slower = $difference > 0 ? 'slower' : 'faster';
375+
376+
// Format the description based on single or multiple posts.
377+
$description = $query_type === 'single'
378+
? sprintf(
379+
esc_html__( 'Retrieving post meta for post ID %d using get_post_meta is %s by %s seconds compared to WP_Meta_Query.', 'benchpress' ),
380+
$post_ids[0],
381+
$faster_or_slower,
382+
round( abs( $difference ), 5 )
383+
)
384+
: sprintf(
385+
esc_html__( 'Retrieving post meta for %d posts using get_post_meta is %s by %s seconds compared to WP_Meta_Query.', 'benchpress' ),
386+
count( $post_ids ),
387+
$faster_or_slower,
388+
round( abs( $difference ), 5 )
389+
);
390+
391+
return [
392+
'name' => esc_html__( 'get_post_meta() vs WP_Meta_Query', 'benchpress' ),
393+
'execution_time'=> round( abs( $difference ), 5 ),
394+
'description' => $description,
395+
];
396+
}

0 commit comments

Comments
 (0)