From f42f2d5e82a7ed915ff54a81fe344468d290673a Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Mon, 17 Dec 2012 19:50:55 +0100 Subject: [PATCH] Ticks per Second info --- TODO | 3 ++- classes/API/ConsoleAPI.php | 9 ++++++++- classes/API/ServerAPI.php | 2 +- classes/PocketMinecraftServer.class.php | 17 +++++++++++++---- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/TODO b/TODO index 0dfe2a669..b0f84f54a 100644 --- a/TODO +++ b/TODO @@ -1,4 +1,5 @@ - Save placed blocks and relay them to other players. - Fix spawn position resetting to 0,128,0 - Mob spawning, item pick up -- Fix metadata orientation \ No newline at end of file +- Fix metadata orientation +- Proper session checks \ No newline at end of file diff --git a/classes/API/ConsoleAPI.php b/classes/API/ConsoleAPI.php index 0de052892..6c0edae3a 100644 --- a/classes/API/ConsoleAPI.php +++ b/classes/API/ConsoleAPI.php @@ -31,6 +31,7 @@ class ConsoleAPI{ $this->help = array(); $this->server = $server; $this->input = fopen(FILE_PATH."console.in", "w+b"); + $this->last = microtime(true); } public function init(){ @@ -44,6 +45,11 @@ class ConsoleAPI{ public function defaultCommands($cmd, $params){ switch($cmd){ + case "status": + case "lag": + $info = $this->server->debugInfo(); + console("[INFO] TPS: ".$info["tps"].", Memory usage: ".$info["memory_usage"]." (Peak ".$info["memory_peak_usage"].")"); + break; case "update-done": $this->server->api->setProperty("last-update", time()); break; @@ -175,6 +181,7 @@ class ConsoleAPI{ case "help": case "?": console("[INFO] /help: Show available commands"); + console("[INFO] /status: Show server TPS and memory usage"); console("[INFO] /gamemode: Changes default gamemode"); console("[INFO] /difficulty: Changes difficulty"); console("[INFO] /say: Broadcasts mesages"); @@ -197,7 +204,7 @@ class ConsoleAPI{ $this->help[strtolower(trim($cmd))] = array($help, $callback); } - public function handle(){ + public function handle($time){ while(($line = fgets($this->input)) !== false){ $line = trim($line); if($line === ""){ diff --git a/classes/API/ServerAPI.php b/classes/API/ServerAPI.php index 9d8e7049c..c5259f9b2 100644 --- a/classes/API/ServerAPI.php +++ b/classes/API/ServerAPI.php @@ -66,7 +66,7 @@ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in run console("[NOTICE] Pocket-PHP-Minecraft has been updated at ".date("Y-m-d H:i:s", $last)); console("[NOTICE] If you want to update, get the latest version at https://github.com/shoghicp/Pocket-Minecraft-PHP/archive/master.zip"); console("[NOTICE] This message will dissapear when you issue the command \"/update-done\""); - sleep(5); + sleep(3); }else{ $last = time(); $this->setProperty("last-update", $last); diff --git a/classes/PocketMinecraftServer.class.php b/classes/PocketMinecraftServer.class.php index cfbe5b68c..ec3aa3b33 100644 --- a/classes/PocketMinecraftServer.class.php +++ b/classes/PocketMinecraftServer.class.php @@ -28,7 +28,7 @@ the Free Software Foundation, either version 3 of the License, or require_once("classes/Session.class.php"); class PocketMinecraftServer extends stdClass{ - var $preparedSQL, $seed, $protocol, $gamemode, $name, $maxClients, $clients, $eidCnt, $custom, $description, $motd, $timePerSecond, $responses, $spawn, $entities, $mapDir, $mapName, $map, $level, $tileEntities; + var $tickMeasure, $preparedSQL, $seed, $protocol, $gamemode, $name, $maxClients, $clients, $eidCnt, $custom, $description, $motd, $timePerSecond, $responses, $spawn, $entities, $mapDir, $mapName, $map, $level, $tileEntities; private $database, $interface, $evCnt, $handCnt, $events, $handlers, $version, $serverType, $lastTick; function __construct($name, $gamemode = 1, $seed = false, $protocol = CURRENT_PROTOCOL, $port = 19132, $serverID = false, $version = CURRENT_VERSION){ $this->port = (int) $port; //19132 - 19135 @@ -67,6 +67,7 @@ class PocketMinecraftServer extends stdClass{ $this->spawn = array("x" => 128.5,"y" => 100,"z" => 128.5); $this->time = 0; $this->timePerSecond = 10; + $this->tickMeasure = array_fill(0, 40, 0); $this->setType("normal"); $this->interface = new MinecraftInterface("255.255.255.255", $this->protocol, $this->port, true, false); $this->reloadConfig(); @@ -77,13 +78,19 @@ class PocketMinecraftServer extends stdClass{ $this->stop = false; } + public function getTPS(){ + $v = array_values($this->tickMeasure); + $tps = 40/($v[39] - $v[0]); + return round($tps, 4); + } + public function loadEvents(){ $this->event("onChat", "eventHandler", true); $this->event("onPlayerDeath", "eventHandler", true); $this->event("onPlayerAdd", "eventHandler", true); $this->event("onHealthChange", "eventHandler", true); - $this->action(500000, '$this->time += ceil($this->timePerSecond / 2);$this->trigger("onTimeChange", $this->time);'); + $this->action(500000, '$this->time += (int) ($this->timePerSecond / 2);$this->trigger("onTimeChange", $this->time);'); $this->action(5000000, '$this->trigger("onHealthRegeneration", 1);'); $this->action(1000000 * 60, '$this->reloadConfig();'); $this->action(1000000 * 60 * 10, '$this->custom = array();'); @@ -130,6 +137,7 @@ class PocketMinecraftServer extends stdClass{ public function debugInfo($console = false){ $info = array(); + $info["tps"] = $this->getTPS(); $info["memory_usage"] = round((memory_get_usage(true) / 1024) / 1024, 2)."MB"; $info["memory_peak_usage"] = round((memory_get_peak_usage(true) / 1024) / 1024, 2)."MB"; $info["entities"] = $this->query("SELECT count(EID) as count FROM entities;", true); @@ -140,7 +148,7 @@ class PocketMinecraftServer extends stdClass{ $info["actions"] = $info["actions"]["count"]; $info["garbage"] = gc_collect_cycles(); if($console === true){ - console("[DEBUG] Memory usage: ".$info["memory_usage"]." (Peak ".$info["memory_peak_usage"]."), Entities: ".$info["entities"].", Events: ".$info["events"].", Actions: ".$info["actions"].", Garbage: ".$info["garbage"], true, true, 2); + console("[DEBUG] TPS: ".$info["tps"].", Memory usage: ".$info["memory_usage"]." (Peak ".$info["memory_peak_usage"]."), Entities: ".$info["entities"].", Events: ".$info["events"].", Actions: ".$info["actions"].", Garbage: ".$info["garbage"], true, true, 2); } return $info; } @@ -310,7 +318,8 @@ class PocketMinecraftServer extends stdClass{ public function tick(){ $time = microtime(true); if($this->lastTick <= ($time - 0.05)){ - $this->lastTick = $time; + $this->tickMeasure[] = $this->lastTick = $time; + array_shift($this->tickMeasure); $this->trigger("onTick", $time); } }