MediaWiki master
Timing.php
Go to the documentation of this file.
1<?php
21use Psr\Log\LoggerAwareInterface;
22use Psr\Log\LoggerInterface;
23use Psr\Log\NullLogger;
24
45class Timing implements LoggerAwareInterface {
46
48 private $entries = [];
49
51 protected $logger;
52
53 public function __construct( array $params = [] ) {
54 $this->clearMarks();
55 $this->setLogger( $params['logger'] ?? new NullLogger() );
56 }
57
61 public function setLogger( LoggerInterface $logger ) {
62 $this->logger = $logger;
63 }
64
72 public function mark( $markName ) {
73 $this->entries[$markName] = [
74 'name' => $markName,
75 'entryType' => 'mark',
76 'startTime' => microtime( true ),
77 'duration' => 0,
78 ];
79 return $this->entries[$markName];
80 }
81
86 public function clearMarks( $markName = null ) {
87 if ( $markName !== null ) {
88 unset( $this->entries[$markName] );
89 } else {
90 $this->entries = [
91 'requestStart' => [
92 'name' => 'requestStart',
93 'entryType' => 'mark',
94 'startTime' => $_SERVER['REQUEST_TIME_FLOAT'],
95 'duration' => 0,
96 ],
97 ];
98 }
99 }
100
121 public function measure( $measureName, $startMark = 'requestStart', $endMark = null ) {
122 $start = $this->getEntryByName( $startMark );
123 if ( $start === null ) {
124 $this->logger->error( __METHOD__ . ": The mark '$startMark' does not exist" );
125 return false;
126 }
127 $startTime = $start['startTime'];
128
129 if ( $endMark ) {
130 $end = $this->getEntryByName( $endMark );
131 if ( $end === null ) {
132 $this->logger->error( __METHOD__ . ": The mark '$endMark' does not exist" );
133 return false;
134 }
135 $endTime = $end['startTime'];
136 } else {
137 $endTime = microtime( true );
138 }
139
140 $this->entries[$measureName] = [
141 'name' => $measureName,
142 'entryType' => 'measure',
143 'startTime' => $startTime,
144 'duration' => $endTime - $startTime,
145 ];
146
147 return $this->entries[$measureName];
148 }
149
153 private function sortEntries() {
154 uasort( $this->entries, static function ( $a, $b ) {
155 return $a['startTime'] <=> $b['startTime'];
156 } );
157 }
158
162 public function getEntries() {
163 $this->sortEntries();
164 return $this->entries;
165 }
166
172 public function getEntriesByType( $entryType ) {
173 $this->sortEntries();
174 $entries = [];
175 foreach ( $this->entries as $entry ) {
176 if ( $entry['entryType'] === $entryType ) {
177 $entries[] = $entry;
178 }
179 }
180 return $entries;
181 }
182
187 public function getEntryByName( $name ) {
188 return $this->entries[$name] ?? null;
189 }
190}
An interface to help developers measure the performance of their applications.
Definition Timing.php:45
clearMarks( $markName=null)
Definition Timing.php:86
mark( $markName)
Store a timestamp with the associated name (a "mark")
Definition Timing.php:72
LoggerInterface $logger
Definition Timing.php:51
getEntriesByType( $entryType)
Definition Timing.php:172
getEntryByName( $name)
Definition Timing.php:187
measure( $measureName, $startMark='requestStart', $endMark=null)
This method stores the duration between two marks along with the associated name (a "measure").
Definition Timing.php:121
setLogger(LoggerInterface $logger)
Sets a logger instance on the object.
Definition Timing.php:61
getEntries()
Definition Timing.php:162
__construct(array $params=[])
Definition Timing.php:53