Archived
0

отдельный класс для слежки за tps

This commit is contained in:
2018-06-12 18:00:02 +03:00
parent f5690ba0bb
commit 5cc4a8d1a4
2 changed files with 93 additions and 40 deletions

View File

@@ -12,15 +12,9 @@ import org.springframework.beans.factory.annotation.Autowired;
@Slf4j
public class GameLoop extends Thread {
private final TpsWatcher TPS_WATCHER = TpsWatcher.getInstance();
@Autowired
PlayerManager playerManager;
/* TPS */
private int tps;
private long pause;
@Setter
private boolean traceTPS = false;
private int lowTps;
private PlayerManager playerManager;
/* Time */
@Setter
@@ -33,41 +27,23 @@ public class GameLoop extends Thread {
}
public void setPercentWarnLowTps(int value) {
if (value > 100) {
log.warn("Percent warn low TPS can't be '{}'. Set 100", tps);
value = 100;
}
this.lowTps = tps - (int)(tps * (value / 100f));
TPS_WATCHER.setPercentWarnLowTps(value);
}
public void setTps(int tps) {
if (tps > 1000) {
log.warn("TPS can't be '{}'. Set 1000", tps);
tps = 1000;
}
this.tps = tps;
this.pause = (1000 / tps);
TPS_WATCHER.setTps(tps);
}
public void setTps(boolean value) {
TPS_WATCHER.setTraceTPS(value);
}
@Override
public void run() {
log.info("Target TPS: {}; Low TPS: {}", tps, lowTps);
int factTps = 0;
long lastTime = System.currentTimeMillis();
TPS_WATCHER.startWatch();
while (!isInterrupted()) {
if ((System.currentTimeMillis() - lastTime) > 1000) {
lastTime = System.currentTimeMillis();
if (factTps < lowTps) {
log.warn("Low TPS: {}/{}", factTps, tps);
} else if (traceTPS) {
log.info("TPS: {}/{}", factTps, tps);
}
factTps = 0;
}
long futureTime = System.currentTimeMillis() + pause;
TPS_WATCHER.check();
/* --- --- --- */
@@ -76,12 +52,7 @@ public class GameLoop extends Thread {
/* --- --- --- */
factTps++;
try {
long pause = futureTime - System.currentTimeMillis();
Thread.sleep((pause <= 0 ? 0 : pause));
} catch (InterruptedException ignored) {
}
TPS_WATCHER.tick();
}
}
}

View File

@@ -0,0 +1,82 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-06-12
*/
package mc.core;
import lombok.extern.slf4j.Slf4j;
@Slf4j
class TpsWatcher {
private static final TpsWatcher instance = new TpsWatcher();
private boolean traceTps = false;
private int tps;
private long pause;
private int lowTps;
private int percentLowTps;
private int factTps;
private long lastTime;
private long futureTime;
public static TpsWatcher getInstance() {
return instance;
}
private TpsWatcher(){ }
public void setTps(int value) {
if (value > 1000) {
log.warn("TPS can't be '{}'. Set 1000", value);
value = 1000;
}
tps = value;
pause = (1000 / value);
}
public void setPercentWarnLowTps(int value) {
if (value > 100) {
log.warn("Percent warn low TPS can't be '{}'. Set 100", value);
value = 100;
}
lowTps = tps - (int)(tps * (value / 100f));
percentLowTps = value;
}
public void setTraceTPS(boolean value) {
traceTps = value;
}
public void startWatch() {
log.info("Target TPS: {}; Low TPS: {}({}%)", tps, lowTps, percentLowTps);
factTps = 0;
lastTime = System.currentTimeMillis();
}
public void check() {
if ((System.currentTimeMillis() - lastTime) > 1000) {
lastTime = System.currentTimeMillis();
if (factTps < lowTps) {
log.warn("Low TPS: {}/{}", factTps, tps);
} else if (traceTps) {
log.trace("TPS: {}/{}", factTps, tps);
}
factTps = 0;
}
futureTime = System.currentTimeMillis() + pause;
}
public void tick() {
factTps++;
try {
long pause = futureTime - System.currentTimeMillis();
Thread.sleep((pause <= 0 ? 0 : pause));
} catch (InterruptedException ignored) {
}
}
}