Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 7, 2025

  • Understand existing HTTP request tracking in Profiler.php
  • Create new requests() method in Command.php to display HTTP request metrics
  • Add support for a new profiler type 'request' to track individual HTTP requests
  • Update Profiler class to track individual HTTP request details (URL, method, time, status)
  • Add tests for the new wp profile requests command
  • Update composer.json to register the new command
  • Update test to include requests command in usage output
  • Optimize profiler to not add unnecessary hooks for request profiling
  • Fix hook parameter signatures to match WordPress
  • Run code review and address feedback
  • Run security checks
  • Improve test with mu-plugin that makes actual HTTP requests
  • Add HTTP mocking to prevent real HTTP requests in tests
  • Handle preempted HTTP requests by resetting tracking properties
  • Fix handling of preempted/mocked requests
  • Fix PHP warning for non-numeric fields in Formatter

Bug Fix

Fixed PHP warning "A non-numeric value encountered" by checking if values are numeric before attempting to sum them in Formatter.php. String fields like 'method', 'url', and 'status' are now properly skipped when calculating totals.

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:

WP_CLI::add_wp_hook( 'pre_http_request', array( $this, 'wp_request_begin' ) );
WP_CLI::add_wp_hook( 'http_api_debug', array( $this, 'wp_request_end' ) );
</comment_new>


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

Copilot AI changed the title [WIP] Add wp profile requests command for monitoring HTTP request timing Add wp profile requests command to monitor HTTP requests Nov 7, 2025
Copilot AI requested a review from swissspidy November 7, 2025 12:33
@codecov
Copy link

codecov bot commented Nov 7, 2025

Codecov Report

❌ Patch coverage is 72.13115% with 17 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Profiler.php 63.82% 17 Missing ⚠️

📢 Thoughts on this report? Let us know!

This comment was marked as resolved.

swissspidy and others added 2 commits November 10, 2025 22:16
@swissspidy

This comment was marked as resolved.

This comment was marked as resolved.

@swissspidy
Copy link
Member

@copilot but those parameters are all unused, that can't be it. please check again

This comment was marked as resolved.

@swissspidy

This comment was marked as resolved.

This comment was marked as resolved.

@swissspidy

This comment was marked as resolved.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a new wp profile requests command to monitor HTTP requests during the WordPress load process. The implementation is well-executed, including comprehensive Behat tests, updates to command registration, and the core profiling logic in Profiler.php. The changes correctly utilize WordPress hooks to track requests and thoughtfully handle preempted/mocked scenarios. A small but important fix in Formatter.php prevents PHP warnings with non-numeric data. My main feedback is to address some code duplication in Profiler.php to enhance maintainability. Overall, this is a valuable addition to the profiling command suite.

Comment on lines +446 to +471
if ( 'request' === $this->type && ! is_null( $this->request_start_time ) ) {
$request_time = microtime( true ) - $this->request_start_time;
$status = '';

// Extract status code from response
if ( is_wp_error( $response ) ) {
$status = 'Error';
} elseif ( is_array( $response ) && isset( $response['response']['code'] ) ) {
$status = $response['response']['code'];
}

$logger = new Logger(
array(
'method' => $this->request_args['method'],
'url' => $this->request_args['url'],
'status' => $status,
)
);
$logger->time = $request_time;

$this->loggers[] = $logger;

// Reset for next request
$this->request_start_time = null;
$this->request_args = null;
}

Choose a reason for hiding this comment

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

medium

The logic inside this if block is very similar to the logic for handling preempted requests in wp_request_begin. To improve maintainability, consider extracting this duplicated code into a private helper method for logging HTTP requests. This would centralize the logic for creating a Logger instance, assigning properties, and adding it to the loggers array.

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 requests

2 participants