profile-command icon indicating copy to clipboard operation
profile-command copied to clipboard

Introduce `wp profile queries` command

Open Copilot opened this issue 2 months ago • 2 comments

Adds a new wp profile queries command to display database queries executed during a WordPress request with timing information and optional filtering by hook or callback.

Changes

  • Command.php: New queries() method that runs WordPress profiling, collects queries from $wpdb->queries, and outputs them with configurable fields and filtering. Includes proper handling for both --hook and --callback parameters used together, and explicit filtering logic to skip loggers without callbacks when callback filtering is requested.
  • QueryLogger.php: Simple data container for query metadata (SQL, execution time, caller, hook, callback)
  • Logger.php: Enhanced to track query_indices array during stop(), enabling accurate query-to-hook/callback mapping. The query_indices array is now properly reset in stop() method to ensure clean state for reused loggers.
  • Formatter.php: Fixed to properly handle non-numeric fields ('query', 'caller', 'hook', 'callback') by skipping them during total calculations and adding numeric validation to prevent PHP warnings
  • Tests: Added features/profile-queries.feature with scenarios for basic listing, filtering, and output formats. Tests include custom mu-plugins that execute specific database queries to verify functionality.

Usage

# All queries with timing
wp profile queries --fields=query,time

# Queries from specific hook
wp profile queries --hook=init --fields=query,time,callback

# Queries from specific callback
wp profile queries --callback='WP_Query->get_posts()' --fields=query,time

# Filter by both hook and callback
wp profile queries --hook=init --callback=my_function --fields=query,time

# Sort by execution time
wp profile queries --orderby=time --order=DESC --fields=query,time

Filtering works by tracking which query indices each Logger executes (via query_offset range), then matching those indices to hook/callback names when filtering is requested. When both --hook and --callback are provided, the command profiles all hooks and then filters to show only queries that match both criteria.

  • Fixes wp-cli/profile-command#130
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)

  • Fixes wp-cli/profile-command#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.

Copilot avatar Nov 08 '25 10:11 Copilot

@copilot There's lots of PHP warnings "Warning: A non-numeric value encountered in src/Formatter.php on line 134" in tests for this new code. Please fix the root cause.

swissspidy avatar Nov 10 '25 17:11 swissspidy

@copilot There's lots of PHP warnings "Warning: A non-numeric value encountered in src/Formatter.php on line 134" in tests for this new code. Please fix the root cause.

Fixed by adding non-numeric field detection in Formatter.php. The code now skips string fields ('query', 'caller', 'hook', 'callback') when calculating totals and includes an is_numeric() check before summing values to prevent the warnings. (commit 28e1e4d)

Copilot avatar Nov 10 '25 17:11 Copilot