X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Flibs%2FTiming.php;h=62baaf1e93f522ee5c3e5e8215248a1491c83057;hb=5ffbb247aeb517bd72d8e53dd601eabd3a68b01b;hp=653227e1b49d8e8c0759b1cf2fc41a6cbd26fffe;hpb=38ba6b620be9f6333d902055ae1c0c610af4985e;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/libs/Timing.php b/includes/libs/Timing.php index 653227e1b4..62baaf1e93 100644 --- a/includes/libs/Timing.php +++ b/includes/libs/Timing.php @@ -18,6 +18,10 @@ * @file */ +use Psr\Log\LoggerAwareInterface; +use Psr\Log\LoggerInterface; +use Psr\Log\NullLogger; + /** * An interface to help developers measure the performance of their applications. * This interface closely matches the W3C's User Timing specification. @@ -38,13 +42,27 @@ * * @since 1.27 */ -class Timing { +class Timing implements LoggerAwareInterface { /** @var array[] */ - private $entries = array(); + private $entries = []; + + /** @var LoggerInterface */ + protected $logger; - public function __construct() { + public function __construct( array $params = [] ) { $this->clearMarks(); + $this->setLogger( isset( $params['logger'] ) ? $params['logger'] : new NullLogger() ); + } + + /** + * Sets a logger instance on the object. + * + * @param LoggerInterface $logger + * @return null + */ + public function setLogger( LoggerInterface $logger ) { + $this->logger = $logger; } /** @@ -55,12 +73,12 @@ class Timing { * @return array The mark that has been created. */ public function mark( $markName ) { - $this->entries[$markName] = array( + $this->entries[$markName] = [ 'name' => $markName, 'entryType' => 'mark', 'startTime' => microtime( true ), 'duration' => 0, - ); + ]; return $this->entries[$markName]; } @@ -72,16 +90,16 @@ class Timing { if ( $markName !== null ) { unset( $this->entries[$markName] ); } else { - $this->entries = array( - 'requestStart' => array( + $this->entries = [ + 'requestStart' => [ 'name' => 'requestStart', 'entryType' => 'mark', 'startTime' => isset( $_SERVER['REQUEST_TIME_FLOAT'] ) ? $_SERVER['REQUEST_TIME_FLOAT'] : $_SERVER['REQUEST_TIME'], 'duration' => 0, - ), - ); + ], + ]; } } @@ -102,25 +120,34 @@ class Timing { * @param string $measureName * @param string $startMark * @param string $endMark - * @return array The measure that has been created. + * @return array|bool The measure that has been created, or false if either + * the start mark or the end mark do not exist. */ public function measure( $measureName, $startMark = 'requestStart', $endMark = null ) { $start = $this->getEntryByName( $startMark ); + if ( $start === null ) { + $this->logger->error( __METHOD__ . ": The mark '$startMark' does not exist" ); + return false; + } $startTime = $start['startTime']; if ( $endMark ) { $end = $this->getEntryByName( $endMark ); + if ( $end === null ) { + $this->logger->error( __METHOD__ . ": The mark '$endMark' does not exist" ); + return false; + } $endTime = $end['startTime']; } else { $endTime = microtime( true ); } - $this->entries[$measureName] = array( + $this->entries[$measureName] = [ 'name' => $measureName, 'entryType' => 'measure', 'startTime' => $startTime, 'duration' => $endTime - $startTime, - ); + ]; return $this->entries[$measureName]; } @@ -149,7 +176,7 @@ class Timing { */ public function getEntriesByType( $entryType ) { $this->sortEntries(); - $entries = array(); + $entries = []; foreach ( $this->entries as $entry ) { if ( $entry['entryType'] === $entryType ) { $entries[] = $entry;