Conversation
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
wp profile queries command
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
This comment was marked as resolved.
This comment was marked as resolved.
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
This comment was marked as resolved.
This comment was marked as resolved.
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 8 out of 8 changed files in this pull request and generated 7 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| foreach ( $logger->query_indices as $query_index ) { | ||
| if ( ! isset( $query_map[ $query_index ] ) ) { | ||
| $query_map[ $query_index ] = array( | ||
| 'hook' => isset( $logger->hook ) ? $logger->hook : null, | ||
| 'callback' => isset( $logger->callback ) ? $logger->callback : null, | ||
| ); | ||
| } | ||
| } |
There was a problem hiding this comment.
[nitpick] Potential issue with query map overwriting. When multiple loggers track the same query index (e.g., nested hooks), the check ! isset( $query_map[ $query_index ] ) at line 602 prevents overwriting. This means the first logger's hook/callback will be used, but in nested scenarios, this might not be the most specific or accurate mapping. Consider whether the first-logger-wins approach is the intended behavior, or if you need to track multiple hooks/callbacks per query.
| $loggers = $profiler->get_loggers(); | ||
| foreach ( $loggers as $logger ) { | ||
| // Skip if filtering by callback and this isn't the right one | ||
| if ( $callback && isset( $logger->callback ) ) { | ||
| // Normalize callback for comparison | ||
| $normalized_callback = str_replace( array( '->', '::' ), '', (string) $logger->callback ); | ||
| $normalized_filter = str_replace( array( '->', '::' ), '', $callback ); | ||
| if ( false === stripos( $normalized_callback, $normalized_filter ) ) { | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| // Skip if filtering by hook and this isn't the right one | ||
| if ( $hook && isset( $logger->hook ) && $logger->hook !== $hook ) { | ||
| continue; | ||
| } | ||
|
|
||
| // Get the query indices for this logger | ||
| if ( isset( $logger->query_indices ) && ! empty( $logger->query_indices ) ) { | ||
| foreach ( $logger->query_indices as $query_index ) { | ||
| if ( ! isset( $query_map[ $query_index ] ) ) { | ||
| $query_map[ $query_index ] = array( | ||
| 'hook' => isset( $logger->hook ) ? $logger->hook : null, | ||
| 'callback' => isset( $logger->callback ) ? $logger->callback : null, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Get all queries | ||
| $queries = array(); | ||
| if ( ! empty( $wpdb->queries ) ) { | ||
| foreach ( $wpdb->queries as $index => $query_data ) { | ||
| // If filtering by hook/callback, only include queries in the map | ||
| if ( ( $hook || $callback ) && ! isset( $query_map[ $index ] ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| $query_obj = new QueryLogger( | ||
| $query_data[0], // SQL query | ||
| $query_data[1], // Time | ||
| isset( $query_data[2] ) ? $query_data[2] : '', // Caller | ||
| isset( $query_map[ $index ]['hook'] ) ? $query_map[ $index ]['hook'] : null, | ||
| isset( $query_map[ $index ]['callback'] ) ? $query_map[ $index ]['callback'] : null | ||
| ); | ||
| $queries[] = $query_obj; | ||
| } |
There was a problem hiding this comment.
[nitpick] Performance consideration: The nested loop structure (lines 583-610 and 616-630) could be inefficient for sites with many queries and hooks. For N loggers and M queries, this is O(N*Q + M) where Q is queries per logger. If $wpdb->queries contains thousands of entries, this might be slow. Consider optimizing by building the query map in a single pass if performance becomes an issue, though the current implementation is clear and correct for typical use cases.
…indices Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This comment was marked as resolved.
This comment was marked as resolved.
… validation Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
wp profile queriescommand structurequeries()method in Command.php--hook=<hook>parameter to filter queries for a specific hook--callback=<callback>parameter to filter queries for a specific callbackSummary
Fixed two issues causing test failures:
Hook filtering returning 0 results: When profiling a specific hook (e.g.,
--hook=init), the Profiler wraps callbacks but wasn't storing which hook they belonged to. Modifiedwrap_current_filter_callbacks()in Profiler.php to include thehookproperty in callback loggers, enabling proper filtering.PHP warning in Formatter: Added additional validation to check that both
$valueand$totals[$i]are numeric before performing addition, preventing warnings when non-numeric values slip through.Original prompt
wp profile queries#130✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.