| @@ -27,13 +27,13 @@ | ||
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | 29 | * Starts an indicator to measure the performances. |
| 30 | 30 | * |
| 31 | - * @param string $id The indicator identifier. | |
| 31 | + * @param ?string $id Optional indicator identifier. | |
| 32 | 32 | * |
| 33 | - * @return string The indicator identifier. | |
| 33 | + * @return string The indicator identifier. | |
| 34 | 34 | */ |
| 35 | - public static function start(string $id = '') | |
| 35 | + public static function start(?string $id = null) | |
| 36 | 36 | { |
| 37 | 37 | if (!$id) { |
| 38 | 38 | $id = static::uuid(); |
| 39 | 39 | } |
| @@ -51,13 +51,13 @@ | ||
| 51 | 51 | |
| 52 | 52 | /** |
| 53 | 53 | * Ends an indicator to measure the performances. |
| 54 | 54 | * |
| 55 | - * @param string $id The indicator identifier. | |
| 55 | + * @param ?string $id Optional indicator identifier. | |
| 56 | 56 | * |
| 57 | - * @return array List of measurements. | |
| 57 | + * @return array List of measurements. | |
| 58 | 58 | */ |
| 59 | - public static function end(string $id = '') | |
| 59 | + public static function end(?string $id = null) | |
| 60 | 60 | { |
| 61 | 61 | if (!$id) { |
| 62 | 62 | $id = end(static::$indicators)['id'] ?? ''; |
| 63 | 63 | } |
| @@ -98,31 +98,50 @@ | ||
| 98 | 98 | |
| 99 | 99 | /** |
| 100 | 100 | * Registers a memory peak within an indicator. |
| 101 | 101 | * |
| 102 | - * @param string $id The indicator identifier. | |
| 102 | + * @param ?string $name Optional peak name for identification. | |
| 103 | + * @param ?string $id Optional indicator identifier. | |
| 104 | + * @param bool $onLast Whether to replace the last peak container. | |
| 103 | 105 | * |
| 104 | 106 | * @return void |
| 107 | + * | |
| 108 | + * @since 1.18.7 (J) - 1.8.7 (WP) added argument $name, $onLast, signature changed. | |
| 105 | 109 | */ |
| 106 | - public static function peak(string $id = '') | |
| 110 | + public static function peak(?string $name = null, ?string $id = null, bool $onLast = false) | |
| 107 | 111 | { |
| 108 | 112 | if (!$id) { |
| 109 | 113 | $id = end(static::$indicators)['id'] ?? ''; |
| 110 | 114 | } |
| 111 | 115 | |
| 112 | - $start_metrics = static::$indicators[$id] ?? []; | |
| 113 | - | |
| 114 | - if (!$start_metrics) { | |
| 116 | + if (!(static::$indicators[$id] ?? [])) { | |
| 115 | 117 | return; |
| 116 | 118 | } |
| 117 | 119 | |
| 118 | 120 | $now_memory = memory_get_usage(); |
| 119 | 121 | |
| 120 | - // push peak | |
| 121 | - static::$indicators[$id]['peaks'] = [ | |
| 122 | + // build peak container | |
| 123 | + $peakContainer = [ | |
| 124 | + 'name' => $name, | |
| 122 | 125 | 'memory' => $now_memory, |
| 123 | 126 | 'memory_r' => static::formatMemoryBytes($now_memory), |
| 124 | 127 | ]; |
| 128 | + | |
| 129 | + if ($onLast) { | |
| 130 | + // get last peak container | |
| 131 | + $lastPeak = end(static::$indicators[$id]['peaks']); | |
| 132 | + | |
| 133 | + if ($lastPeak) { | |
| 134 | + // replace last peak container | |
| 135 | + array_splice(static::$indicators[$id]['peaks'], count(static::$indicators[$id]['peaks']) - 1, 1, [$peakContainer]); | |
| 136 | + | |
| 137 | + // do not proceed | |
| 138 | + return; | |
| 139 | + } | |
| 140 | + } | |
| 141 | + | |
| 142 | + // push peak container | |
| 143 | + static::$indicators[$id]['peaks'][] = $peakContainer; | |
| 125 | 144 | } |
| 126 | 145 | |
| 127 | 146 | /** |
| 128 | 147 | * Ends all indicators and returns them. |
| @@ -130,9 +149,9 @@ | ||
| 130 | 149 | * @param bool $end True to end all indicators before returning them. |
| 131 | 150 | * |
| 132 | 151 | * @return void |
| 133 | 152 | */ |
| 134 | - public static function getAll($end = true) | |
| 153 | + public static function getAll(bool $end = true) | |
| 135 | 154 | { |
| 136 | 155 | $list = []; |
| 137 | 156 | |
| 138 | 157 | foreach (static::$indicators as $id => $indicator) { |