#39163 closed enhancement (fixed)
Use REQUEST_TIME_FLOAT for timing
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| 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)
Change History (13)
#4
@
5 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
@
5 years ago
- Owner set to jorbin
- Resolution set to fixed
- Status changed from new to closed
In 50786:
#6
follow-up:
↓ 7
@
5 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
@
5 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
@
5 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
After more thorough review of auxiliary code, I can not recommend medium.diff or large.diff.
Some scripts use
timer_start()andtimer_stop()for timing things other than the main request.Best just to leave those alone and add
timer_float()from small.diff.