Timing.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. */
  20. use Psr\Log\LoggerAwareInterface;
  21. use Psr\Log\LoggerInterface;
  22. use Psr\Log\NullLogger;
  23. /**
  24. * An interface to help developers measure the performance of their applications.
  25. * This interface closely matches the W3C's User Timing specification.
  26. * The key differences are:
  27. *
  28. * - The reference point for all measurements which do not explicitly specify
  29. * a start time is $_SERVER['REQUEST_TIME_FLOAT'], not navigationStart.
  30. * - Successive calls to mark() and measure() with the same entry name cause
  31. * the previous entry to be overwritten. This ensures that there is a 1:1
  32. * mapping between names and entries.
  33. * - Because there is a 1:1 mapping, instead of getEntriesByName(), we have
  34. * getEntryByName().
  35. *
  36. * The in-line documentation incorporates content from the User Timing Specification
  37. * https://www.w3.org/TR/user-timing/
  38. * Copyright © 2013 World Wide Web Consortium, (MIT, ERCIM, Keio, Beihang).
  39. * https://www.w3.org/Consortium/Legal/2015/doc-license
  40. *
  41. * @since 1.27
  42. */
  43. class Timing implements LoggerAwareInterface {
  44. /** @var array[] */
  45. private $entries = [];
  46. /** @var LoggerInterface */
  47. protected $logger;
  48. public function __construct( array $params = [] ) {
  49. $this->clearMarks();
  50. $this->setLogger( $params['logger'] ?? new NullLogger() );
  51. }
  52. /**
  53. * Sets a logger instance on the object.
  54. *
  55. * @param LoggerInterface $logger
  56. * @return null
  57. */
  58. public function setLogger( LoggerInterface $logger ) {
  59. $this->logger = $logger;
  60. }
  61. /**
  62. * Store a timestamp with the associated name (a "mark")
  63. *
  64. * @param string $markName The name associated with the timestamp.
  65. * If there already exists an entry by that name, it is overwritten.
  66. * @return array The mark that has been created.
  67. */
  68. public function mark( $markName ) {
  69. $this->entries[$markName] = [
  70. 'name' => $markName,
  71. 'entryType' => 'mark',
  72. 'startTime' => microtime( true ),
  73. 'duration' => 0,
  74. ];
  75. return $this->entries[$markName];
  76. }
  77. /**
  78. * @param string|null $markName The name of the mark that should
  79. * be cleared. If not specified, all marks will be cleared.
  80. */
  81. public function clearMarks( $markName = null ) {
  82. if ( $markName !== null ) {
  83. unset( $this->entries[$markName] );
  84. } else {
  85. $this->entries = [
  86. 'requestStart' => [
  87. 'name' => 'requestStart',
  88. 'entryType' => 'mark',
  89. 'startTime' => $_SERVER['REQUEST_TIME_FLOAT'],
  90. 'duration' => 0,
  91. ],
  92. ];
  93. }
  94. }
  95. /**
  96. * This method stores the duration between two marks along with
  97. * the associated name (a "measure").
  98. *
  99. * If neither the startMark nor the endMark argument is specified,
  100. * measure() will store the duration from $_SERVER['REQUEST_TIME_FLOAT'] to
  101. * the current time.
  102. * If the startMark argument is specified, but the endMark argument is not
  103. * specified, measure() will store the duration from the most recent
  104. * occurrence of the start mark to the current time.
  105. * If both the startMark and endMark arguments are specified, measure()
  106. * will store the duration from the most recent occurrence of the start
  107. * mark to the most recent occurrence of the end mark.
  108. *
  109. * @param string $measureName
  110. * @param string $startMark
  111. * @param string|null $endMark
  112. * @return array|bool The measure that has been created, or false if either
  113. * the start mark or the end mark do not exist.
  114. */
  115. public function measure( $measureName, $startMark = 'requestStart', $endMark = null ) {
  116. $start = $this->getEntryByName( $startMark );
  117. if ( $start === null ) {
  118. $this->logger->error( __METHOD__ . ": The mark '$startMark' does not exist" );
  119. return false;
  120. }
  121. $startTime = $start['startTime'];
  122. if ( $endMark ) {
  123. $end = $this->getEntryByName( $endMark );
  124. if ( $end === null ) {
  125. $this->logger->error( __METHOD__ . ": The mark '$endMark' does not exist" );
  126. return false;
  127. }
  128. $endTime = $end['startTime'];
  129. } else {
  130. $endTime = microtime( true );
  131. }
  132. $this->entries[$measureName] = [
  133. 'name' => $measureName,
  134. 'entryType' => 'measure',
  135. 'startTime' => $startTime,
  136. 'duration' => $endTime - $startTime,
  137. ];
  138. return $this->entries[$measureName];
  139. }
  140. /**
  141. * Sort entries in chronological order with respect to startTime.
  142. */
  143. private function sortEntries() {
  144. uasort( $this->entries, function ( $a, $b ) {
  145. return $a['startTime'] <=> $b['startTime'];
  146. } );
  147. }
  148. /**
  149. * @return array[] All entries in chronological order.
  150. */
  151. public function getEntries() {
  152. $this->sortEntries();
  153. return $this->entries;
  154. }
  155. /**
  156. * @param string $entryType
  157. * @return array[] Entries (in chronological order) that have the same value
  158. * for the entryType attribute as the $entryType parameter.
  159. */
  160. public function getEntriesByType( $entryType ) {
  161. $this->sortEntries();
  162. $entries = [];
  163. foreach ( $this->entries as $entry ) {
  164. if ( $entry['entryType'] === $entryType ) {
  165. $entries[] = $entry;
  166. }
  167. }
  168. return $entries;
  169. }
  170. /**
  171. * @param string $name
  172. * @return array|null Entry named $name or null if it does not exist.
  173. */
  174. public function getEntryByName( $name ) {
  175. return $this->entries[$name] ?? null;
  176. }
  177. }