Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong algorithm for calculating server TPS #2809

Open
DGCK81LNN opened this issue Oct 19, 2024 · 0 comments
Open

Wrong algorithm for calculating server TPS #2809

DGCK81LNN opened this issue Oct 19, 2024 · 0 comments

Comments

@DGCK81LNN
Copy link

DGCK81LNN commented Oct 19, 2024

/// <summary>
/// Called when server sent a Time Update packet.
/// </summary>
/// <param name="WorldAge"></param>
/// <param name="TimeOfDay"></param>
public void OnTimeUpdate(long WorldAge, long TimeOfDay)
{
// TimeUpdate sent every server tick hence used as timeout detect
UpdateKeepAlive();
// calculate server tps
if (lastAge != 0)
{
DateTime currentTime = DateTime.Now;
long tickDiff = WorldAge - lastAge;
Double tps = tickDiff / (currentTime - lastTime).TotalSeconds;
lastAge = WorldAge;
lastTime = currentTime;
if (tps <= 20 && tps > 0)
{
// calculate average tps
if (tpsSamples.Count >= maxSamples)
{
// full
sampleSum -= tpsSamples[0];
tpsSamples.RemoveAt(0);
}
tpsSamples.Add(tps);
sampleSum += tps;
averageTPS = sampleSum / tpsSamples.Count;
serverTPS = tps;
DispatchBotEvent(bot => bot.OnServerTpsUpdate(tps));
}
}
else
{
lastAge = WorldAge;
lastTime = DateTime.Now;
}
DispatchBotEvent(bot => bot.OnTimeUpdate(WorldAge, TimeOfDay));
}

The timing of time update packets is not perfect, so although the server runs at 20.0 TPS, the quotient of ticks to elapsed seconds between time updates floats around 20.0, sometimes higher than that and sometimes lower. If you entirely exclude "instantaneous" TPS samples above 20.0, the calculated average TPS is virtually always less than 20.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant