Make WordPress Core

Opened 8 years ago

Closed 3 years ago

Last modified 3 years ago

#39163 closed enhancement (fixed)

Use REQUEST_TIME_FLOAT for timing

Reported by: andy's profile andy Owned by: jorbin's profile jorbin
Milestone: 5.8 Priority: normal
Severity: normal Version: 4.8
Component: Bootstrap/Load Keywords: has-patch
Focuses: performance Cc:

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 8 years ago.
medium.diff (960 bytes) - added by andy 8 years ago.
large.diff (3.7 KB) - added by andy 8 years ago.

Download all attachments as: .zip

Change History (13)

@andy
8 years ago

@andy
8 years ago

@andy
8 years ago

#1 @andy
8 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() from small.diff.

Last edited 8 years ago by andy (previous) (diff)

#2 @swissspidy
8 years ago

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

#3 @matt
8 years ago

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

#4 @jorbin
3 years ago

  • Milestone changed from Awaiting Review to 5.8

small.diff looks good except now that WordPress dropped support for PHP 5.4, we don't need the compat code

#5 @jorbin
3 years ago

  • Owner set to jorbin
  • Resolution set to fixed
  • Status changed from new to closed

In 50786:

Bootstrap/Load: Add Function for reliable timing data

Adds timer_float which can be used to get the time elapsed so far during the PHP script. Should make it easier to display the page generation time in the footer of admin.

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 and its formatted return value can not reliably be used as a number (some locales swap commas and periods, for example).

Props andy, matt, jorbin.
Fixes #39163.

#6 follow-up: @peterwilsoncc
3 years ago

@jorbin Sorry to be that person but I didn't know this existed. Is it possible to rename the function wp_timer_float() to avoid the potential for naming collisions?

I'll leave you to reopen if you're happy to rename.

#7 in reply to: ↑ 6 @SergeyBiryukov
3 years ago

Replying to peterwilsoncc:

Is it possible to rename the function wp_timer_float() to avoid the potential for naming collisions?

I think it was named without a prefix for consistency with timer_start() and timer_stop(), so the current naming would be preferable.

I've tried to search on https://wpdirectory.net/ if there are any plugins currently using timer_float(), but it looks like the search is still queued.

#8 @jorbin
3 years ago

I had kept it the same in order to stay consistent. I think it is better for these to all be consistent and we can go with the small risk that someone has used this name. I had tried to search, but ran into the same probelm as Sergey. Thankfully, wpdirectry is back up, and there are zero results for timer_float in both plugins and themes

#9 @milana_cap
3 years ago

  • Keywords needs-dev-note added
Note: See TracTickets for help on using tickets.