From 10b603423f6572ad0d3cfaecfca254f2f11ca03d Mon Sep 17 00:00:00 2001 From: Tuomo Kriikkula Date: Sun, 7 Jul 2024 20:23:50 +0300 Subject: [PATCH] Use more frequent samples to calculate transfer rates --- Classes/FCryptoGMPClient.uc | 102 ++++++++++++++++++++++++++++++------ 1 file changed, 85 insertions(+), 17 deletions(-) diff --git a/Classes/FCryptoGMPClient.uc b/Classes/FCryptoGMPClient.uc index 81b50fe..713b536 100644 --- a/Classes/FCryptoGMPClient.uc +++ b/Classes/FCryptoGMPClient.uc @@ -86,12 +86,23 @@ const ID_PRIME = "PRIME"; const TID_PRIME = "TPRIME"; // Bitrate calculations. -// First packet has been sent. -var bool bTransfersStarted; var float StartTimeSeconds; var float StopTimeSeconds; var int BytesOut; // TODO: does this overflow? var int BytesIn; // TODO: does this overflow? +var int LastBytesOut; +var int LastBytesIn; +var array BytesOutSamples; +var array BytesInSamples; +var float ByteRateOut; +var float ByteRateIn; +var int BytesOutSamplesIndex; +var int BytesInSamplesIndex; +const NUM_SAMPLES = 5; +var float MaxByteRateOut; +var float MaxByteRateIn; +var float AvgByteRateOut; +var float AvgByteRateIn; var delegate RandPrimeDelegate; delegate OnRandPrimeReceived(const out array P); @@ -100,22 +111,87 @@ simulated event PreBeginPlay() { super.PreBeginPlay(); + SampleTransferRates(); SetTimer(2.0, True, NameOf(LogTransferRates)); } -simulated final function LogTransferRates() +simulated final function SampleTransferRates() +{ + StartTimeSeconds = WorldInfo.RealTimeSeconds; + SetTimer(0.2, False, NameOf(StopTransferRateSample)); +} + +simulated final function StopTransferRateSample() { local float TimeSpent; - local float ByteRateOut; - local float ByteRateIn; + local float CurrentByteRateOut; + local float CurrentByteRateIn; + local float CurrentBytesOut; + local float CurrentBytesIn; + local int i; StopTimeSeconds = WorldInfo.RealTimeSeconds; TimeSpent = StopTimeSeconds - StartTimeSeconds; - ByteRateOut = BytesOut / TimeSpent; - ByteRateIn = BytesIn / TimeSpent; - `fclog("BytesOut :" @ ByteRateOut @ "B/s" @ ByteRateOut * BpsToMbps @ "Mb/s"); - `fclog("BytesIn :" @ ByteRateIn @ "B/s" @ ByteRateIn * BpsToMbps @ "Mb/s"); + CurrentBytesOut = BytesOut - LastBytesOut; + CurrentBytesIn = BytesIn - LastBytesIn; + + CurrentByteRateOut = CurrentBytesOut / TimeSpent; + CurrentByteRateIn = CurrentBytesIn / TimeSpent; + + // `fclog("CurrentByteRateOut=" $ CurrentByteRateOut); + // `fclog("CurrentByteRateIn=" $ CurrentByteRateIn); + + BytesOutSamplesIndex = (BytesOutSamplesIndex + 1) % NUM_SAMPLES; + BytesInSamplesIndex = (BytesInSamplesIndex + 1) % NUM_SAMPLES; + + BytesOutSamples[BytesOutSamplesIndex] = CurrentByteRateOut; + BytesInSamples[BytesInSamplesIndex] = CurrentByteRateIn; + + LastBytesOut = BytesOut; + LastBytesIn = BytesIn; + + for (i = 0; i < NUM_SAMPLES; ++i) + { + ByteRateOut += BytesOutSamples[i]; + ByteRateIn += BytesInSamples[i]; + } + + ByteRateOut /= NUM_SAMPLES; + ByteRateIn /= NUM_SAMPLES; + + MaxByteRateOut = Max(MaxByteRateOut, ByteRateOut); + MaxByteRateIn = Max(MaxByteRateIn, ByteRateIn); + + if (ByteRateOut > 0) + { + AvgByteRateOut += ByteRateOut; + AvgByteRateOut /= 2; + } + + if (ByteRateIn > 0) + { + AvgByteRateIn += ByteRateIn; + AvgByteRateIn /= 2; + } + + SampleTransferRates(); +} + +simulated final function LogTransferRates() +{ + `fclog( + "BytesOut :" @ ByteRateOut @ "B/s" + @ ByteRateOut * BpsToMbps @ "Mb/s" + @ AvgByteRateOut * BpsToMbps @ "(avg)" + @ MaxByteRateOut * BpsToMbps @ "(max)" + ); + `fclog( + "BytesIn :" @ ByteRateIn @ "B/s" + @ ByteRateIn * BpsToMbps @ "Mb/s" + @ AvgByteRateIn * BpsToMbps @ "(avg)" + @ MaxByteRateIn * BpsToMbps @ "(max)" + ); } simulated event Tick(float DeltaTime) @@ -317,12 +393,6 @@ final simulated function RandPrime(int Size) final function SendTextEx(coerce string Str) { - if (!bTransfersStarted) - { - bTransfersStarted = True; - StartTimeSeconds = WorldInfo.RealTimeSeconds; - } - // TODO: is this right? UScript strings are UTF-16 (UCS-2). BytesOut += Len(Str) * 2; @@ -386,6 +456,4 @@ DefaultProperties OutLineMode=LMODE_UNIX TickGroup=TG_DuringAsyncWork - - bTransfersStarted=False }