21use Psr\Log\LoggerAwareInterface;
22use Psr\Log\LoggerInterface;
23use Psr\Log\NullLogger;
45class Timing implements LoggerAwareInterface {
55 $this->
setLogger( $params[
'logger'] ??
new NullLogger() );
75 public function mark( $markName ) {
76 $this->entries[$markName] = [
78 'entryType' =>
'mark',
79 'startTime' => microtime(
true ),
82 return $this->entries[$markName];
90 if ( $markName !==
null ) {
91 unset( $this->entries[$markName] );
95 'name' =>
'requestStart',
96 'entryType' =>
'mark',
97 'startTime' => $_SERVER[
'REQUEST_TIME_FLOAT'],
124 public function measure( $measureName, $startMark =
'requestStart', $endMark =
null ) {
126 if ( $start ===
null ) {
127 $this->logger->error( __METHOD__ .
": The mark '$startMark' does not exist" );
130 $startTime = $start[
'startTime'];
134 if ( $end ===
null ) {
135 $this->logger->error( __METHOD__ .
": The mark '$endMark' does not exist" );
138 $endTime = $end[
'startTime'];
140 $endTime = microtime(
true );
143 $this->entries[$measureName] = [
144 'name' => $measureName,
145 'entryType' =>
'measure',
146 'startTime' => $startTime,
147 'duration' => $endTime - $startTime,
150 return $this->entries[$measureName];
157 uasort( $this->entries,
function ( $a, $b ) {
158 return $a[
'startTime'] <=> $b[
'startTime'];
178 foreach ( $this->entries as $entry ) {
179 if ( $entry[
'entryType'] === $entryType ) {
191 return $this->entries[$name] ??
null;
An interface to help developers measure the performance of their applications.
sortEntries()
Sort entries in chronological order with respect to startTime.
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=[])