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