Timings: added broadcastPackets timer

the body of this function is potentially very expensive and isn't currently recorded by any timer.
This commit is contained in:
Dylan K. Taylor 2020-11-12 17:53:07 +00:00
parent afbef242c6
commit 31089ce3b2
2 changed files with 46 additions and 39 deletions

View File

@ -1182,54 +1182,56 @@ class Server{
throw new \InvalidArgumentException("Cannot broadcast empty list of packets"); throw new \InvalidArgumentException("Cannot broadcast empty list of packets");
} }
/** @var NetworkSession[] $recipients */ return Timings::$broadcastPackets->time(function() use ($players, $packets) : bool{
$recipients = []; /** @var NetworkSession[] $recipients */
foreach($players as $player){ $recipients = [];
if($player->isConnected()){ foreach($players as $player){
$recipients[] = $player->getNetworkSession(); if($player->isConnected()){
$recipients[] = $player->getNetworkSession();
}
}
if(count($recipients) === 0){
return false;
} }
}
if(count($recipients) === 0){
return false;
}
$ev = new DataPacketSendEvent($recipients, $packets); $ev = new DataPacketSendEvent($recipients, $packets);
$ev->call(); $ev->call();
if($ev->isCancelled()){ if($ev->isCancelled()){
return false; return false;
} }
$recipients = $ev->getTargets(); $recipients = $ev->getTargets();
$stream = PacketBatch::fromPackets(...$ev->getPackets()); $stream = PacketBatch::fromPackets(...$ev->getPackets());
/** @var Compressor[] $compressors */ /** @var Compressor[] $compressors */
$compressors = []; $compressors = [];
/** @var NetworkSession[][] $compressorTargets */ /** @var NetworkSession[][] $compressorTargets */
$compressorTargets = []; $compressorTargets = [];
foreach($recipients as $recipient){ foreach($recipients as $recipient){
$compressor = $recipient->getCompressor(); $compressor = $recipient->getCompressor();
$compressorId = spl_object_id($compressor); $compressorId = spl_object_id($compressor);
//TODO: different compressors might be compatible, it might not be necessary to split them up by object //TODO: different compressors might be compatible, it might not be necessary to split them up by object
$compressors[$compressorId] = $compressor; $compressors[$compressorId] = $compressor;
$compressorTargets[$compressorId][] = $recipient; $compressorTargets[$compressorId][] = $recipient;
} }
foreach($compressors as $compressorId => $compressor){ foreach($compressors as $compressorId => $compressor){
if(!$compressor->willCompress($stream->getBuffer())){ if(!$compressor->willCompress($stream->getBuffer())){
foreach($compressorTargets[$compressorId] as $target){ foreach($compressorTargets[$compressorId] as $target){
foreach($ev->getPackets() as $pk){ foreach($ev->getPackets() as $pk){
$target->addToSendBuffer($pk); $target->addToSendBuffer($pk);
}
}
}else{
$promise = $this->prepareBatch($stream, $compressor);
foreach($compressorTargets[$compressorId] as $target){
$target->queueCompressed($promise);
} }
} }
}else{
$promise = $this->prepareBatch($stream, $compressor);
foreach($compressorTargets[$compressorId] as $target){
$target->queueCompressed($promise);
}
} }
}
return true; return true;
});
} }
/** /**

View File

@ -118,6 +118,9 @@ abstract class Timings{
/** @var TimingsHandler[] */ /** @var TimingsHandler[] */
public static $pluginTaskTimingMap = []; public static $pluginTaskTimingMap = [];
/** @var TimingsHandler */
public static $broadcastPackets;
public static function init() : void{ public static function init() : void{
if(self::$initialized){ if(self::$initialized){
return; return;
@ -138,6 +141,8 @@ abstract class Timings{
self::$playerNetworkReceiveDecompressTimer = new TimingsHandler(self::INCLUDED_BY_OTHER_TIMINGS_PREFIX . "Player Network Receive - Decompression", self::$playerNetworkReceiveTimer); self::$playerNetworkReceiveDecompressTimer = new TimingsHandler(self::INCLUDED_BY_OTHER_TIMINGS_PREFIX . "Player Network Receive - Decompression", self::$playerNetworkReceiveTimer);
self::$playerNetworkReceiveDecryptTimer = new TimingsHandler(self::INCLUDED_BY_OTHER_TIMINGS_PREFIX . "Player Network Receive - Decryption", self::$playerNetworkReceiveTimer); self::$playerNetworkReceiveDecryptTimer = new TimingsHandler(self::INCLUDED_BY_OTHER_TIMINGS_PREFIX . "Player Network Receive - Decryption", self::$playerNetworkReceiveTimer);
self::$broadcastPackets = new TimingsHandler(self::INCLUDED_BY_OTHER_TIMINGS_PREFIX . "Broadcast Packets", self::$playerNetworkSendTimer);
self::$playerChunkOrderTimer = new TimingsHandler("Player Order Chunks"); self::$playerChunkOrderTimer = new TimingsHandler("Player Order Chunks");
self::$playerChunkSendTimer = new TimingsHandler("Player Send Chunks"); self::$playerChunkSendTimer = new TimingsHandler("Player Send Chunks");
self::$connectionTimer = new TimingsHandler("Connection Handler"); self::$connectionTimer = new TimingsHandler("Connection Handler");