Skip to content

Commit 10b6034

Browse files
committed
Use more frequent samples to calculate transfer rates
1 parent 1e9a32c commit 10b6034

File tree

1 file changed

+85
-17
lines changed

1 file changed

+85
-17
lines changed

Classes/FCryptoGMPClient.uc

Lines changed: 85 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,23 @@ const ID_PRIME = "PRIME";
8686
const TID_PRIME = "TPRIME";
8787

8888
// Bitrate calculations.
89-
// First packet has been sent.
90-
var bool bTransfersStarted;
9189
var float StartTimeSeconds;
9290
var float StopTimeSeconds;
9391
var int BytesOut; // TODO: does this overflow?
9492
var int BytesIn; // TODO: does this overflow?
93+
var int LastBytesOut;
94+
var int LastBytesIn;
95+
var array<float> BytesOutSamples;
96+
var array<float> BytesInSamples;
97+
var float ByteRateOut;
98+
var float ByteRateIn;
99+
var int BytesOutSamplesIndex;
100+
var int BytesInSamplesIndex;
101+
const NUM_SAMPLES = 5;
102+
var float MaxByteRateOut;
103+
var float MaxByteRateIn;
104+
var float AvgByteRateOut;
105+
var float AvgByteRateIn;
95106

96107
var delegate<OnRandPrimeReceived> RandPrimeDelegate;
97108
delegate OnRandPrimeReceived(const out array<byte> P);
@@ -100,22 +111,87 @@ simulated event PreBeginPlay()
100111
{
101112
super.PreBeginPlay();
102113

114+
SampleTransferRates();
103115
SetTimer(2.0, True, NameOf(LogTransferRates));
104116
}
105117

106-
simulated final function LogTransferRates()
118+
simulated final function SampleTransferRates()
119+
{
120+
StartTimeSeconds = WorldInfo.RealTimeSeconds;
121+
SetTimer(0.2, False, NameOf(StopTransferRateSample));
122+
}
123+
124+
simulated final function StopTransferRateSample()
107125
{
108126
local float TimeSpent;
109-
local float ByteRateOut;
110-
local float ByteRateIn;
127+
local float CurrentByteRateOut;
128+
local float CurrentByteRateIn;
129+
local float CurrentBytesOut;
130+
local float CurrentBytesIn;
131+
local int i;
111132

112133
StopTimeSeconds = WorldInfo.RealTimeSeconds;
113134
TimeSpent = StopTimeSeconds - StartTimeSeconds;
114-
ByteRateOut = BytesOut / TimeSpent;
115-
ByteRateIn = BytesIn / TimeSpent;
116135

117-
`fclog("BytesOut :" @ ByteRateOut @ "B/s" @ ByteRateOut * BpsToMbps @ "Mb/s");
118-
`fclog("BytesIn :" @ ByteRateIn @ "B/s" @ ByteRateIn * BpsToMbps @ "Mb/s");
136+
CurrentBytesOut = BytesOut - LastBytesOut;
137+
CurrentBytesIn = BytesIn - LastBytesIn;
138+
139+
CurrentByteRateOut = CurrentBytesOut / TimeSpent;
140+
CurrentByteRateIn = CurrentBytesIn / TimeSpent;
141+
142+
// `fclog("CurrentByteRateOut=" $ CurrentByteRateOut);
143+
// `fclog("CurrentByteRateIn=" $ CurrentByteRateIn);
144+
145+
BytesOutSamplesIndex = (BytesOutSamplesIndex + 1) % NUM_SAMPLES;
146+
BytesInSamplesIndex = (BytesInSamplesIndex + 1) % NUM_SAMPLES;
147+
148+
BytesOutSamples[BytesOutSamplesIndex] = CurrentByteRateOut;
149+
BytesInSamples[BytesInSamplesIndex] = CurrentByteRateIn;
150+
151+
LastBytesOut = BytesOut;
152+
LastBytesIn = BytesIn;
153+
154+
for (i = 0; i < NUM_SAMPLES; ++i)
155+
{
156+
ByteRateOut += BytesOutSamples[i];
157+
ByteRateIn += BytesInSamples[i];
158+
}
159+
160+
ByteRateOut /= NUM_SAMPLES;
161+
ByteRateIn /= NUM_SAMPLES;
162+
163+
MaxByteRateOut = Max(MaxByteRateOut, ByteRateOut);
164+
MaxByteRateIn = Max(MaxByteRateIn, ByteRateIn);
165+
166+
if (ByteRateOut > 0)
167+
{
168+
AvgByteRateOut += ByteRateOut;
169+
AvgByteRateOut /= 2;
170+
}
171+
172+
if (ByteRateIn > 0)
173+
{
174+
AvgByteRateIn += ByteRateIn;
175+
AvgByteRateIn /= 2;
176+
}
177+
178+
SampleTransferRates();
179+
}
180+
181+
simulated final function LogTransferRates()
182+
{
183+
`fclog(
184+
"BytesOut :" @ ByteRateOut @ "B/s"
185+
@ ByteRateOut * BpsToMbps @ "Mb/s"
186+
@ AvgByteRateOut * BpsToMbps @ "(avg)"
187+
@ MaxByteRateOut * BpsToMbps @ "(max)"
188+
);
189+
`fclog(
190+
"BytesIn :" @ ByteRateIn @ "B/s"
191+
@ ByteRateIn * BpsToMbps @ "Mb/s"
192+
@ AvgByteRateIn * BpsToMbps @ "(avg)"
193+
@ MaxByteRateIn * BpsToMbps @ "(max)"
194+
);
119195
}
120196
121197
simulated event Tick(float DeltaTime)
@@ -317,12 +393,6 @@ final simulated function RandPrime(int Size)
317393
318394
final function SendTextEx(coerce string Str)
319395
{
320-
if (!bTransfersStarted)
321-
{
322-
bTransfersStarted = True;
323-
StartTimeSeconds = WorldInfo.RealTimeSeconds;
324-
}
325-
326396
// TODO: is this right? UScript strings are UTF-16 (UCS-2).
327397
BytesOut += Len(Str) * 2;
328398
@@ -386,6 +456,4 @@ DefaultProperties
386456
OutLineMode=LMODE_UNIX
387457

388458
TickGroup=TG_DuringAsyncWork
389-
390-
bTransfersStarted=False
391459
}

0 commit comments

Comments
 (0)