MediaWiki master
Timing.php
Go to the documentation of this file.
1<?php
8
9use Psr\Log\LoggerAwareInterface;
10use Psr\Log\LoggerInterface;
11use Psr\Log\NullLogger;
12
33class Timing implements LoggerAwareInterface {
34
36 private $entries = [];
37
39 protected $logger;
40
41 public function __construct( array $params = [] ) {
42 $this->clearMarks();
43 $this->setLogger( $params['logger'] ?? new NullLogger() );
44 }
45
49 public function setLogger( LoggerInterface $logger ): void {
50 $this->logger = $logger;
51 }
52
60 public function mark( $markName ) {
61 $this->entries[$markName] = [
62 'name' => $markName,
63 'entryType' => 'mark',
64 'startTime' => microtime( true ),
65 'duration' => 0,
66 ];
67 return $this->entries[$markName];
68 }
69
74 public function clearMarks( $markName = null ) {
75 if ( $markName !== null ) {
76 unset( $this->entries[$markName] );
77 } else {
78 $this->entries = [
79 'requestStart' => [
80 'name' => 'requestStart',
81 'entryType' => 'mark',
82 'startTime' => $_SERVER['REQUEST_TIME_FLOAT'],
83 'duration' => 0,
84 ],
85 ];
86 }
87 }
88
109 public function measure( $measureName, $startMark = 'requestStart', $endMark = null ) {
110 $start = $this->getEntryByName( $startMark );
111 if ( $start === null ) {
112 $this->logger->error( __METHOD__ . ": The mark '$startMark' does not exist" );
113 return false;
114 }
115 $startTime = $start['startTime'];
116
117 if ( $endMark ) {
118 $end = $this->getEntryByName( $endMark );
119 if ( $end === null ) {
120 $this->logger->error( __METHOD__ . ": The mark '$endMark' does not exist" );
121 return false;
122 }
123 $endTime = $end['startTime'];
124 } else {
125 $endTime = microtime( true );
126 }
127
128 $this->entries[$measureName] = [
129 'name' => $measureName,
130 'entryType' => 'measure',
131 'startTime' => $startTime,
132 'duration' => $endTime - $startTime,
133 ];
134
135 return $this->entries[$measureName];
136 }
137
141 private function sortEntries() {
142 uasort( $this->entries, static function ( $a, $b ) {
143 return $a['startTime'] <=> $b['startTime'];
144 } );
145 }
146
150 public function getEntries() {
151 $this->sortEntries();
152 return $this->entries;
153 }
154
160 public function getEntriesByType( $entryType ) {
161 $this->sortEntries();
162 $entries = [];
163 foreach ( $this->entries as $entry ) {
164 if ( $entry['entryType'] === $entryType ) {
165 $entries[] = $entry;
166 }
167 }
168 return $entries;
169 }
170
175 public function getEntryByName( $name ) {
176 return $this->entries[$name] ?? null;
177 }
178}
179
181class_alias( Timing::class, 'Timing' );
An interface to help developers measure the performance of their applications.
Definition Timing.php:33
__construct(array $params=[])
Definition Timing.php:41
mark( $markName)
Store a timestamp with the associated name (a "mark")
Definition Timing.php:60
LoggerInterface $logger
Definition Timing.php:39
setLogger(LoggerInterface $logger)
Sets a logger instance on the object.
Definition Timing.php:49
measure( $measureName, $startMark='requestStart', $endMark=null)
This method stores the duration between two marks along with the associated name (a "measure").
Definition Timing.php:109
getEntriesByType( $entryType)
Definition Timing.php:160
clearMarks( $markName=null)
Definition Timing.php:74