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 7 commits into
base: fabric
Choose a base branch
from
Open

Add the ctick command #712

wants to merge 7 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;
}
}


@Override
public boolean condition() {
return tickCount <= 1200;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wasn't a problem in 1.12 since the client tick rate was always expected to be 20 and hardly ever fell below 20, but now if the TPS is set to 1 then this could take a long time to complete. I would suggest using real time instead. Just be extra careful about there being zero ticks elapsed and dividing by zero.

src/main/resources/assets/clientcommands/lang/en_us.json Outdated Show resolved Hide resolved
currentMeasurer = measurer;

float tps = source.getWorld().tickRateManager().tickrate();
source.sendFeedback(Component.translatable("commands.ctick.client.tps.expectedTps", tps));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These expected TPS and MSPT messages shouldn't be printed while the game is sprinting

stopPreviousTask();

boolean isIntegratedServer = source.getClient().hasSingleplayerServer();
TickMeasuringTask measurer = new TickMeasuringTask(false, !isIntegratedServer);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not seeing where there is any accurate measurements from the integrated server. Be careful about thread safety with that, though.

long totalTime = lastTickStart - firstTickStart;
double mspt = totalTime / (1_000_000D * tickCount);
minecraft.player.displayClientMessage(Component.translatable("commands.ctick.mspt", DEC_FMT.format(mspt)), false);
minecraft.player.displayClientMessage(Component.translatable("commands.ctick.mspt.inaccurate"), false);
Copy link
Owner

@Earthcomputer Earthcomputer Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message shouldn't display if the server is clearly lagging (e.g. the observed TPS is say 10% lower than expected). It also shouldn't display if the game is sprinting. Also for cosmetic reasons maybe it could be replaced with one of those help comments (ClientCommandHelper.sendHelp)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was copied over from your original code which I wanted to stay as close to as possible.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The concept of help messages didn't exist back then, and besides we can improve on code I wrote 7 years ago :)

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.

2 participants