MediaWiki REL1_32
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
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}
An interface to help developers measure the performance of their applications.
Definition Timing.php:45
array[] $entries
Definition Timing.php:48
sortEntries()
Sort entries in chronological order with respect to startTime.
Definition Timing.php:156
clearMarks( $markName=null)
Definition Timing.php:89
mark( $markName)
Store a timestamp with the associated name (a "mark")
Definition Timing.php:75
LoggerInterface $logger
Definition Timing.php:51
getEntriesByType( $entryType)
Definition Timing.php:175
getEntryByName( $name)
Definition Timing.php:190
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
setLogger(LoggerInterface $logger)
Sets a logger instance on the object.
Definition Timing.php:64
getEntries()
Definition Timing.php:165
__construct(array $params=[])
Definition Timing.php:53
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:302
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
$params