PocketMine-MP 5.23.3 git-976fc63567edab7a6fb6aeae739f43cf9fe57de4
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";
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 $asyncTaskCompletionParent;
122 private static array $asyncTaskCompletion = [];
123
124 public static TimingsHandler $asyncTaskWorkers;
126 private static array $asyncTaskRun = [];
127
128 public static function init() : void{
129 if(self::$initialized){
130 return;
131 }
132 self::$initialized = true;
133
134 self::$fullTick = new TimingsHandler("Full Server Tick");
135 self::$serverTick = new TimingsHandler("Server Tick Update Cycle", self::$fullTick);
136 self::$serverInterrupts = new TimingsHandler("Server Mid-Tick Processing", self::$fullTick);
137 self::$memoryManager = new TimingsHandler("Memory Manager");
138 self::$garbageCollector = new TimingsHandler("Garbage Collector", self::$memoryManager);
139 self::$titleTick = new TimingsHandler("Console Title Tick");
140
141 self::$connection = new TimingsHandler("Connection Handler");
142
143 self::$playerNetworkSend = new TimingsHandler("Player Network Send", self::$connection);
144 self::$playerNetworkSendCompress = new TimingsHandler("Player Network Send - Compression", self::$playerNetworkSend);
145 self::$playerNetworkSendCompressBroadcast = new TimingsHandler("Player Network Send - Compression (Broadcast)", self::$playerNetworkSendCompress);
146 self::$playerNetworkSendCompressSessionBuffer = new TimingsHandler("Player Network Send - Compression (Session Buffer)", self::$playerNetworkSendCompress);
147 self::$playerNetworkSendEncrypt = new TimingsHandler("Player Network Send - Encryption", self::$playerNetworkSend);
148 self::$playerNetworkSendInventorySync = new TimingsHandler("Player Network Send - Inventory Sync", self::$playerNetworkSend);
149 self::$playerNetworkSendPreSpawnGameData = new TimingsHandler("Player Network Send - Pre-Spawn Game Data", self::$playerNetworkSend);
150
151 self::$playerNetworkReceive = new TimingsHandler("Player Network Receive", self::$connection);
152 self::$playerNetworkReceiveDecompress = new TimingsHandler("Player Network Receive - Decompression", self::$playerNetworkReceive);
153 self::$playerNetworkReceiveDecrypt = new TimingsHandler("Player Network Receive - Decryption", self::$playerNetworkReceive);
154
155 self::$broadcastPackets = new TimingsHandler("Broadcast Packets", self::$playerNetworkSend);
156
157 self::$playerMove = new TimingsHandler("Player Movement");
158 self::$playerChunkOrder = new TimingsHandler("Player Order Chunks");
159 self::$playerChunkSend = new TimingsHandler("Player Network Send - Chunks", self::$playerNetworkSend);
160 self::$scheduler = new TimingsHandler("Scheduler");
161 self::$serverCommand = new TimingsHandler("Server Command");
162 self::$permissibleCalculation = new TimingsHandler("Permissible Calculation");
163 self::$permissibleCalculationDiff = new TimingsHandler("Permissible Calculation - Diff", self::$permissibleCalculation);
164 self::$permissibleCalculationCallback = new TimingsHandler("Permissible Calculation - Callbacks", self::$permissibleCalculation);
165
166 self::$syncPlayerDataLoad = new TimingsHandler("Player Data Load");
167 self::$syncPlayerDataSave = new TimingsHandler("Player Data Save");
168
169 self::$entityMove = new TimingsHandler("Entity Movement");
170 self::$entityMoveCollision = new TimingsHandler("Entity Movement - Collision Checks", self::$entityMove);
171
172 self::$projectileMove = new TimingsHandler("Projectile Movement", self::$entityMove);
173 self::$projectileMoveRayTrace = new TimingsHandler("Projectile Movement - Ray Tracing", self::$projectileMove);
174
175 self::$playerCheckNearEntities = new TimingsHandler("checkNearEntities");
176 self::$entityBaseTick = new TimingsHandler("Entity Base Tick");
177 self::$livingEntityBaseTick = new TimingsHandler("Entity Base Tick - Living");
178 self::$itemEntityBaseTick = new TimingsHandler("Entity Base Tick - ItemEntity");
179
180 self::$schedulerSync = new TimingsHandler("Scheduler - Sync Tasks");
181
182 self::$schedulerAsync = new TimingsHandler("Scheduler - Async Tasks");
183 self::$asyncTaskCompletionParent = new TimingsHandler("Async Tasks - Completion Handlers", self::$schedulerAsync);
184
185 self::$asyncTaskWorkers = new TimingsHandler("Async Task Workers");
186
187 self::$playerCommand = new TimingsHandler("Player Command");
188 self::$craftingDataCacheRebuild = new TimingsHandler("Build CraftingDataPacket Cache");
189
190 }
191
196 public static function getScheduledTaskTimings(TaskHandler $task, int $period) : TimingsHandler{
197 self::init();
198 $name = "Task: " . $task->getTaskName();
199
200 if($period > 0){
201 $name .= "(interval:" . $period . ")";
202 }else{
203 $name .= "(Single)";
204 }
205
206 if(!isset(self::$pluginTaskTimingMap[$name])){
207 self::$pluginTaskTimingMap[$name] = new TimingsHandler($name, self::$schedulerSync, $task->getOwnerName());
208 }
209
210 return self::$pluginTaskTimingMap[$name];
211 }
212
216 private static function shortenCoreClassName(string $class, string $prefix) : string{
217 if(str_starts_with($class, $prefix)){
218 return (new \ReflectionClass($class))->getShortName();
219 }
220 return $class;
221 }
222
223 public static function getEntityTimings(Entity $entity) : TimingsHandler{
224 self::init();
225 if(!isset(self::$entityTypeTimingMap[$entity::class])){
226 if($entity instanceof Player){
227 //the timings viewer calculates average player count by looking at this timer, so we need to ensure it has
228 //a name it can identify. However, we also want to make it obvious if this is a custom Player class.
229 $displayName = $entity::class !== Player::class ? "Player (" . $entity::class . ")" : "Player";
230 }else{
231 $displayName = self::shortenCoreClassName($entity::class, "pocketmine\\entity\\");
232 }
233 self::$entityTypeTimingMap[$entity::class] = new TimingsHandler("Entity Tick - " . $displayName);
234 }
235
236 return self::$entityTypeTimingMap[$entity::class];
237 }
238
239 public static function getTileEntityTimings(Tile $tile) : TimingsHandler{
240 self::init();
241 if(!isset(self::$tileEntityTypeTimingMap[$tile::class])){
242 self::$tileEntityTypeTimingMap[$tile::class] = new TimingsHandler(
243 "Block Entity Tick - " . self::shortenCoreClassName($tile::class, "pocketmine\\block\\tile\\")
244 );
245 }
246
247 return self::$tileEntityTypeTimingMap[$tile::class];
248 }
249
250 public static function getReceiveDataPacketTimings(ServerboundPacket $pk) : TimingsHandler{
251 self::init();
252 if(!isset(self::$packetReceiveTimingMap[$pk::class])){
253 self::$packetReceiveTimingMap[$pk::class] = new TimingsHandler("Receive - " . $pk->getName(), self::$playerNetworkReceive);
254 }
255
256 return self::$packetReceiveTimingMap[$pk::class];
257 }
258
259 public static function getDecodeDataPacketTimings(ServerboundPacket $pk) : TimingsHandler{
260 return self::$packetDecodeTimingMap[$pk::class] ??= new TimingsHandler(
261 "Decode - " . $pk->getName(),
262 self::getReceiveDataPacketTimings($pk)
263 );
264 }
265
266 public static function getHandleDataPacketTimings(ServerboundPacket $pk) : TimingsHandler{
267 return self::$packetHandleTimingMap[$pk::class] ??= new TimingsHandler(
268 "Handler - " . $pk->getName(),
269 self::getReceiveDataPacketTimings($pk)
270 );
271 }
272
273 public static function getEncodeDataPacketTimings(ClientboundPacket $pk) : TimingsHandler{
274 return self::$packetEncodeTimingMap[$pk::class] ??= new TimingsHandler(
275 "Encode - " . $pk->getName(),
276 self::getSendDataPacketTimings($pk)
277 );
278 }
279
280 public static function getSendDataPacketTimings(ClientboundPacket $pk) : TimingsHandler{
281 self::init();
282 if(!isset(self::$packetSendTimingMap[$pk::class])){
283 self::$packetSendTimingMap[$pk::class] = new TimingsHandler("Send - " . $pk->getName(), self::$playerNetworkSend);
284 }
285
286 return self::$packetSendTimingMap[$pk::class];
287 }
288
289 public static function getCommandDispatchTimings(string $commandName) : TimingsHandler{
290 self::init();
291
292 return self::$commandTimingMap[$commandName] ??= new TimingsHandler("Command - " . $commandName);
293 }
294
295 public static function getEventTimings(Event $event) : TimingsHandler{
296 $eventClass = get_class($event);
297 if(!isset(self::$events[$eventClass])){
298 self::$events[$eventClass] = new TimingsHandler(self::shortenCoreClassName($eventClass, "pocketmine\\event\\"), group: "Events");
299 }
300
301 return self::$events[$eventClass];
302 }
303
307 public static function getEventHandlerTimings(string $event, string $handlerName, string $group) : TimingsHandler{
308 if(!isset(self::$eventHandlers[$event][$handlerName])){
309 self::$eventHandlers[$event][$handlerName] = new TimingsHandler($handlerName . "(" . self::shortenCoreClassName($event, "pocketmine\\event\\") . ")", group: $group);
310 }
311
312 return self::$eventHandlers[$event][$handlerName];
313 }
314
315 public static function getAsyncTaskCompletionTimings(AsyncTask $task, string $group = self::GROUP_MINECRAFT) : TimingsHandler{
316 $taskClass = $task::class;
317 if(!isset(self::$asyncTaskCompletion[$taskClass])){
318 self::init();
319 self::$asyncTaskCompletion[$taskClass] = new TimingsHandler(
320 "AsyncTask - " . self::shortenCoreClassName($taskClass, "pocketmine\\") . " - Completion Handler",
321 self::$asyncTaskCompletionParent,
322 $group
323 );
324 }
325
326 return self::$asyncTaskCompletion[$taskClass];
327 }
328
329 public static function getAsyncTaskRunTimings(AsyncTask $task, string $group = self::GROUP_MINECRAFT) : TimingsHandler{
330 $taskClass = $task::class;
331 if(!isset(self::$asyncTaskRun[$taskClass])){
332 self::init();
333 self::$asyncTaskRun[$taskClass] = new TimingsHandler(
334 "AsyncTask - " . self::shortenCoreClassName($taskClass, "pocketmine\\") . " - Run",
335 self::$asyncTaskWorkers,
336 $group
337 );
338 }
339
340 return self::$asyncTaskRun[$taskClass];
341 }
342}
static getEventHandlerTimings(string $event, string $handlerName, string $group)
Definition Timings.php:307
static getScheduledTaskTimings(TaskHandler $task, int $period)
Definition Timings.php:196