21use Psr\Log\LoggerAwareInterface;
22use Psr\Log\LoggerInterface;
23use Psr\Log\NullLogger;
45class Timing implements LoggerAwareInterface {
48 private $entries = [];
63 public function setLogger( LoggerInterface $logger ) {
64 $this->logger = $logger;
74 public function mark( $markName ) {
75 $this->entries[$markName] = [
77 'entryType' =>
'mark',
78 'startTime' => microtime(
true ),
81 return $this->entries[$markName];
89 if ( $markName !==
null ) {
90 unset( $this->entries[$markName] );
94 'name' =>
'requestStart',
95 'entryType' =>
'mark',
96 'startTime' => $_SERVER[
'REQUEST_TIME_FLOAT'],
123 public function measure( $measureName, $startMark =
'requestStart', $endMark =
null ) {
125 if ( $start ===
null ) {
126 $this->logger->error( __METHOD__ .
": The mark '$startMark' does not exist" );
129 $startTime = $start[
'startTime'];
133 if ( $end ===
null ) {
134 $this->logger->error( __METHOD__ .
": The mark '$endMark' does not exist" );
137 $endTime = $end[
'startTime'];
139 $endTime = microtime(
true );
142 $this->entries[$measureName] = [
143 'name' => $measureName,
144 'entryType' =>
'measure',
145 'startTime' => $startTime,
146 'duration' => $endTime - $startTime,
149 return $this->entries[$measureName];
155 private function sortEntries() {
156 uasort( $this->entries,
static function ( $a, $b ) {
157 return $a[
'startTime'] <=> $b[
'startTime'];
165 $this->sortEntries();
166 return $this->entries;
175 $this->sortEntries();
177 foreach ( $this->entries as $entry ) {
178 if ( $entry[
'entryType'] === $entryType ) {
190 return $this->entries[$name] ??
null;
array $params
The job parameters.
An interface to help developers measure the performance of their applications.
clearMarks( $markName=null)
mark( $markName)
Store a timestamp with the associated name (a "mark")
getEntriesByType( $entryType)
measure( $measureName, $startMark='requestStart', $endMark=null)
This method stores the duration between two marks along with the associated name (a "measure").
setLogger(LoggerInterface $logger)
Sets a logger instance on the object.
__construct(array $params=[])