MediaWiki  1.29.1
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( isset( $params['logger'] ) ? $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' => isset( $_SERVER['REQUEST_TIME_FLOAT'] )
98  ? $_SERVER['REQUEST_TIME_FLOAT']
99  : $_SERVER['REQUEST_TIME'],
100  'duration' => 0,
101  ],
102  ];
103  }
104  }
105 
126  public function measure( $measureName, $startMark = 'requestStart', $endMark = null ) {
127  $start = $this->getEntryByName( $startMark );
128  if ( $start === null ) {
129  $this->logger->error( __METHOD__ . ": The mark '$startMark' does not exist" );
130  return false;
131  }
132  $startTime = $start['startTime'];
133 
134  if ( $endMark ) {
135  $end = $this->getEntryByName( $endMark );
136  if ( $end === null ) {
137  $this->logger->error( __METHOD__ . ": The mark '$endMark' does not exist" );
138  return false;
139  }
140  $endTime = $end['startTime'];
141  } else {
142  $endTime = microtime( true );
143  }
144 
145  $this->entries[$measureName] = [
146  'name' => $measureName,
147  'entryType' => 'measure',
148  'startTime' => $startTime,
149  'duration' => $endTime - $startTime,
150  ];
151 
152  return $this->entries[$measureName];
153  }
154 
158  private function sortEntries() {
159  uasort( $this->entries, function ( $a, $b ) {
160  return 10000 * ( $a['startTime'] - $b['startTime'] );
161  } );
162  }
163 
167  public function getEntries() {
168  $this->sortEntries();
169  return $this->entries;
170  }
171 
177  public function getEntriesByType( $entryType ) {
178  $this->sortEntries();
179  $entries = [];
180  foreach ( $this->entries as $entry ) {
181  if ( $entry['entryType'] === $entryType ) {
182  $entries[] = $entry;
183  }
184  }
185  return $entries;
186  }
187 
192  public function getEntryByName( $name ) {
193  return isset( $this->entries[$name] ) ? $this->entries[$name] : null;
194  }
195 }
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
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$params
$params
Definition: styleTest.css.php:40
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:304
Timing\setLogger
setLogger(LoggerInterface $logger)
Sets a logger instance on the object.
Definition: Timing.php:64
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Timing\getEntriesByType
getEntriesByType( $entryType)
Definition: Timing.php:177
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:126
Timing\getEntries
getEntries()
Definition: Timing.php:167
Timing\getEntryByName
getEntryByName( $name)
Definition: Timing.php:192
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:158
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Timing\$entries
array[] $entries
Definition: Timing.php:48
array
the array() calling protocol came about after MediaWiki 1.4rc1.