PluginProbe
VikBooking Hotel Booking Engine & PMS / trunk
VikBooking Hotel Booking Engine & PMS vtrunk
1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.7.3 All 36 releases
vikbooking / admin / helpers / src / performance / indicator.php

indicator.php in VikBooking Hotel Booking Engine & PMS trunk, at admin/helpers/src/performance/indicator.php

228 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package VikBooking
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2024 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * Performance Indicator implementation.
16 *
17 * @since 1.16.10 (J) - 1.6.10 (WP)
18 */
19 final class VBOPerformanceIndicator
20 {
21 /**
22 * Indicators pool.
23 *
24 * @var array
25 */
26 private static $indicators = [];
27
28 /**
29 * Starts an indicator to measure the performances.
30 *
31 * @param ?string $id Optional indicator identifier.
32 *
33 * @return string The indicator identifier.
34 */
35 public static function start(?string $id = null)
36 {
37 if (!$id) {
38 $id = static::uuid();
39 }
40
41 static::$indicators[$id] = [
42 'id' => $id,
43 'memory' => memory_get_usage(),
44 'memory_r' => static::formatMemoryBytes(memory_get_usage()),
45 'peaks' => [],
46 'ms_start' => microtime(true),
47 ];
48
49 return $id;
50 }
51
52 /**
53 * Ends an indicator to measure the performances.
54 *
55 * @param ?string $id Optional indicator identifier.
56 *
57 * @return array List of measurements.
58 */
59 public static function end(?string $id = null)
60 {
61 if (!$id) {
62 $id = end(static::$indicators)['id'] ?? '';
63 }
64
65 $start_metrics = static::$indicators[$id] ?? [];
66
67 if (!$start_metrics) {
68 return [];
69 }
70
71 // ensure metrics were not ended already
72 if (static::$indicators[$id]['ended'] ?? false) {
73 // indicator was ended already
74 return static::$indicators[$id];
75 }
76
77 $now_ms = microtime(true);
78 $now_memory = memory_get_usage();
79 $memory_used = $now_memory - $start_metrics['memory'];
80 $ms_duration = $now_ms - $start_metrics['ms_start'];
81
82 $end_metrics = [
83 'ended' => true,
84 'ms_end' => $now_ms,
85 'memory_end' => $now_memory,
86 'ms_duration' => $ms_duration,
87 'memory_used' => $memory_used,
88 'memory_end_r' => static::formatMemoryBytes($now_memory),
89 'memory_peak_r' => static::formatMemoryBytes(memory_get_peak_usage()),
90 'memory_used_r' => static::formatMemoryBytes($memory_used),
91 'ms_duration_r' => round($ms_duration, 4),
92 ];
93
94 static::$indicators[$id] = array_merge($start_metrics, $end_metrics);
95
96 return static::$indicators[$id];
97 }
98
99 /**
100 * Registers a memory peak within an indicator.
101 *
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.
105 *
106 * @return void
107 *
108 * @since 1.18.7 (J) - 1.8.7 (WP) added argument $name, $onLast, signature changed.
109 */
110 public static function peak(?string $name = null, ?string $id = null, bool $onLast = false)
111 {
112 if (!$id) {
113 $id = end(static::$indicators)['id'] ?? '';
114 }
115
116 if (!(static::$indicators[$id] ?? [])) {
117 return;
118 }
119
120 $now_memory = memory_get_usage();
121
122 // build peak container
123 $peakContainer = [
124 'name' => $name,
125 'memory' => $now_memory,
126 'memory_r' => static::formatMemoryBytes($now_memory),
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;
144 }
145
146 /**
147 * Ends all indicators and returns them.
148 *
149 * @param bool $end True to end all indicators before returning them.
150 *
151 * @return void
152 */
153 public static function getAll(bool $end = true)
154 {
155 $list = [];
156
157 foreach (static::$indicators as $id => $indicator) {
158 if ($end) {
159 static::end($id);
160 }
161
162 $list[$id] = static::$indicators[$id];
163 }
164
165 return $list;
166 }
167
168 /**
169 * Generates a random UUID v4.
170 *
171 * A UUID is a 16-octet (128-bit) number. In its canonical form, a UUID is represented by 32
172 * hexadecimal digits, displayed in five groups separated by hyphens, in the form 8-4-4-4-12
173 * for a total of 36 characters (32 alphanumeric characters and four hyphens).
174 *
175 * @return string
176 */
177 public static function uuid()
178 {
179 return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
180 // 32 bits for "time_low"
181 mt_rand(0, 0xffff), mt_rand(0, 0xffff),
182
183 // 16 bits for "time_mid"
184 mt_rand(0, 0xffff),
185
186 // 16 bits for "time_hi_and_version",
187 // four most significant bits holds version number 4
188 mt_rand(0, 0x0fff) | 0x4000,
189
190 // 16 bits, 8 bits for "clk_seq_hi_res",
191 // 8 bits for "clk_seq_low",
192 // two most significant bits holds zero and one for variant DCE1.1
193 mt_rand(0, 0x3fff) | 0x8000,
194
195 // 48 bits for "node"
196 mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
197 );
198 }
199
200 /**
201 * Helper method to format float values expressed in bytes (i.e. memory usage).
202 *
203 * @param float $bytes The number of bytes to format.
204 * @param int $precision The precision to use for rounding.
205 *
206 * @return string The formatted bytes string.
207 */
208 public static function formatMemoryBytes($bytes, $precision = 2)
209 {
210 $units = [
211 'B',
212 'KB',
213 'MB',
214 'GB',
215 'TB',
216 ];
217
218 $negative = (bool) ($bytes < 0);
219 $bytes = $negative ? abs($bytes) : $bytes;
220 $bytes = max($bytes, 0);
221 $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
222 $pow = min($pow, count($units) - 1);
223 $bytes /= pow(1024, $pow);
224
225 return ($negative ? '-' : '') . round($bytes, $precision) . ($units[$pow] ?? '?');
226 }
227 }
228