PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
Timings.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\timings;
25
35use function get_class;
36use function str_starts_with;
37
38abstract class Timings{
39 public const GROUP_MINECRAFT = "Minecraft";
41 public const GROUP_BREAKDOWN = "Minecraft - Breakdown";
42
43 private static bool $initialized = false;
44
45 public static TimingsHandler $fullTick;
46 public static TimingsHandler $serverTick;
47 public static TimingsHandler $serverInterrupts;
48 public static TimingsHandler $memoryManager;
49 public static TimingsHandler $garbageCollector;
50 public static TimingsHandler $titleTick;
51 public static TimingsHandler $playerNetworkSend;
52 public static TimingsHandler $playerNetworkSendCompress;
53 public static TimingsHandler $playerNetworkSendCompressBroadcast;
54 public static TimingsHandler $playerNetworkSendCompressSessionBuffer;
55 public static TimingsHandler $playerNetworkSendEncrypt;
56 public static TimingsHandler $playerNetworkSendInventorySync;
57 public static TimingsHandler $playerNetworkSendPreSpawnGameData;
58 public static TimingsHandler $playerNetworkReceive;
59 public static TimingsHandler $playerNetworkReceiveDecompress;
60 public static TimingsHandler $playerNetworkReceiveDecrypt;
61 public static TimingsHandler $playerChunkOrder;
62 public static TimingsHandler $playerChunkSend;
63 public static TimingsHandler $connection;
64 public static TimingsHandler $scheduler;
65 public static TimingsHandler $serverCommand;
66 public static TimingsHandler $permissibleCalculation;
67 public static TimingsHandler $permissibleCalculationDiff;
68 public static TimingsHandler $permissibleCalculationCallback;
69 public static TimingsHandler $entityMove;
70 public static TimingsHandler $entityMoveCollision;
71 public static TimingsHandler $projectileMove;
72 public static TimingsHandler $projectileMoveRayTrace;
73 public static TimingsHandler $playerCheckNearEntities;
74 public static TimingsHandler $entityBaseTick;
75 public static TimingsHandler $livingEntityBaseTick;
76 public static TimingsHandler $itemEntityBaseTick;
77
78 public static TimingsHandler $schedulerSync;
79 public static TimingsHandler $schedulerAsync;
80
81 public static TimingsHandler $playerCommand;
82 public static TimingsHandler $craftingDataCacheRebuild;
83
84 public static TimingsHandler $syncPlayerDataLoad;
85 public static TimingsHandler $syncPlayerDataSave;
86
88 public static array $entityTypeTimingMap = [];
90 public static array $tileEntityTypeTimingMap = [];
92 public static array $packetReceiveTimingMap = [];
93
95 private static array $packetDecodeTimingMap = [];
97 private static array $packetHandleTimingMap = [];
98
100 private static array $packetEncodeTimingMap = [];
101
103 public static array $packetSendTimingMap = [];
105 public static array $pluginTaskTimingMap = [];
106
111 private static array $commandTimingMap = [];
112
113 public static TimingsHandler $broadcastPackets;
114
115 public static TimingsHandler $playerMove;
116
118 private static array $events = [];
120 private static array $eventHandlers = [];
121
122 private static TimingsHandler $asyncTaskProgressUpdateParent;
123 private static TimingsHandler $asyncTaskCompletionParent;
124 private static TimingsHandler $asyncTaskErrorParent;
125
127 private static array $asyncTaskProgressUpdate = [];
128
130 private static array $asyncTaskCompletion = [];
132 private static array $asyncTaskError = [];
133
134 private static TimingsHandler $asyncTaskWorkers;
136 private static array $asyncTaskRun = [];
137
138 public static function init() : void{
139 if(self::$initialized){
140 return;
141 }
142 self::$initialized = true;
143
144 self::$fullTick = new TimingsHandler("Full Server Tick");
145 self::$serverTick = new TimingsHandler("Server Tick Update Cycle", self::$fullTick);
146 self::$serverInterrupts = new TimingsHandler("Server Mid-Tick Processing", self::$fullTick);
147 self::$memoryManager = new TimingsHandler("Memory Manager");
148 self::$garbageCollector = new TimingsHandler("Garbage Collector", self::$memoryManager);
149 self::$titleTick = new TimingsHandler("Console Title Tick");
150
151 self::$connection = new TimingsHandler("Connection Handler");
152
153 self::$playerNetworkSend = new TimingsHandler("Player Network Send", self::$connection);
154 self::$playerNetworkSendCompress = new TimingsHandler("Player Network Send - Compression", self::$playerNetworkSend);
155 self::$playerNetworkSendCompressBroadcast = new TimingsHandler("Player Network Send - Compression (Broadcast)", self::$playerNetworkSendCompress);
156 self::$playerNetworkSendCompressSessionBuffer = new TimingsHandler("Player Network Send - Compression (Session Buffer)", self::$playerNetworkSendCompress);
157 self::$playerNetworkSendEncrypt = new TimingsHandler("Player Network Send - Encryption", self::$playerNetworkSend);
158 self::$playerNetworkSendInventorySync = new TimingsHandler("Player Network Send - Inventory Sync", self::$playerNetworkSend);
159 self::$playerNetworkSendPreSpawnGameData = new TimingsHandler("Player Network Send - Pre-Spawn Game Data", self::$playerNetworkSend);
160
161 self::$playerNetworkReceive = new TimingsHandler("Player Network Receive", self::$connection);
162 self::$playerNetworkReceiveDecompress = new TimingsHandler("Player Network Receive - Decompression", self::$playerNetworkReceive);
163 self::$playerNetworkReceiveDecrypt = new TimingsHandler("Player Network Receive - Decryption", self::$playerNetworkReceive);
164
165 self::$broadcastPackets = new TimingsHandler("Broadcast Packets", self::$playerNetworkSend);
166
167 self::$playerMove = new TimingsHandler("Player Movement");
168 self::$playerChunkOrder = new TimingsHandler("Player Order Chunks");
169 self::$playerChunkSend = new TimingsHandler("Player Network Send - Chunks", self::$playerNetworkSend);
170 self::$scheduler = new TimingsHandler("Scheduler");
171 self::$serverCommand = new TimingsHandler("Server Command");
172 self::$permissibleCalculation = new TimingsHandler("Permissible Calculation");
173 self::$permissibleCalculationDiff = new TimingsHandler("Permissible Calculation - Diff", self::$permissibleCalculation);
174 self::$permissibleCalculationCallback = new TimingsHandler("Permissible Calculation - Callbacks", self::$permissibleCalculation);
175
176 self::$syncPlayerDataLoad = new TimingsHandler("Player Data Load");
177 self::$syncPlayerDataSave = new TimingsHandler("Player Data Save");
178
179 self::$entityMove = new TimingsHandler("Entity Movement");
180 self::$entityMoveCollision = new TimingsHandler("Entity Movement - Collision Checks", self::$entityMove);
181
182 self::$projectileMove = new TimingsHandler("Projectile Movement", self::$entityMove);
183 self::$projectileMoveRayTrace = new TimingsHandler("Projectile Movement - Ray Tracing", self::$projectileMove);
184
185 self::$playerCheckNearEntities = new TimingsHandler("checkNearEntities");
186 self::$entityBaseTick = new TimingsHandler("Entity Base Tick");
187 self::$livingEntityBaseTick = new TimingsHandler("Entity Base Tick - Living");
188 self::$itemEntityBaseTick = new TimingsHandler("Entity Base Tick - ItemEntity");
189
190 self::$schedulerSync = new TimingsHandler("Scheduler - Sync Tasks");
191
192 self::$schedulerAsync = new TimingsHandler("Scheduler - Async Tasks");
193 self::$asyncTaskProgressUpdateParent = new TimingsHandler("Async Tasks - Progress Updates", self::$schedulerAsync);
194 self::$asyncTaskCompletionParent = new TimingsHandler("Async Tasks - Completion Handlers", self::$schedulerAsync);
195 self::$asyncTaskErrorParent = new TimingsHandler("Async Tasks - Error Handlers", self::$schedulerAsync);
196
197 self::$asyncTaskWorkers = new TimingsHandler("Async Task Workers");
198
199 self::$playerCommand = new TimingsHandler("Player Command");
200 self::$craftingDataCacheRebuild = new TimingsHandler("Build CraftingDataPacket Cache");
201
202 }
203
208 public static function getScheduledTaskTimings(TaskHandler $task, int $period) : TimingsHandler{
209 self::init();
210 $name = "Task: " . $task->getTaskName();
211
212 if($period > 0){
213 $name .= "(interval:" . $period . ")";
214 }else{
215 $name .= "(Single)";
216 }
217
218 if(!isset(self::$pluginTaskTimingMap[$name])){
219 self::$pluginTaskTimingMap[$name] = new TimingsHandler($name, self::$schedulerSync, $task->getOwnerName());
220 }
221
222 return self::$pluginTaskTimingMap[$name];
223 }
224
228 private static function shortenCoreClassName(string $class, string $prefix) : string{
229 if(str_starts_with($class, $prefix)){
230 return (new \ReflectionClass($class))->getShortName();
231 }
232 return $class;
233 }
234
235 public static function getEntityTimings(Entity $entity) : TimingsHandler{
236 self::init();
237 if(!isset(self::$entityTypeTimingMap[$entity::class])){
238 if($entity instanceof Player){
239 //the timings viewer calculates average player count by looking at this timer, so we need to ensure it has
240 //a name it can identify. However, we also want to make it obvious if this is a custom Player class.
241 $displayName = $entity::class !== Player::class ? "Player (" . $entity::class . ")" : "Player";
242 }else{
243 $displayName = self::shortenCoreClassName($entity::class, "pocketmine\\entity\\");
244 }
245 self::$entityTypeTimingMap[$entity::class] = new TimingsHandler("Entity Tick - " . $displayName);
246 }
247
248 return self::$entityTypeTimingMap[$entity::class];
249 }
250
251 public static function getTileEntityTimings(Tile $tile) : TimingsHandler{
252 self::init();
253 if(!isset(self::$tileEntityTypeTimingMap[$tile::class])){
254 self::$tileEntityTypeTimingMap[$tile::class] = new TimingsHandler(
255 "Block Entity Tick - " . self::shortenCoreClassName($tile::class, "pocketmine\\block\\tile\\")
256 );
257 }
258
259 return self::$tileEntityTypeTimingMap[$tile::class];
260 }
261
262 public static function getReceiveDataPacketTimings(ServerboundPacket $pk) : TimingsHandler{
263 self::init();
264 if(!isset(self::$packetReceiveTimingMap[$pk::class])){
265 self::$packetReceiveTimingMap[$pk::class] = new TimingsHandler("Receive - " . $pk->getName(), self::$playerNetworkReceive);
266 }
267
268 return self::$packetReceiveTimingMap[$pk::class];
269 }
270
271 public static function getDecodeDataPacketTimings(ServerboundPacket $pk) : TimingsHandler{
272 return self::$packetDecodeTimingMap[$pk::class] ??= new TimingsHandler(
273 "Decode - " . $pk->getName(),
274 self::getReceiveDataPacketTimings($pk)
275 );
276 }
277
278 public static function getHandleDataPacketTimings(ServerboundPacket $pk) : TimingsHandler{
279 return self::$packetHandleTimingMap[$pk::class] ??= new TimingsHandler(
280 "Handler - " . $pk->getName(),
281 self::getReceiveDataPacketTimings($pk)
282 );
283 }
284
285 public static function getEncodeDataPacketTimings(ClientboundPacket $pk) : TimingsHandler{
286 return self::$packetEncodeTimingMap[$pk::class] ??= new TimingsHandler(
287 "Encode - " . $pk->getName(),
288 self::getSendDataPacketTimings($pk)
289 );
290 }
291
292 public static function getSendDataPacketTimings(ClientboundPacket $pk) : TimingsHandler{
293 self::init();
294 if(!isset(self::$packetSendTimingMap[$pk::class])){
295 self::$packetSendTimingMap[$pk::class] = new TimingsHandler("Send - " . $pk->getName(), self::$playerNetworkSend);
296 }
297
298 return self::$packetSendTimingMap[$pk::class];
299 }
300
301 public static function getCommandDispatchTimings(string $commandName) : TimingsHandler{
302 self::init();
303
304 return self::$commandTimingMap[$commandName] ??= new TimingsHandler("Command - " . $commandName);
305 }
306
307 public static function getEventTimings(Event $event) : TimingsHandler{
308 $eventClass = get_class($event);
309 if(!isset(self::$events[$eventClass])){
310 self::$events[$eventClass] = new TimingsHandler(self::shortenCoreClassName($eventClass, "pocketmine\\event\\"), group: "Events");
311 }
312
313 return self::$events[$eventClass];
314 }
315
319 public static function getEventHandlerTimings(string $event, string $handlerName, string $group) : TimingsHandler{
320 if(!isset(self::$eventHandlers[$event][$handlerName])){
321 self::$eventHandlers[$event][$handlerName] = new TimingsHandler($handlerName . "(" . self::shortenCoreClassName($event, "pocketmine\\event\\") . ")", group: $group);
322 }
323
324 return self::$eventHandlers[$event][$handlerName];
325 }
326
327 public static function getAsyncTaskProgressUpdateTimings(AsyncTask $task, string $group = self::GROUP_MINECRAFT) : TimingsHandler{
328 $taskClass = $task::class;
329 if(!isset(self::$asyncTaskProgressUpdate[$taskClass])){
330 self::init();
331 self::$asyncTaskProgressUpdate[$taskClass] = new TimingsHandler(
332 "AsyncTask - " . self::shortenCoreClassName($taskClass, "pocketmine\\") . " - Progress Updates",
333 self::$asyncTaskProgressUpdateParent,
334 $group
335 );
336 }
337
338 return self::$asyncTaskProgressUpdate[$taskClass];
339 }
340
341 public static function getAsyncTaskCompletionTimings(AsyncTask $task, string $group = self::GROUP_MINECRAFT) : TimingsHandler{
342 $taskClass = $task::class;
343 if(!isset(self::$asyncTaskCompletion[$taskClass])){
344 self::init();
345 self::$asyncTaskCompletion[$taskClass] = new TimingsHandler(
346 "AsyncTask - " . self::shortenCoreClassName($taskClass, "pocketmine\\") . " - Completion Handler",
347 self::$asyncTaskCompletionParent,
348 $group
349 );
350 }
351
352 return self::$asyncTaskCompletion[$taskClass];
353 }
354
358 public static function getAsyncTaskErrorTimings(AsyncTask $task, string $group = self::GROUP_MINECRAFT) : TimingsHandler{
359 $taskClass = $task::class;
360 if(!isset(self::$asyncTaskError[$taskClass])){
361 self::init();
362 self::$asyncTaskError[$taskClass] = new TimingsHandler(
363 "AsyncTask - " . self::shortenCoreClassName($taskClass, "pocketmine\\") . " - Error Handler",
364 self::$asyncTaskErrorParent,
365 $group
366 );
367 }
368
369 return self::$asyncTaskError[$taskClass];
370 }
371
372 public static function getAsyncTaskRunTimings(AsyncTask $task, string $group = self::GROUP_MINECRAFT) : TimingsHandler{
373 $taskClass = $task::class;
374 if(!isset(self::$asyncTaskRun[$taskClass])){
375 self::init();
376 self::$asyncTaskRun[$taskClass] = new TimingsHandler(
377 "AsyncTask - " . self::shortenCoreClassName($taskClass, "pocketmine\\") . " - Run",
378 self::$asyncTaskWorkers,
379 $group
380 );
381 }
382
383 return self::$asyncTaskRun[$taskClass];
384 }
385}
static getAsyncTaskErrorTimings(AsyncTask $task, string $group=self::GROUP_MINECRAFT)
Definition Timings.php:358
static getEventHandlerTimings(string $event, string $handlerName, string $group)
Definition Timings.php:319
static getScheduledTaskTimings(TaskHandler $task, int $period)
Definition Timings.php:208