21 use Psr\Log\LoggerAwareInterface;
22 use Psr\Log\LoggerInterface;
23 use Psr\Log\NullLogger;
45 class 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;