PocketMine-MP 5.15.1 git-fb9a74e8799c71ed8292cfa53abe7a4c9204629d
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
34use function get_class;
35use function str_starts_with;
36
37abstract class Timings{
38 public const GROUP_MINECRAFT = "Minecraft";
39 public const GROUP_BREAKDOWN = "Minecraft - Breakdown";
40
41 private static bool $initialized = false;
42
43 public static TimingsHandler $fullTick;
44 public static TimingsHandler $serverTick;
45 public static TimingsHandler $serverInterrupts;
46 public static TimingsHandler $memoryManager;
47 public static TimingsHandler $garbageCollector;
48 public static TimingsHandler $titleTick;
49 public static TimingsHandler $playerNetworkSend;
50 public static TimingsHandler $playerNetworkSendCompress;
51 public static TimingsHandler $playerNetworkSendCompressBroadcast;
52 public static TimingsHandler $playerNetworkSendCompressSessionBuffer;
53 public static TimingsHandler $playerNetworkSendEncrypt;
54 public static TimingsHandler $playerNetworkSendInventorySync;
55 public static TimingsHandler $playerNetworkSendPreSpawnGameData;
56 public static TimingsHandler $playerNetworkReceive;
57 public static TimingsHandler $playerNetworkReceiveDecompress;
58 public static TimingsHandler $playerNetworkReceiveDecrypt;
59 public static TimingsHandler $playerChunkOrder;
60 public static TimingsHandler $playerChunkSend;
61 public static TimingsHandler $connection;
62 public static TimingsHandler $scheduler;
63 public static TimingsHandler $serverCommand;
64 public static TimingsHandler $permissibleCalculation;
65 public static TimingsHandler $permissibleCalculationDiff;
66 public static TimingsHandler $permissibleCalculationCallback;
67 public static TimingsHandler $entityMove;
68 public static TimingsHandler $entityMoveCollision;
69 public static TimingsHandler $projectileMove;
70 public static TimingsHandler $projectileMoveRayTrace;
71 public static TimingsHandler $playerCheckNearEntities;
72 public static TimingsHandler $entityBaseTick;
73 public static TimingsHandler $livingEntityBaseTick;
74 public static TimingsHandler $itemEntityBaseTick;
75
76 public static TimingsHandler $schedulerSync;
77 public static TimingsHandler $schedulerAsync;
78
79 public static TimingsHandler $playerCommand;
80 public static TimingsHandler $craftingDataCacheRebuild;
81
82 public static TimingsHandler $syncPlayerDataLoad;
83 public static TimingsHandler $syncPlayerDataSave;
84
86 public static array $entityTypeTimingMap = [];
88 public static array $tileEntityTypeTimingMap = [];
90 public static array $packetReceiveTimingMap = [];
91
93 private static array $packetDecodeTimingMap = [];
95 private static array $packetHandleTimingMap = [];
96
98 private static array $packetEncodeTimingMap = [];
99
101 public static array $packetSendTimingMap = [];
103 public static array $pluginTaskTimingMap = [];
104
109 private static array $commandTimingMap = [];
110
111 public static TimingsHandler $broadcastPackets;
112
113 public static TimingsHandler $playerMove;
114
116 private static array $events = [];
118 private static array $eventHandlers = [];
119
120 private static TimingsHandler $asyncTaskProgressUpdateParent;
121 private static TimingsHandler $asyncTaskCompletionParent;
122 private static TimingsHandler $asyncTaskErrorParent;
123
125 private static array $asyncTaskProgressUpdate = [];
127 private static array $asyncTaskCompletion = [];
129 private static array $asyncTaskError = [];
130
131 public static function init() : void{
132 if(self::$initialized){
133 return;
134 }
135 self::$initialized = true;
136
137 self::$fullTick = new TimingsHandler("Full Server Tick");
138 self::$serverTick = new TimingsHandler("Server Tick Update Cycle", self::$fullTick);
139 self::$serverInterrupts = new TimingsHandler("Server Mid-Tick Processing", self::$fullTick);
140 self::$memoryManager = new TimingsHandler("Memory Manager");
141 self::$garbageCollector = new TimingsHandler("Garbage Collector", self::$memoryManager);
142 self::$titleTick = new TimingsHandler("Console Title Tick");
143
144 self::$connection = new TimingsHandler("Connection Handler");
145
146 self::$playerNetworkSend = new TimingsHandler("Player Network Send", self::$connection);
147 self::$playerNetworkSendCompress = new TimingsHandler("Player Network Send - Compression", self::$playerNetworkSend);
148 self::$playerNetworkSendCompressBroadcast = new TimingsHandler("Player Network Send - Compression (Broadcast)", self::$playerNetworkSendCompress);
149 self::$playerNetworkSendCompressSessionBuffer = new TimingsHandler("Player Network Send - Compression (Session Buffer)", self::$playerNetworkSendCompress);
150 self::$playerNetworkSendEncrypt = new TimingsHandler("Player Network Send - Encryption", self::$playerNetworkSend);
151 self::$playerNetworkSendInventorySync = new TimingsHandler("Player Network Send - Inventory Sync", self::$playerNetworkSend);
152 self::$playerNetworkSendPreSpawnGameData = new TimingsHandler("Player Network Send - Pre-Spawn Game Data", self::$playerNetworkSend);
153
154 self::$playerNetworkReceive = new TimingsHandler("Player Network Receive", self::$connection);
155 self::$playerNetworkReceiveDecompress = new TimingsHandler("Player Network Receive - Decompression", self::$playerNetworkReceive);
156 self::$playerNetworkReceiveDecrypt = new TimingsHandler("Player Network Receive - Decryption", self::$playerNetworkReceive);
157
158 self::$broadcastPackets = new TimingsHandler("Broadcast Packets", self::$playerNetworkSend);
159
160 self::$playerMove = new TimingsHandler("Player Movement");
161 self::$playerChunkOrder = new TimingsHandler("Player Order Chunks");
162 self::$playerChunkSend = new TimingsHandler("Player Network Send - Chunks", self::$playerNetworkSend);
163 self::$scheduler = new TimingsHandler("Scheduler");
164 self::$serverCommand = new TimingsHandler("Server Command");
165 self::$permissibleCalculation = new TimingsHandler("Permissible Calculation");
166 self::$permissibleCalculationDiff = new TimingsHandler("Permissible Calculation - Diff", self::$permissibleCalculation);
167 self::$permissibleCalculationCallback = new TimingsHandler("Permissible Calculation - Callbacks", self::$permissibleCalculation);
168
169 self::$syncPlayerDataLoad = new TimingsHandler("Player Data Load");
170 self::$syncPlayerDataSave = new TimingsHandler("Player Data Save");
171
172 self::$entityMove = new TimingsHandler("Entity Movement");
173 self::$entityMoveCollision = new TimingsHandler("Entity Movement - Collision Checks", self::$entityMove);
174
175 self::$projectileMove = new TimingsHandler("Projectile Movement", self::$entityMove);
176 self::$projectileMoveRayTrace = new TimingsHandler("Projectile Movement - Ray Tracing", self::$projectileMove);
177
178 self::$playerCheckNearEntities = new TimingsHandler("checkNearEntities");
179 self::$entityBaseTick = new TimingsHandler("Entity Base Tick");
180 self::$livingEntityBaseTick = new TimingsHandler("Entity Base Tick - Living");
181 self::$itemEntityBaseTick = new TimingsHandler("Entity Base Tick - ItemEntity");
182
183 self::$schedulerSync = new TimingsHandler("Scheduler - Sync Tasks");
184
185 self::$schedulerAsync = new TimingsHandler("Scheduler - Async Tasks");
186 self::$asyncTaskProgressUpdateParent = new TimingsHandler("Async Tasks - Progress Updates", self::$schedulerAsync);
187 self::$asyncTaskCompletionParent = new TimingsHandler("Async Tasks - Completion Handlers", self::$schedulerAsync);
188 self::$asyncTaskErrorParent = new TimingsHandler("Async Tasks - Error Handlers", self::$schedulerAsync);
189
190 self::$playerCommand = new TimingsHandler("Player Command");
191 self::$craftingDataCacheRebuild = new TimingsHandler("Build CraftingDataPacket Cache");
192
193 }
194
195 public static function getScheduledTaskTimings(TaskHandler $task, int $period) : TimingsHandler{
196 self::init();
197 $name = "Task: " . $task->getTaskName();
198
199 if($period > 0){
200 $name .= "(interval:" . $period . ")";
201 }else{
202 $name .= "(Single)";
203 }
204
205 if(!isset(self::$pluginTaskTimingMap[$name])){
206 self::$pluginTaskTimingMap[$name] = new TimingsHandler($name, self::$schedulerSync, $task->getOwnerName());
207 }
208
209 return self::$pluginTaskTimingMap[$name];
210 }
211
215 private static function shortenCoreClassName(string $class, string $prefix) : string{
216 if(str_starts_with($class, $prefix)){
217 return (new \ReflectionClass($class))->getShortName();
218 }
219 return $class;
220 }
221
222 public static function getEntityTimings(Entity $entity) : TimingsHandler{
223 self::init();
224 if(!isset(self::$entityTypeTimingMap[$entity::class])){
225 if($entity instanceof Player){
226 //the timings viewer calculates average player count by looking at this timer, so we need to ensure it has
227 //a name it can identify. However, we also want to make it obvious if this is a custom Player class.
228 $displayName = $entity::class !== Player::class ? "Player (" . $entity::class . ")" : "Player";
229 }else{
230 $displayName = self::shortenCoreClassName($entity::class, "pocketmine\\entity\\");
231 }
232 self::$entityTypeTimingMap[$entity::class] = new TimingsHandler("Entity Tick - " . $displayName);
233 }
234
235 return self::$entityTypeTimingMap[$entity::class];
236 }
237
238 public static function getTileEntityTimings(Tile $tile) : TimingsHandler{
239 self::init();
240 if(!isset(self::$tileEntityTypeTimingMap[$tile::class])){
241 self::$tileEntityTypeTimingMap[$tile::class] = new TimingsHandler(
242 "Block Entity Tick - " . self::shortenCoreClassName($tile::class, "pocketmine\\block\\tile\\")
243 );
244 }
245
246 return self::$tileEntityTypeTimingMap[$tile::class];
247 }
248
249 public static function getReceiveDataPacketTimings(ServerboundPacket $pk) : TimingsHandler{
250 self::init();
251 if(!isset(self::$packetReceiveTimingMap[$pk::class])){
252 self::$packetReceiveTimingMap[$pk::class] = new TimingsHandler("Receive - " . $pk->getName(), self::$playerNetworkReceive);
253 }
254
255 return self::$packetReceiveTimingMap[$pk::class];
256 }
257
258 public static function getDecodeDataPacketTimings(ServerboundPacket $pk) : TimingsHandler{
259 return self::$packetDecodeTimingMap[$pk::class] ??= new TimingsHandler(
260 "Decode - " . $pk->getName(),
261 self::getReceiveDataPacketTimings($pk)
262 );
263 }
264
265 public static function getHandleDataPacketTimings(ServerboundPacket $pk) : TimingsHandler{
266 return self::$packetHandleTimingMap[$pk::class] ??= new TimingsHandler(
267 "Handler - " . $pk->getName(),
268 self::getReceiveDataPacketTimings($pk)
269 );
270 }
271
272 public static function getEncodeDataPacketTimings(ClientboundPacket $pk) : TimingsHandler{
273 return self::$packetEncodeTimingMap[$pk::class] ??= new TimingsHandler(
274 "Encode - " . $pk->getName(),
275 self::getSendDataPacketTimings($pk)
276 );
277 }
278
279 public static function getSendDataPacketTimings(ClientboundPacket $pk) : TimingsHandler{
280 self::init();
281 if(!isset(self::$packetSendTimingMap[$pk::class])){
282 self::$packetSendTimingMap[$pk::class] = new TimingsHandler("Send - " . $pk->getName(), self::$playerNetworkSend);
283 }
284
285 return self::$packetSendTimingMap[$pk::class];
286 }
287
288 public static function getCommandDispatchTimings(string $commandName) : TimingsHandler{
289 self::init();
290
291 return self::$commandTimingMap[$commandName] ??= new TimingsHandler("Command - " . $commandName);
292 }
293
294 public static function getEventTimings(Event $event) : TimingsHandler{
295 $eventClass = get_class($event);
296 if(!isset(self::$events[$eventClass])){
297 self::$events[$eventClass] = new TimingsHandler(self::shortenCoreClassName($eventClass, "pocketmine\\event\\"), group: "Events");
298 }
299
300 return self::$events[$eventClass];
301 }
302
306 public static function getEventHandlerTimings(string $event, string $handlerName, string $group) : TimingsHandler{
307 if(!isset(self::$eventHandlers[$event][$handlerName])){
308 self::$eventHandlers[$event][$handlerName] = new TimingsHandler($handlerName . "(" . self::shortenCoreClassName($event, "pocketmine\\event\\") . ")", group: $group);
309 }
310
311 return self::$eventHandlers[$event][$handlerName];
312 }
313
314 public static function getAsyncTaskProgressUpdateTimings(AsyncTask $task, string $group = self::GROUP_MINECRAFT) : TimingsHandler{
315 $taskClass = $task::class;
316 if(!isset(self::$asyncTaskProgressUpdate[$taskClass])){
317 self::init();
318 self::$asyncTaskProgressUpdate[$taskClass] = new TimingsHandler(
319 "AsyncTask - " . self::shortenCoreClassName($taskClass, "pocketmine\\") . " - Progress Updates",
320 self::$asyncTaskProgressUpdateParent,
321 $group
322 );
323 }
324
325 return self::$asyncTaskProgressUpdate[$taskClass];
326 }
327
328 public static function getAsyncTaskCompletionTimings(AsyncTask $task, string $group = self::GROUP_MINECRAFT) : TimingsHandler{
329 $taskClass = $task::class;
330 if(!isset(self::$asyncTaskCompletion[$taskClass])){
331 self::init();
332 self::$asyncTaskCompletion[$taskClass] = new TimingsHandler(
333 "AsyncTask - " . self::shortenCoreClassName($taskClass, "pocketmine\\") . " - Completion Handler",
334 self::$asyncTaskCompletionParent,
335 $group
336 );
337 }
338
339 return self::$asyncTaskCompletion[$taskClass];
340 }
341
342 public static function getAsyncTaskErrorTimings(AsyncTask $task, string $group = self::GROUP_MINECRAFT) : TimingsHandler{
343 $taskClass = $task::class;
344 if(!isset(self::$asyncTaskError[$taskClass])){
345 self::init();
346 self::$asyncTaskError[$taskClass] = new TimingsHandler(
347 "AsyncTask - " . self::shortenCoreClassName($taskClass, "pocketmine\\") . " - Error Handler",
348 self::$asyncTaskErrorParent,
349 $group
350 );
351 }
352
353 return self::$asyncTaskError[$taskClass];
354 }
355}
static getEventHandlerTimings(string $event, string $handlerName, string $group)
Definition: Timings.php:306