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
63 public function setLogger( LoggerInterface $logger ) {
64 $this->logger = $logger;
65 }
66
74 public function mark( $markName ) {
75 $this->entries[$markName] = [
76 'name' => $markName,
77 'entryType' => 'mark',
78 'startTime' => microtime( true ),
79 'duration' => 0,
80 ];
81 return $this->entries[$markName];
82 }
83
88 public function clearMarks( $markName = null ) {
89 if ( $markName !== null ) {
90 unset( $this->entries[$markName] );
91 } else {
92 $this->entries = [
93 'requestStart' => [
94 'name' => 'requestStart',
95 'entryType' => 'mark',
96 'startTime' => $_SERVER['REQUEST_TIME_FLOAT'],
97 'duration' => 0,
98 ],
99 ];
100 }
101 }
102
123 public function measure( $measureName, $startMark = 'requestStart', $endMark = null ) {
124 $start = $this->getEntryByName( $startMark );
125 if ( $start === null ) {
126 $this->logger->error( __METHOD__ . ": The mark '$startMark' does not exist" );
127 return false;
128 }
129 $startTime = $start['startTime'];
130
131 if ( $endMark ) {
132 $end = $this->getEntryByName( $endMark );
133 if ( $end === null ) {
134 $this->logger->error( __METHOD__ . ": The mark '$endMark' does not exist" );
135 return false;
136 }
137 $endTime = $end['startTime'];
138 } else {
139 $endTime = microtime( true );
140 }
141
142 $this->entries[$measureName] = [
143 'name' => $measureName,
144 'entryType' => 'measure',
145 'startTime' => $startTime,
146 'duration' => $endTime - $startTime,
147 ];
148
149 return $this->entries[$measureName];
150 }
151
155 private function sortEntries() {
156 uasort( $this->entries, static function ( $a, $b ) {
157 return $a['startTime'] <=> $b['startTime'];
158 } );
159 }
160
164 public function getEntries() {
165 $this->sortEntries();
166 return $this->entries;
167 }
168
174 public function getEntriesByType( $entryType ) {
175 $this->sortEntries();
176 $entries = [];
177 foreach ( $this->entries as $entry ) {
178 if ( $entry['entryType'] === $entryType ) {
179 $entries[] = $entry;
180 }
181 }
182 return $entries;
183 }
184
189 public function getEntryByName( $name ) {
190 return $this->entries[$name] ?? null;
191 }
192}
array $params
The job parameters.
An interface to help developers measure the performance of their applications.
Definition Timing.php:45
clearMarks( $markName=null)
Definition Timing.php:88
mark( $markName)
Store a timestamp with the associated name (a "mark")
Definition Timing.php:74
LoggerInterface $logger
Definition Timing.php:51
getEntriesByType( $entryType)
Definition Timing.php:174
getEntryByName( $name)
Definition Timing.php:189
measure( $measureName, $startMark='requestStart', $endMark=null)
This method stores the duration between two marks along with the associated name (a "measure").
Definition Timing.php:123
setLogger(LoggerInterface $logger)
Sets a logger instance on the object.
Definition Timing.php:63
getEntries()
Definition Timing.php:164
__construct(array $params=[])
Definition Timing.php:53