WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 8 months ago

#39163 new enhancement

Use REQUEST_TIME_FLOAT for timing

Reported by: andy Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.8
Component: Bootstrap/Load Keywords: has-patch
Focuses: performance Cc:
PR Number:

Description

$_SERVER['REQUEST_TIME_FLOAT']

The timestamp of the start of the request, with microsecond precision. Available since PHP 5.4.0.

Related: #26874 timer_stop() returns a string, not a float

WordPress should expose timing data as a float in the most accurate manner possible. timer_stop() has two problems: it uses an initial timestamp generated later than it needs to be (50ms late is what I have seen most often), and its formatted return value can not reliably be used as a number (some locales swap commas and periods, for example).

As timer_stop() is a well-established formatting function, a new function is needed for getting the raw data. The new timer_float() will simply return microtime( true ) - $_SERVER['REQUEST_TIME_FLOAT'];. For compatibility with PHP < 5.4, the initial value is conditionally set at the top of load.php. This is all I've done in small.diff, leaving timer_stop() alone.

My goal for this ticket is to get timer_float() in core so we can start using it for accurate and reliable timing data to support efforts to manage and improve performance. I don't really care whether any changes are made to timer_start() and timer_stop(). However, I included them in patches to save time just in case we want them updated.

medium.diff changes timer_start() to use REQUEST_TIME_FLOAT. This will make measurements more accurate without changing any other semantics.

large.diff bypasses timer_start() and automatically starts the timer as soon as possible, i.e. at the top of load.php. This patch tags timer_start() as deprecated but we should not move it to deprecated.php because drop-ins might expect timer_start() to exist, as might any auxiliary scripts that directly include load.php. (I am not familiar with deprecation strategy generally so I expect feedback on this.) The legacy globals, $timestart and its cousin that hopefully nobody ever used, $timeend, are both supported in this patch. (I think they should be deprecated as soon as we bump to 5.4.)

Attachments (3)

small.diff (787 bytes) - added by andy 3 years ago.
medium.diff (960 bytes) - added by andy 3 years ago.
large.diff (3.7 KB) - added by andy 3 years ago.

Download all attachments as: .zip

Change History (6)

@andy
3 years ago

@andy
3 years ago

@andy
3 years ago

#1 @andy
3 years ago

After more thorough review of auxiliary code, I can not recommend medium.diff or large.diff.

Some scripts use timer_start() and timer_stop() for timing things other than the main request.

Best just to leave those alone and add timer_float().

Version 1, edited 3 years ago by andy (previous) (next) (diff)

#2 @swissspidy
3 years ago

  • Keywords has-patch added
  • Type changed from defect (bug) to enhancement

#3 @matt
3 years ago

Makes me miss when we had page generation time in the footer of admin.

Note: See TracTickets for help on using tickets.