mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-12 00:39:45 +00:00
Improved network bandwidth data collection
This commit is contained in:
parent
92afad5e6f
commit
bc52a38922
@ -1587,14 +1587,15 @@ class Server{
|
||||
|
||||
$online = count($this->playerList);
|
||||
$connecting = $this->network->getConnectionCount() - $online;
|
||||
$bandwidthStats = $this->network->getBandwidthTracker();
|
||||
|
||||
echo "\x1b]0;" . $this->getName() . " " .
|
||||
$this->getPocketMineVersion() .
|
||||
" | Online $online/" . $this->getMaxPlayers() .
|
||||
($connecting > 0 ? " (+$connecting connecting)" : "") .
|
||||
" | Memory " . $usage .
|
||||
" | U " . round($this->network->getUpload() / 1024, 2) .
|
||||
" D " . round($this->network->getDownload() / 1024, 2) .
|
||||
" | U " . round($bandwidthStats->getSend()->getAverageBytes() / 1024, 2) .
|
||||
" D " . round($bandwidthStats->getReceive()->getAverageBytes() / 1024, 2) .
|
||||
" kB/s | TPS " . $this->getTicksPerSecondAverage() .
|
||||
" | Load " . $this->getTickUsageAverage() . "%\x07";
|
||||
|
||||
@ -1640,7 +1641,7 @@ class Server{
|
||||
$this->queryInfo = $queryRegenerateEvent->getQueryInfo();
|
||||
|
||||
$this->network->updateName();
|
||||
$this->network->resetStatistics();
|
||||
$this->network->getBandwidthTracker()->rotateAverageHistory();
|
||||
}
|
||||
|
||||
if($this->sendUsageTicker > 0 and --$this->sendUsageTicker === 0){
|
||||
|
@ -91,8 +91,9 @@ class StatusCommand extends VanillaCommand{
|
||||
$sender->sendMessage(TextFormat::GOLD . "Current TPS: {$tpsColor}{$server->getTicksPerSecond()} ({$server->getTickUsage()}%)");
|
||||
$sender->sendMessage(TextFormat::GOLD . "Average TPS: {$tpsColor}{$server->getTicksPerSecondAverage()} ({$server->getTickUsageAverage()}%)");
|
||||
|
||||
$sender->sendMessage(TextFormat::GOLD . "Network upload: " . TextFormat::RED . round($server->getNetwork()->getUpload() / 1024, 2) . " kB/s");
|
||||
$sender->sendMessage(TextFormat::GOLD . "Network download: " . TextFormat::RED . round($server->getNetwork()->getDownload() / 1024, 2) . " kB/s");
|
||||
$bandwidth = $server->getNetwork()->getBandwidthTracker();
|
||||
$sender->sendMessage(TextFormat::GOLD . "Network upload: " . TextFormat::RED . round($bandwidth->getSend()->getAverageBytes() / 1024, 2) . " kB/s");
|
||||
$sender->sendMessage(TextFormat::GOLD . "Network download: " . TextFormat::RED . round($bandwidth->getReceive()->getAverageBytes() / 1024, 2) . " kB/s");
|
||||
|
||||
$sender->sendMessage(TextFormat::GOLD . "Thread count: " . TextFormat::RED . Process::getThreadCount());
|
||||
|
||||
|
74
src/network/BandwidthStatsTracker.php
Normal file
74
src/network/BandwidthStatsTracker.php
Normal file
@ -0,0 +1,74 @@
|
||||
<?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\network;
|
||||
|
||||
use function array_fill;
|
||||
use function array_sum;
|
||||
use function count;
|
||||
|
||||
final class BandwidthStatsTracker{
|
||||
/** @var int[] */
|
||||
private $history;
|
||||
/** @var int */
|
||||
private $nextHistoryIndex = 0;
|
||||
|
||||
/** @var int */
|
||||
private $bytesSinceLastRotation = 0;
|
||||
|
||||
/** @var int */
|
||||
private $totalBytes = 0;
|
||||
|
||||
public function __construct(int $historySize){
|
||||
$this->history = array_fill(0, $historySize, 0);
|
||||
}
|
||||
|
||||
public function add(int $bytes) : void{
|
||||
$this->totalBytes += $bytes;
|
||||
$this->bytesSinceLastRotation += $bytes;
|
||||
}
|
||||
|
||||
public function getTotalBytes() : int{ return $this->totalBytes; }
|
||||
|
||||
/**
|
||||
* Adds the bytes tracked since the last rotation to the history array, overwriting an old entry.
|
||||
* This should be called on a regular interval that you want to collect average measurements over
|
||||
* (e.g. if you want bytes per second, call this every second).
|
||||
*/
|
||||
public function rotateHistory() : void{
|
||||
$this->history[$this->nextHistoryIndex] = $this->bytesSinceLastRotation;
|
||||
$this->bytesSinceLastRotation = 0;
|
||||
$this->nextHistoryIndex = ($this->nextHistoryIndex + 1) % count($this->history);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the average of all the tracked history values.
|
||||
*/
|
||||
public function getAverageBytes() : float{
|
||||
return array_sum($this->history) / count($this->history);
|
||||
}
|
||||
|
||||
public function resetHistory() : void{
|
||||
$this->history = array_fill(0, count($this->history), 0);
|
||||
}
|
||||
}
|
59
src/network/BidirectionalBandwidthStatsTracker.php
Normal file
59
src/network/BidirectionalBandwidthStatsTracker.php
Normal file
@ -0,0 +1,59 @@
|
||||
<?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\network;
|
||||
|
||||
final class BidirectionalBandwidthStatsTracker{
|
||||
|
||||
/** @var BandwidthStatsTracker */
|
||||
private $send;
|
||||
|
||||
/** @var BandwidthStatsTracker */
|
||||
private $receive;
|
||||
|
||||
public function __construct(int $historySize){
|
||||
$this->send = new BandwidthStatsTracker($historySize);
|
||||
$this->receive = new BandwidthStatsTracker($historySize);
|
||||
}
|
||||
|
||||
public function getSend() : BandwidthStatsTracker{ return $this->send; }
|
||||
|
||||
public function getReceive() : BandwidthStatsTracker{ return $this->receive; }
|
||||
|
||||
public function add(int $sendBytes, int $recvBytes) : void{
|
||||
$this->send->add($sendBytes);
|
||||
$this->receive->add($recvBytes);
|
||||
}
|
||||
|
||||
/** @see BandwidthStatsTracker::rotateHistory() */
|
||||
public function rotateAverageHistory() : void{
|
||||
$this->send->rotateHistory();
|
||||
$this->receive->rotateHistory();
|
||||
}
|
||||
|
||||
/** @see BandwidthStatsTracker::resetHistory() */
|
||||
public function resetHistory() : void{
|
||||
$this->send->resetHistory();
|
||||
$this->receive->resetHistory();
|
||||
}
|
||||
}
|
@ -48,10 +48,8 @@ class Network{
|
||||
/** @var int[] */
|
||||
private $bannedIps = [];
|
||||
|
||||
/** @var float */
|
||||
private $upload = 0;
|
||||
/** @var float */
|
||||
private $download = 0;
|
||||
/** @var BandwidthStatsTracker */
|
||||
private $bandwidthTracker;
|
||||
|
||||
/** @var string */
|
||||
private $name;
|
||||
@ -65,25 +63,10 @@ class Network{
|
||||
public function __construct(\Logger $logger){
|
||||
$this->sessionManager = new NetworkSessionManager();
|
||||
$this->logger = $logger;
|
||||
$this->bandwidthTracker = new BidirectionalBandwidthStatsTracker(5);
|
||||
}
|
||||
|
||||
public function addStatistics(float $upload, float $download) : void{
|
||||
$this->upload += $upload;
|
||||
$this->download += $download;
|
||||
}
|
||||
|
||||
public function getUpload() : float{
|
||||
return $this->upload;
|
||||
}
|
||||
|
||||
public function getDownload() : float{
|
||||
return $this->download;
|
||||
}
|
||||
|
||||
public function resetStatistics() : void{
|
||||
$this->upload = 0;
|
||||
$this->download = 0;
|
||||
}
|
||||
public function getBandwidthTracker() : BidirectionalBandwidthStatsTracker{ return $this->bandwidthTracker; }
|
||||
|
||||
/**
|
||||
* @return NetworkInterface[]
|
||||
|
@ -43,7 +43,6 @@ use raklib\utils\InternetAddress;
|
||||
use function addcslashes;
|
||||
use function bin2hex;
|
||||
use function implode;
|
||||
use function microtime;
|
||||
use function mt_rand;
|
||||
use function random_bytes;
|
||||
use function rtrim;
|
||||
@ -83,13 +82,9 @@ class RakLibInterface implements ServerEventListener, AdvancedNetworkInterface{
|
||||
/** @var SleeperNotifier */
|
||||
private $sleeper;
|
||||
|
||||
/** @var float */
|
||||
private $lastBandwidthReport;
|
||||
|
||||
public function __construct(Server $server){
|
||||
$this->server = $server;
|
||||
$this->rakServerId = mt_rand(0, PHP_INT_MAX);
|
||||
$this->lastBandwidthReport = microtime(true);
|
||||
|
||||
$this->sleeper = new SleeperNotifier();
|
||||
|
||||
@ -250,10 +245,7 @@ class RakLibInterface implements ServerEventListener, AdvancedNetworkInterface{
|
||||
}
|
||||
|
||||
public function handleBandwidthStats(int $bytesSentDiff, int $bytesReceivedDiff) : void{
|
||||
$now = microtime(true);
|
||||
$diff = $now - $this->lastBandwidthReport;
|
||||
$this->lastBandwidthReport = $now;
|
||||
$this->network->addStatistics($bytesSentDiff / $diff, $bytesReceivedDiff / $diff);
|
||||
$this->network->getBandwidthTracker()->add($bytesSentDiff, $bytesReceivedDiff);
|
||||
}
|
||||
|
||||
public function putPacket(int $sessionId, string $payload, bool $immediate = true) : void{
|
||||
|
Loading…
x
Reference in New Issue
Block a user