ServerAPI/src/main/java/xyz/etztech/serverapi/tps/TPS.java

31 lines
784 B
Java

package xyz.etztech.serverapi.tps;
import java.util.LinkedList;
public class TPS implements Runnable {
private transient long lastPoll = System.nanoTime();
private final LinkedList<Float> history = new LinkedList<>();
@Override
public void run() {
final long startTime = System.nanoTime();
long timeSpent = (startTime - lastPoll) / 1000;
if (timeSpent == 0) {
timeSpent = 1;
}
if (history.size() > 10) {
history.remove();
}
long tickInterval = 50;
float tps = tickInterval * 1000000.0f / timeSpent;
if (tps <= 21) {
history.add(tps);
}
lastPoll = startTime;
}
public LinkedList<Float> getHistory() {
return history;
}
}