From 65820040ce997a960f44f5167284f336f2bcc7e6 Mon Sep 17 00:00:00 2001 From: OLEGSHA Date: Sat, 2 Jan 2021 00:37:12 +0300 Subject: [PATCH] Added FPS and TPS display averaging --- .../progressia/test/LayerTestGUI.java | 66 ++++++++++++++++++- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/src/main/java/ru/windcorp/progressia/test/LayerTestGUI.java b/src/main/java/ru/windcorp/progressia/test/LayerTestGUI.java index 0395f35..1a513bd 100755 --- a/src/main/java/ru/windcorp/progressia/test/LayerTestGUI.java +++ b/src/main/java/ru/windcorp/progressia/test/LayerTestGUI.java @@ -30,6 +30,8 @@ import ru.windcorp.progressia.client.graphics.gui.Label; import ru.windcorp.progressia.client.graphics.gui.Panel; import ru.windcorp.progressia.client.graphics.gui.layout.LayoutAlign; import ru.windcorp.progressia.client.graphics.gui.layout.LayoutVertical; +import ru.windcorp.progressia.common.Units; +import ru.windcorp.progressia.server.Server; import ru.windcorp.progressia.server.ServerState; public class LayerTestGUI extends GUILayer { @@ -63,13 +65,13 @@ public class LayerTestGUI extends GUILayer { panel.addChild(new DynamicLabel( "FPSDisplay", new Font().withColor(0x37A3E6).deriveShadow(), - () -> String.format(Locale.US, "FPS: %5.1f", GraphicsInterface.getFPS()), + LayerTestGUI::getFPS, 128 )); panel.addChild(new DynamicLabel( "TPSDisplay", new Font().withColor(0x37A3E6).deriveShadow(), - () -> ServerState.getInstance() == null ? "TPS: n/a" : String.format(Locale.US, "TPS: %5.1f", ServerState.getInstance().getTPS()), + LayerTestGUI::getTPS, 128 )); @@ -89,6 +91,66 @@ public class LayerTestGUI extends GUILayer { getRoot().addChild(panel); } + private static class Averager { + + private static final int DISPLAY_INERTIA = 32; + private static final double UPDATE_INTERVAL = Units.get(50.0, "ms"); + + private final double[] values = new double[DISPLAY_INERTIA]; + private int size; + private int head; + + private long lastUpdate; + + public void add(double value) { + if (size == values.length) { + values[head] = value; + head++; + if (head == values.length) head = 0; + } else { + values[size] = value; + size++; + } + } + + public double average() { + double product = 1; + + if (size == values.length) { + for (double d : values) product *= d; + } else { + for (int i = 0; i < size; ++i) product *= values[i]; + } + + return Math.pow(product, 1.0 / size); + } + + public double update(double value) { + long now = (long) (GraphicsInterface.getTime() / UPDATE_INTERVAL); + if (lastUpdate != now) { + lastUpdate = now; + add(value); + } + + return average(); + } + + } + + private static final Averager FPS_RECORD = new Averager(); + private static final Averager TPS_RECORD = new Averager(); + + private static String getFPS() { + return String.format(Locale.US, "FPS: %5.1f", FPS_RECORD.update(GraphicsInterface.getFPS())); + } + + private static String getTPS() { + Server server = ServerState.getInstance(); + if (server == null) return "TPS: n/a"; + + return String.format(Locale.US, "TPS: %5.1f", TPS_RECORD.update(server.getTPS())); + } + // private static class DebugComponent extends Component { // private final int color; //