MediaWiki  1.33.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
$params
$params
Definition: styleTest.css.php:44
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: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
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
array
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))
Timing\getEntryByName
getEntryByName( $name)
Definition: Timing.php:190
Timing\__construct
__construct(array $params=[])
Definition: Timing.php:53
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
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
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