Problem
We currently use microtime(true) quite a lot. This internally uses gettimeofday() in C, which isn't monotonic and subject to clock drift etc.
Impact
The metrics we collect for WANObjectCache, ResourceLoader, Backend Save Timing etc. are all affected by this and thus cannot be trusted. Their mean average might give a rough sense of how long something takes under "boring" circumstances, but it's not a useful way to set alerts because we can't trust the outliers.
For backend metrics it's important that we monitor the p99 and max, not the mean average (why). However, due to clock drift, the metrics that fire these alerts are difficult to trust (e.g. T242024 and T233059).
Examples:
- https://grafana.wikimedia.org/d/lqE4lcGWz/wanobjectcache-key-group
- https://grafana.wikimedia.org/d/000000066/resourceloader
- https://grafana.wikimedia.org/d/000000429/backend-save-timing-breakdown
Requirements
PHP 7.3+ introduces hrtime() for this purpose, which internally uses clock_gettime(CLOCK_MONOTONIC) in C. This is comparable to performance.now() in web browsers, and process.hrtime() in Node.js.
We'll need to provide a way to get access to the monotonic timer in within the MediaWiki run-time.
Proposal
It looks like it will probably take at least a year before we run MediaWiki on PHP 7.3 by default at WMF. Both for that reason and for third parties, I suggest we invest in supporting a way on PHP 7.2 to get monotonic timers.
Assuming this can't be done in pure PHP, it will require a PHP extension (either from Pecl or to developer our own).
We would then conditionally define hrtime() if absent to use that instead (for older PHP versions), with ultimate falllback to microtime(true) if the user is neither PHP 7.3 nor the extension in question.