Add `wp profile requests` command to monitor HTTP requests
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_timeand$request_argsproperties 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, $urlfor pre_http_request;$response, $context, $class, $parsed_args, $urlfor http_api_debug) - Skip 'all' hook registration when profiling requests to avoid overhead
- Added proper null checks for
$parsed_argsto prevent PHP warnings - Reset tracking properties at the start of each request to handle preempted requests (where
pre_http_requestfilter returns early andhttp_api_debugdoesn't fire)
composer.json
- Registered
profile requestscommand
Tests
- Added
features/profile-requests.featurewith test scenarios including a mu-plugin that makes HTTP requests with HTTP response mocking to prevent actual external requests - Updated
features/profile.featureusage 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.
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!