отдельный класс для слежки за tps
This commit is contained in:
@@ -12,15 +12,9 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class GameLoop extends Thread {
|
public class GameLoop extends Thread {
|
||||||
|
private final TpsWatcher TPS_WATCHER = TpsWatcher.getInstance();
|
||||||
@Autowired
|
@Autowired
|
||||||
PlayerManager playerManager;
|
private PlayerManager playerManager;
|
||||||
|
|
||||||
/* TPS */
|
|
||||||
private int tps;
|
|
||||||
private long pause;
|
|
||||||
@Setter
|
|
||||||
private boolean traceTPS = false;
|
|
||||||
private int lowTps;
|
|
||||||
|
|
||||||
/* Time */
|
/* Time */
|
||||||
@Setter
|
@Setter
|
||||||
@@ -33,41 +27,23 @@ public class GameLoop extends Thread {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setPercentWarnLowTps(int value) {
|
public void setPercentWarnLowTps(int value) {
|
||||||
if (value > 100) {
|
TPS_WATCHER.setPercentWarnLowTps(value);
|
||||||
log.warn("Percent warn low TPS can't be '{}'. Set 100", tps);
|
|
||||||
value = 100;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.lowTps = tps - (int)(tps * (value / 100f));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTps(int tps) {
|
public void setTps(int tps) {
|
||||||
if (tps > 1000) {
|
TPS_WATCHER.setTps(tps);
|
||||||
log.warn("TPS can't be '{}'. Set 1000", tps);
|
}
|
||||||
tps = 1000;
|
|
||||||
}
|
public void setTps(boolean value) {
|
||||||
this.tps = tps;
|
TPS_WATCHER.setTraceTPS(value);
|
||||||
this.pause = (1000 / tps);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
log.info("Target TPS: {}; Low TPS: {}", tps, lowTps);
|
TPS_WATCHER.startWatch();
|
||||||
int factTps = 0;
|
|
||||||
long lastTime = System.currentTimeMillis();
|
|
||||||
|
|
||||||
while (!isInterrupted()) {
|
while (!isInterrupted()) {
|
||||||
if ((System.currentTimeMillis() - lastTime) > 1000) {
|
TPS_WATCHER.check();
|
||||||
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;
|
|
||||||
|
|
||||||
/* --- --- --- */
|
/* --- --- --- */
|
||||||
|
|
||||||
@@ -76,12 +52,7 @@ public class GameLoop extends Thread {
|
|||||||
|
|
||||||
/* --- --- --- */
|
/* --- --- --- */
|
||||||
|
|
||||||
factTps++;
|
TPS_WATCHER.tick();
|
||||||
try {
|
|
||||||
long pause = futureTime - System.currentTimeMillis();
|
|
||||||
Thread.sleep((pause <= 0 ? 0 : pause));
|
|
||||||
} catch (InterruptedException ignored) {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
82
core/src/main/java/mc/core/TpsWatcher.java
Normal file
82
core/src/main/java/mc/core/TpsWatcher.java
Normal 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) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user