PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.7.2
VikBooking Hotel Booking Engine & PMS v1.7.2
1.8.16 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 All 37 releases
vikbooking / admin / helpers / src / performance / indicator.php

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

209 lines 5.7 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 The indicator identifier.
32 *
33 * @return string The indicator identifier.
34 */
35 public static function start(string $id = '')
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 The indicator identifier.
56 *
57 * @return array List of measurements.
58 */
59 public static function end(string $id = '')
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 $id The indicator identifier.
103 *
104 * @return void
105 */
106 public static function peak(string $id = '')
107 {
108 if (!$id) {
109 $id = end(static::$indicators)['id'] ?? '';
110 }
111
112 $start_metrics = static::$indicators[$id] ?? [];
113
114 if (!$start_metrics) {
115 return;
116 }
117
118 $now_memory = memory_get_usage();
119
120 // push peak
121 static::$indicators[$id]['peaks'] = [
122 'memory' => $now_memory,
123 'memory_r' => static::formatMemoryBytes($now_memory),
124 ];
125 }
126
127 /**
128 * Ends all indicators and returns them.
129 *
130 * @param bool $end True to end all indicators before returning them.
131 *
132 * @return void
133 */
134 public static function getAll($end = true)
135 {
136 $list = [];
137
138 foreach (static::$indicators as $id => $indicator) {
139 if ($end) {
140 static::end($id);
141 }
142
143 $list[$id] = static::$indicators[$id];
144 }
145
146 return $list;
147 }
148
149 /**
150 * Generates a random UUID v4.
151 *
152 * A UUID is a 16-octet (128-bit) number. In its canonical form, a UUID is represented by 32
153 * hexadecimal digits, displayed in five groups separated by hyphens, in the form 8-4-4-4-12
154 * for a total of 36 characters (32 alphanumeric characters and four hyphens).
155 *
156 * @return string
157 */
158 public static function uuid()
159 {
160 return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
161 // 32 bits for "time_low"
162 mt_rand(0, 0xffff), mt_rand(0, 0xffff),
163
164 // 16 bits for "time_mid"
165 mt_rand(0, 0xffff),
166
167 // 16 bits for "time_hi_and_version",
168 // four most significant bits holds version number 4
169 mt_rand(0, 0x0fff) | 0x4000,
170
171 // 16 bits, 8 bits for "clk_seq_hi_res",
172 // 8 bits for "clk_seq_low",
173 // two most significant bits holds zero and one for variant DCE1.1
174 mt_rand(0, 0x3fff) | 0x8000,
175
176 // 48 bits for "node"
177 mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
178 );
179 }
180
181 /**
182 * Helper method to format float values expressed in bytes (i.e. memory usage).
183 *
184 * @param float $bytes The number of bytes to format.
185 * @param int $precision The precision to use for rounding.
186 *
187 * @return string The formatted bytes string.
188 */
189 public static function formatMemoryBytes($bytes, $precision = 2)
190 {
191 $units = [
192 'B',
193 'KB',
194 'MB',
195 'GB',
196 'TB',
197 ];
198
199 $negative = (bool) ($bytes < 0);
200 $bytes = $negative ? abs($bytes) : $bytes;
201 $bytes = max($bytes, 0);
202 $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
203 $pow = min($pow, count($units) - 1);
204 $bytes /= pow(1024, $pow);
205
206 return ($negative ? '-' : '') . round($bytes, $precision) . ($units[$pow] ?? '?');
207 }
208 }
209