diff options
author | Cole White <cwhite@wikimedia.org> | 2023-03-03 15:32:01 +0000 |
---|---|---|
committer | Cole White <cwhite@wikimedia.org> | 2023-03-06 18:40:32 +0000 |
commit | faa8aee53f135f29d839b8914ca89218c6eb4c6f (patch) | |
tree | d62297c869e303fb94936b678a8d32b43d9c8a2f /includes/libs/Stats | |
parent | 216ca9ebe16311d66bbffde5911839de11f860bf (diff) | |
download | mediawikicore-faa8aee53f135f29d839b8914ca89218c6eb4c6f.tar.gz mediawikicore-faa8aee53f135f29d839b8914ca89218c6eb4c6f.zip |
Stats: add timing start and stop helper functions
Adds a start() and stop() helper functions to simplfy measuring the timing value.
Bug: T240685
Change-Id: Ifecb4ffc679dc46b9806aecb8b947b67c7884a1b
Diffstat (limited to 'includes/libs/Stats')
-rw-r--r-- | includes/libs/Stats/Metrics/TimingMetric.php | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/includes/libs/Stats/Metrics/TimingMetric.php b/includes/libs/Stats/Metrics/TimingMetric.php index 1c9a38697448..545dbdf9bd40 100644 --- a/includes/libs/Stats/Metrics/TimingMetric.php +++ b/includes/libs/Stats/Metrics/TimingMetric.php @@ -52,6 +52,9 @@ class TimingMetric implements MetricInterface { /** @var LoggerInterface */ private LoggerInterface $logger; + /** @var float|null */ + private ?float $startTime = null; + /** @inheritDoc */ public function __construct( $baseMetric, $logger ) { $this->baseMetric = $baseMetric; @@ -59,6 +62,29 @@ class TimingMetric implements MetricInterface { } /** + * Starts a timer. + * + * @return void + */ + public function start(): void { + $this->startTime = microtime( true ); + } + + /** + * Stops a running timer. + * + * @return void + */ + public function stop(): void { + if ( $this->startTime === null ) { + trigger_error( "Stats: stop() called before start() for metric '{$this->getName()}'", E_USER_WARNING ); + return; + } + $this->observe( ( microtime( true ) - $this->startTime ) * 1000 ); + $this->startTime = null; + } + + /** * Records a previously calculated observation. * * @param float $value |