Skip to content

Introduce wp profile queries command#207

Draft
Copilot wants to merge 16 commits intomainfrom
copilot/add-wp-profile-queries
Draft

Introduce wp profile queries command#207
Copilot wants to merge 16 commits intomainfrom
copilot/add-wp-profile-queries

Conversation

Copy link
Contributor

Copilot AI commented Nov 8, 2025

  • Understand the codebase structure and how profiling works
  • Design the wp profile queries command structure
  • Implement the core queries() method in Command.php
  • Add support for viewing all queries with execution time
  • Add --hook=<hook> parameter to filter queries for a specific hook
  • Add --callback=<callback> parameter to filter queries for a specific callback
  • Add formatting support (table, json, yaml, csv)
  • Add ordering support (--order, --orderby)
  • Add tests for the new command including hook/callback filtering
  • Enhance Logger to track query indices
  • Update documentation in README.md
  • Run code review and security checks
  • Improve first test to add custom plugin with database queries
  • Fix PHP warnings for non-numeric values in Formatter
  • Address code review feedback (callback filtering, query_indices reset, handle both params)
  • Fix test failures (add hook property to callback loggers, improve Formatter validation)
  • Change query map to use last-logger-wins approach for better specificity in nested hooks
  • Document performance characteristics and complexity of query mapping
  • Implementation complete

Summary

Added documentation comment explaining the O(N*Q + M) complexity of the query mapping logic, noting that it performs well for typical WordPress sites with array-based lookups. The current implementation prioritizes code clarity and correctness for typical use cases.

Original prompt

This section details on the original issue you should resolve

<issue_title>Introduce wp profile queries</issue_title>
<issue_description>It would be helpful to be able to see all queries and how much time they're taking.

Also make it possible to show actual queries for (a) a specific callback or (b) all queries within a specific hook</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI self-assigned this Nov 8, 2025
Copilot AI and others added 3 commits November 8, 2025 10:36
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>
Copilot AI changed the title [WIP] Introduce wp profile queries to monitor query performance Introduce wp profile queries command Nov 8, 2025
Copilot AI requested a review from swissspidy November 8, 2025 10:42
@swissspidy swissspidy requested a review from Copilot November 8, 2025 14:23

This comment was marked as resolved.

swissspidy and others added 3 commits November 8, 2025 15:27
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>
@swissspidy

This comment was marked as resolved.

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

This comment was marked as resolved.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines 582 to 630
$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;
}
Copy link

Copilot AI Nov 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added documentation comment explaining the O(N*Q + M) complexity and noting that the implementation performs well for typical WordPress sites. The current approach prioritizes code clarity and uses array-based lookups for efficiency. (commit 91c69cf)

…indices

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@swissspidy

This comment was marked as resolved.

… validation

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

This comment was marked as resolved.

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Introduce wp profile queries

2 participants