From faa8aee53f135f29d839b8914ca89218c6eb4c6f Mon Sep 17 00:00:00 2001 From: Cole White Date: Fri, 3 Mar 2023 15:32:01 +0000 Subject: 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 --- includes/libs/Stats/Metrics/TimingMetric.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'includes/libs/Stats') 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,12 +52,38 @@ 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; $this->logger = $logger; } + /** + * 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. * -- cgit v1.2.3