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

Add `wp profile requests` command to monitor HTTP requests

Open Copilot opened this issue 2 months ago • 1 comments

Implements wp profile requests to display all HTTP requests made during WordPress load, showing method, URL, status code, and execution time for each request.

Changes

Command.php

  • Added requests() method with standard profiling options (--url, --fields, --format, --order, --orderby)
  • Displays fields: method, url, status, time

Profiler.php

  • Added $request_start_time and $request_args properties to track individual requests
  • Enhanced wp_request_begin() to capture request details (URL, method) when type='request'
  • Enhanced wp_request_end() to create Logger entries with timing and status from response
  • Updated hook parameters to match WordPress signatures ($preempt, $parsed_args, $url for pre_http_request; $response, $context, $class, $parsed_args, $url for http_api_debug)
  • Skip 'all' hook registration when profiling requests to avoid overhead
  • Added proper null checks for $parsed_args to prevent PHP warnings
  • Reset tracking properties at the start of each request to handle preempted requests (where pre_http_request filter returns early and http_api_debug doesn't fire)

composer.json

  • Registered profile requests command

Tests

  • Added features/profile-requests.feature with test scenarios including a mu-plugin that makes HTTP requests with HTTP response mocking to prevent actual external requests
  • Updated features/profile.feature usage output

Example

$ wp profile requests --fields=method,url,status,time
+-----------+----------------------------+--------+---------+
| method    | url                        | status | time    |
+-----------+----------------------------+--------+---------+
| GET       | https://api.example.com    | 200    | 0.2341s |
| POST      | https://api.example.com    | 201    | 0.1653s |
+-----------+----------------------------+--------+---------+
| total (2) |                            |        | 0.3994s |
+-----------+----------------------------+--------+---------+

Fixes #129

Original prompt

This section details on the original issue you should resolve

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

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

@danielbachhuber The idea is to create a `wp profile requests` command that would minor execution time, etc. for all HTTP requests made during the WordPress load process.

Here's where we're already hooking into the HTTP request process to observe timing:

https://github.com/wp-cli/profile-command/blob/d21d2d915b7c81c2b0cb7e4bb9af1f9cff45f429/inc/class-profiler.php#L115-L116</comment_new>

  • Fixes wp-cli/profile-command#129

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot avatar Nov 07 '25 12:11 Copilot

Codecov Report

:x: Patch coverage is 52.38095% with 20 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Profiler.php 31.03% 20 Missing :warning:

:loudspeaker: Thoughts on this report? Let us know!

codecov[bot] avatar Nov 07 '25 12:11 codecov[bot]