mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-04 17:06:16 +00:00
Removed pocketmine subdirectory, map PSR-4 style
This commit is contained in:
245
src/timings/Timings.php
Normal file
245
src/timings/Timings.php
Normal file
@ -0,0 +1,245 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\timings;
|
||||
|
||||
use pocketmine\block\tile\Tile;
|
||||
use pocketmine\entity\Entity;
|
||||
use pocketmine\network\mcpe\protocol\ClientboundPacket;
|
||||
use pocketmine\network\mcpe\protocol\ServerboundPacket;
|
||||
use pocketmine\player\Player;
|
||||
use pocketmine\scheduler\TaskHandler;
|
||||
use function dechex;
|
||||
|
||||
abstract class Timings{
|
||||
|
||||
/** @var TimingsHandler */
|
||||
public static $fullTickTimer;
|
||||
/** @var TimingsHandler */
|
||||
public static $serverTickTimer;
|
||||
/** @var TimingsHandler */
|
||||
public static $memoryManagerTimer;
|
||||
/** @var TimingsHandler */
|
||||
public static $garbageCollectorTimer;
|
||||
/** @var TimingsHandler */
|
||||
public static $titleTickTimer;
|
||||
/** @var TimingsHandler */
|
||||
public static $playerNetworkSendTimer;
|
||||
/** @var TimingsHandler */
|
||||
public static $playerNetworkSendCompressTimer;
|
||||
/** @var TimingsHandler */
|
||||
public static $playerNetworkSendEncryptTimer;
|
||||
/** @var TimingsHandler */
|
||||
public static $playerNetworkReceiveTimer;
|
||||
/** @var TimingsHandler */
|
||||
public static $playerNetworkReceiveDecompressTimer;
|
||||
/** @var TimingsHandler */
|
||||
public static $playerNetworkReceiveDecryptTimer;
|
||||
/** @var TimingsHandler */
|
||||
public static $playerChunkOrderTimer;
|
||||
/** @var TimingsHandler */
|
||||
public static $playerChunkSendTimer;
|
||||
/** @var TimingsHandler */
|
||||
public static $connectionTimer;
|
||||
/** @var TimingsHandler */
|
||||
public static $schedulerTimer;
|
||||
/** @var TimingsHandler */
|
||||
public static $serverCommandTimer;
|
||||
/** @var TimingsHandler */
|
||||
public static $worldSaveTimer;
|
||||
/** @var TimingsHandler */
|
||||
public static $populationTimer;
|
||||
/** @var TimingsHandler */
|
||||
public static $generationCallbackTimer;
|
||||
/** @var TimingsHandler */
|
||||
public static $permissibleCalculationTimer;
|
||||
/** @var TimingsHandler */
|
||||
public static $permissionDefaultTimer;
|
||||
|
||||
/** @var TimingsHandler */
|
||||
public static $entityMoveTimer;
|
||||
/** @var TimingsHandler */
|
||||
public static $playerCheckNearEntitiesTimer;
|
||||
/** @var TimingsHandler */
|
||||
public static $tickEntityTimer;
|
||||
/** @var TimingsHandler */
|
||||
public static $tickTileEntityTimer;
|
||||
|
||||
/** @var TimingsHandler */
|
||||
public static $timerEntityBaseTick;
|
||||
/** @var TimingsHandler */
|
||||
public static $timerLivingEntityBaseTick;
|
||||
|
||||
/** @var TimingsHandler */
|
||||
public static $schedulerSyncTimer;
|
||||
/** @var TimingsHandler */
|
||||
public static $schedulerAsyncTimer;
|
||||
|
||||
/** @var TimingsHandler */
|
||||
public static $playerCommandTimer;
|
||||
|
||||
/** @var TimingsHandler */
|
||||
public static $craftingDataCacheRebuildTimer;
|
||||
|
||||
/** @var TimingsHandler[] */
|
||||
public static $entityTypeTimingMap = [];
|
||||
/** @var TimingsHandler[] */
|
||||
public static $tileEntityTypeTimingMap = [];
|
||||
/** @var TimingsHandler[] */
|
||||
public static $packetReceiveTimingMap = [];
|
||||
/** @var TimingsHandler[] */
|
||||
public static $packetSendTimingMap = [];
|
||||
/** @var TimingsHandler[] */
|
||||
public static $pluginTaskTimingMap = [];
|
||||
|
||||
public static function init() : void{
|
||||
if(self::$serverTickTimer instanceof TimingsHandler){
|
||||
return;
|
||||
}
|
||||
|
||||
self::$fullTickTimer = new TimingsHandler("Full Server Tick");
|
||||
self::$serverTickTimer = new TimingsHandler("** Full Server Tick", self::$fullTickTimer);
|
||||
self::$memoryManagerTimer = new TimingsHandler("Memory Manager");
|
||||
self::$garbageCollectorTimer = new TimingsHandler("Garbage Collector", self::$memoryManagerTimer);
|
||||
self::$titleTickTimer = new TimingsHandler("Console Title Tick");
|
||||
|
||||
self::$playerNetworkSendTimer = new TimingsHandler("Player Network Send");
|
||||
self::$playerNetworkSendCompressTimer = new TimingsHandler("** Player Network Send - Compression", self::$playerNetworkSendTimer);
|
||||
self::$playerNetworkSendEncryptTimer = new TimingsHandler("** Player Network Send - Encryption", self::$playerNetworkSendTimer);
|
||||
|
||||
self::$playerNetworkReceiveTimer = new TimingsHandler("Player Network Receive");
|
||||
self::$playerNetworkReceiveDecompressTimer = new TimingsHandler("** Player Network Receive - Decompression", self::$playerNetworkReceiveTimer);
|
||||
self::$playerNetworkReceiveDecryptTimer = new TimingsHandler("** Player Network Receive - Decryption", self::$playerNetworkReceiveTimer);
|
||||
|
||||
self::$playerChunkOrderTimer = new TimingsHandler("Player Order Chunks");
|
||||
self::$playerChunkSendTimer = new TimingsHandler("Player Send Chunks");
|
||||
self::$connectionTimer = new TimingsHandler("Connection Handler");
|
||||
self::$schedulerTimer = new TimingsHandler("Scheduler");
|
||||
self::$serverCommandTimer = new TimingsHandler("Server Command");
|
||||
self::$worldSaveTimer = new TimingsHandler("World Save");
|
||||
self::$populationTimer = new TimingsHandler("World Population");
|
||||
self::$generationCallbackTimer = new TimingsHandler("World Generation Callback");
|
||||
self::$permissibleCalculationTimer = new TimingsHandler("Permissible Calculation");
|
||||
self::$permissionDefaultTimer = new TimingsHandler("Default Permission Calculation");
|
||||
|
||||
self::$entityMoveTimer = new TimingsHandler("** entityMove");
|
||||
self::$playerCheckNearEntitiesTimer = new TimingsHandler("** checkNearEntities");
|
||||
self::$tickEntityTimer = new TimingsHandler("** tickEntity");
|
||||
self::$tickTileEntityTimer = new TimingsHandler("** tickTileEntity");
|
||||
|
||||
self::$timerEntityBaseTick = new TimingsHandler("** entityBaseTick");
|
||||
self::$timerLivingEntityBaseTick = new TimingsHandler("** livingEntityBaseTick");
|
||||
|
||||
self::$schedulerSyncTimer = new TimingsHandler("** Scheduler - Sync Tasks");
|
||||
self::$schedulerAsyncTimer = new TimingsHandler("** Scheduler - Async Tasks");
|
||||
|
||||
self::$playerCommandTimer = new TimingsHandler("** playerCommand");
|
||||
self::$craftingDataCacheRebuildTimer = new TimingsHandler("** craftingDataCacheRebuild");
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TaskHandler $task
|
||||
* @param int $period
|
||||
*
|
||||
* @return TimingsHandler
|
||||
*/
|
||||
public static function getScheduledTaskTimings(TaskHandler $task, int $period) : TimingsHandler{
|
||||
$name = "Task: " . ($task->getOwnerName() ?? "Unknown") . " Runnable: " . $task->getTaskName();
|
||||
|
||||
if($period > 0){
|
||||
$name .= "(interval:" . $period . ")";
|
||||
}else{
|
||||
$name .= "(Single)";
|
||||
}
|
||||
|
||||
if(!isset(self::$pluginTaskTimingMap[$name])){
|
||||
self::$pluginTaskTimingMap[$name] = new TimingsHandler($name, self::$schedulerSyncTimer);
|
||||
}
|
||||
|
||||
return self::$pluginTaskTimingMap[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Entity $entity
|
||||
*
|
||||
* @return TimingsHandler
|
||||
*/
|
||||
public static function getEntityTimings(Entity $entity) : TimingsHandler{
|
||||
$entityType = (new \ReflectionClass($entity))->getShortName();
|
||||
if(!isset(self::$entityTypeTimingMap[$entityType])){
|
||||
if($entity instanceof Player){
|
||||
self::$entityTypeTimingMap[$entityType] = new TimingsHandler("** tickEntity - EntityPlayer", self::$tickEntityTimer);
|
||||
}else{
|
||||
self::$entityTypeTimingMap[$entityType] = new TimingsHandler("** tickEntity - " . $entityType, self::$tickEntityTimer);
|
||||
}
|
||||
}
|
||||
|
||||
return self::$entityTypeTimingMap[$entityType];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Tile $tile
|
||||
*
|
||||
* @return TimingsHandler
|
||||
*/
|
||||
public static function getTileEntityTimings(Tile $tile) : TimingsHandler{
|
||||
$tileType = (new \ReflectionClass($tile))->getShortName();
|
||||
if(!isset(self::$tileEntityTypeTimingMap[$tileType])){
|
||||
self::$tileEntityTypeTimingMap[$tileType] = new TimingsHandler("** tickTileEntity - " . $tileType, self::$tickTileEntityTimer);
|
||||
}
|
||||
|
||||
return self::$tileEntityTypeTimingMap[$tileType];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ServerboundPacket $pk
|
||||
*
|
||||
* @return TimingsHandler
|
||||
*/
|
||||
public static function getReceiveDataPacketTimings(ServerboundPacket $pk) : TimingsHandler{
|
||||
$pid = $pk->pid();
|
||||
if(!isset(self::$packetReceiveTimingMap[$pid])){
|
||||
$pkName = (new \ReflectionClass($pk))->getShortName();
|
||||
self::$packetReceiveTimingMap[$pid] = new TimingsHandler("** receivePacket - " . $pkName . " [0x" . dechex($pid) . "]", self::$playerNetworkReceiveTimer);
|
||||
}
|
||||
|
||||
return self::$packetReceiveTimingMap[$pid];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ClientboundPacket $pk
|
||||
*
|
||||
* @return TimingsHandler
|
||||
*/
|
||||
public static function getSendDataPacketTimings(ClientboundPacket $pk) : TimingsHandler{
|
||||
$pid = $pk->pid();
|
||||
if(!isset(self::$packetSendTimingMap[$pid])){
|
||||
$pkName = (new \ReflectionClass($pk))->getShortName();
|
||||
self::$packetSendTimingMap[$pid] = new TimingsHandler("** sendPacket - " . $pkName . " [0x" . dechex($pid) . "]", self::$playerNetworkSendTimer);
|
||||
}
|
||||
|
||||
return self::$packetSendTimingMap[$pid];
|
||||
}
|
||||
}
|
214
src/timings/TimingsHandler.php
Normal file
214
src/timings/TimingsHandler.php
Normal file
@ -0,0 +1,214 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\timings;
|
||||
|
||||
use pocketmine\entity\Living;
|
||||
use pocketmine\Server;
|
||||
use function count;
|
||||
use function fwrite;
|
||||
use function microtime;
|
||||
use function round;
|
||||
use function spl_object_id;
|
||||
use const PHP_EOL;
|
||||
|
||||
class TimingsHandler{
|
||||
|
||||
/** @var TimingsHandler[] */
|
||||
private static $HANDLERS = [];
|
||||
/** @var bool */
|
||||
private static $enabled = false;
|
||||
/** @var float */
|
||||
private static $timingStart = 0;
|
||||
|
||||
/**
|
||||
* @param resource $fp
|
||||
*/
|
||||
public static function printTimings($fp) : void{
|
||||
fwrite($fp, "Minecraft" . PHP_EOL);
|
||||
|
||||
foreach(self::$HANDLERS as $timings){
|
||||
$time = $timings->totalTime;
|
||||
$count = $timings->count;
|
||||
if($count === 0){
|
||||
continue;
|
||||
}
|
||||
|
||||
$avg = $time / $count;
|
||||
|
||||
fwrite($fp, " " . $timings->name . " Time: " . round($time * 1000000000) . " Count: " . $count . " Avg: " . round($avg * 1000000000) . " Violations: " . $timings->violations . PHP_EOL);
|
||||
}
|
||||
|
||||
fwrite($fp, "# Version " . Server::getInstance()->getVersion() . PHP_EOL);
|
||||
fwrite($fp, "# " . Server::getInstance()->getName() . " " . Server::getInstance()->getPocketMineVersion() . PHP_EOL);
|
||||
|
||||
$entities = 0;
|
||||
$livingEntities = 0;
|
||||
foreach(Server::getInstance()->getWorldManager()->getWorlds() as $world){
|
||||
$entities += count($world->getEntities());
|
||||
foreach($world->getEntities() as $e){
|
||||
if($e instanceof Living){
|
||||
++$livingEntities;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fwrite($fp, "# Entities " . $entities . PHP_EOL);
|
||||
fwrite($fp, "# LivingEntities " . $livingEntities . PHP_EOL);
|
||||
|
||||
$sampleTime = microtime(true) - self::$timingStart;
|
||||
fwrite($fp, "Sample time " . round($sampleTime * 1000000000) . " (" . $sampleTime . "s)" . PHP_EOL);
|
||||
}
|
||||
|
||||
public static function isEnabled() : bool{
|
||||
return self::$enabled;
|
||||
}
|
||||
|
||||
public static function setEnabled(bool $enable = true) : void{
|
||||
self::$enabled = $enable;
|
||||
self::reload();
|
||||
}
|
||||
|
||||
public static function getStartTime() : float{
|
||||
return self::$timingStart;
|
||||
}
|
||||
|
||||
public static function reload() : void{
|
||||
if(self::$enabled){
|
||||
foreach(self::$HANDLERS as $timings){
|
||||
$timings->reset();
|
||||
}
|
||||
self::$timingStart = microtime(true);
|
||||
}
|
||||
}
|
||||
|
||||
public static function tick(bool $measure = true) : void{
|
||||
if(self::$enabled){
|
||||
if($measure){
|
||||
foreach(self::$HANDLERS as $timings){
|
||||
if($timings->curTickTotal > 0.05){
|
||||
$timings->violations += (int) round($timings->curTickTotal / 0.05);
|
||||
}
|
||||
$timings->curTickTotal = 0;
|
||||
$timings->curCount = 0;
|
||||
$timings->timingDepth = 0;
|
||||
}
|
||||
}else{
|
||||
foreach(self::$HANDLERS as $timings){
|
||||
$timings->totalTime -= $timings->curTickTotal;
|
||||
$timings->count -= $timings->curCount;
|
||||
|
||||
$timings->curTickTotal = 0;
|
||||
$timings->curCount = 0;
|
||||
$timings->timingDepth = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** @var string */
|
||||
private $name;
|
||||
/** @var TimingsHandler */
|
||||
private $parent = null;
|
||||
|
||||
/** @var int */
|
||||
private $count = 0;
|
||||
/** @var int */
|
||||
private $curCount = 0;
|
||||
/** @var float */
|
||||
private $start = 0;
|
||||
/** @var int */
|
||||
private $timingDepth = 0;
|
||||
/** @var float */
|
||||
private $totalTime = 0;
|
||||
/** @var float */
|
||||
private $curTickTotal = 0;
|
||||
/** @var int */
|
||||
private $violations = 0;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param TimingsHandler $parent
|
||||
*/
|
||||
public function __construct(string $name, ?TimingsHandler $parent = null){
|
||||
$this->name = $name;
|
||||
$this->parent = $parent;
|
||||
|
||||
self::$HANDLERS[spl_object_id($this)] = $this;
|
||||
}
|
||||
|
||||
public function startTiming() : void{
|
||||
if(self::$enabled and ++$this->timingDepth === 1){
|
||||
$this->start = microtime(true);
|
||||
if($this->parent !== null and ++$this->parent->timingDepth === 1){
|
||||
$this->parent->start = $this->start;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function stopTiming() : void{
|
||||
if(self::$enabled){
|
||||
if(--$this->timingDepth !== 0 or $this->start === 0){
|
||||
return;
|
||||
}
|
||||
|
||||
$diff = microtime(true) - $this->start;
|
||||
$this->totalTime += $diff;
|
||||
$this->curTickTotal += $diff;
|
||||
++$this->curCount;
|
||||
++$this->count;
|
||||
$this->start = 0;
|
||||
if($this->parent !== null){
|
||||
$this->parent->stopTiming();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Closure $closure
|
||||
*
|
||||
* @return mixed the result of the given closure
|
||||
*/
|
||||
public function time(\Closure $closure){
|
||||
$this->startTiming();
|
||||
try{
|
||||
return $closure();
|
||||
}finally{
|
||||
$this->stopTiming();
|
||||
}
|
||||
}
|
||||
|
||||
public function reset() : void{
|
||||
$this->count = 0;
|
||||
$this->curCount = 0;
|
||||
$this->violations = 0;
|
||||
$this->curTickTotal = 0;
|
||||
$this->totalTime = 0;
|
||||
$this->start = 0;
|
||||
$this->timingDepth = 0;
|
||||
}
|
||||
|
||||
public function remove() : void{
|
||||
unset(self::$HANDLERS[spl_object_id($this)]);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user