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

Add the ctick command #712

Open
wants to merge 2 commits into
base: fabric
Choose a base branch
from
Open

Add the ctick command #712

wants to merge 2 commits into from

Conversation

xpple
Copy link
Collaborator

@xpple xpple commented Feb 4, 2025

This PR adds a simple ctick command for querying both the client's and server's TPS and MSPT. The expected value is printed for completeness. The task code is mostly copied from:

private static class TickMeasuringTask extends LongTask {
private static final int PERIOD = 100;
private int tickCount = 0;
private long totalTickTime = 0;
private long startTickTime;
private boolean hadFirstTick = false;
private long firstTickStart;
private long lastTickStart;
private ICommandSender sender;
private boolean tps;
private boolean forceInaccurate;
public TickMeasuringTask(ICommandSender sender, boolean tps, boolean forceInaccurate) {
this.sender = sender;
this.tps = tps;
this.forceInaccurate = forceInaccurate;
}
public void incrTickCount(int count) {
if (!hadFirstTick) {
firstTickStart = System.nanoTime();
hadFirstTick = true;
} else {
tickCount += count;
}
}
public void startTick() {
startTickTime = System.nanoTime();
if (!hadFirstTick) {
firstTickStart = startTickTime;
hadFirstTick = true;
}
}
public void endTick() {
if (hadFirstTick) {
totalTickTime += System.nanoTime() - startTickTime;
tickCount++;
}
}
@Override
protected void taskTick() {
if (tickCount >= PERIOD) {
lastTickStart = System.nanoTime();
setFinished();
}
}
@Override
public void start() {
sender.sendMessage(new TextComponentTranslation("commands.ctick.measuring"));
}
@Override
public void cleanup() {
if (tps) {
long totalTime = lastTickStart - firstTickStart;
double tps = 1000000000D * tickCount / totalTime;
sender.sendMessage(new TextComponentTranslation("commands.ctick.tps",
totalTime == 0 ? "Immeasurable" : DEC_FMT.format(tps)));
} else if (forceInaccurate) {
long totalTime = lastTickStart - firstTickStart;
double mspt = totalTime / (1000000D * tickCount);
sender.sendMessage(new TextComponentTranslation("commands.ctick.mspt", DEC_FMT.format(mspt)));
sender.sendMessage(new TextComponentTranslation("commands.ctick.mspt.inaccurate"));
} else {
double mspt = totalTickTime / (1000000D * tickCount);
sender.sendMessage(new TextComponentTranslation("commands.ctick.mspt", DEC_FMT.format(mspt)));
}
}
@Override
public int getTimeout() {
return 1200;
}
}

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

Successfully merging this pull request may close these issues.

1 participant