From fe123a5e92ad8528ba7093f834591f403525f6e9 Mon Sep 17 00:00:00 2001 From: William Blake Galbreath Date: Thu, 30 Jan 2020 00:41:24 -0600 Subject: [PATCH] Add tick times API --- .../net/pl3x/purpur/gui/info/RAMDetails.java | 13 +++---------- .../org/bukkit/craftbukkit/CraftServer.java | 15 +++++++++++++++ .../org/spigotmc/TicksPerSecondCommand.java | 18 +++++++++++++++++- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/pl3x/purpur/gui/info/RAMDetails.java b/src/main/java/net/pl3x/purpur/gui/info/RAMDetails.java index b1ea91b49b..2981afaaaa 100644 --- a/src/main/java/net/pl3x/purpur/gui/info/RAMDetails.java +++ b/src/main/java/net/pl3x/purpur/gui/info/RAMDetails.java @@ -2,6 +2,7 @@ package net.pl3x.purpur.gui.info; import net.minecraft.server.MinecraftServer; import net.minecraft.server.SystemUtils; +import org.bukkit.Bukkit; import javax.swing.DefaultListCellRenderer; import javax.swing.JList; @@ -13,7 +14,7 @@ import java.util.Locale; import java.util.Vector; public class RAMDetails extends JList { - private static final DecimalFormat DECIMAL_FORMAT = SystemUtils.a(new DecimalFormat("########0.000"), (format) + public static final DecimalFormat DECIMAL_FORMAT = SystemUtils.a(new DecimalFormat("########0.000"), (format) -> format.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(Locale.ROOT))); private final MinecraftServer server; @@ -38,15 +39,7 @@ public class RAMDetails extends JList { Vector vector = new Vector<>(); vector.add("Memory use: " + (graph.usedMem / 1024L / 1024L) + " mb (" + (graph.free * 100L / graph.max) + "% free)"); vector.add("Heap: " + (graph.total / 1024L / 1024L) + " / " + (graph.max / 1024L / 1024L) + " mb"); - vector.add("Avg tick: " + DECIMAL_FORMAT.format(getAverage(server.getTickTimes()) * 1.0E-6D) + " ms"); + vector.add("Avg tick: " + DECIMAL_FORMAT.format(Bukkit.getAverageTickTime()) + " ms"); setListData(vector); } - - private double getAverage(long[] values) { - long total = 0L; - for (long value : values) { - total += value; - } - return (double) total / (double) values.length; - } } diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java index 262b82538c..2b78709262 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java @@ -2185,5 +2185,20 @@ public final class CraftServer implements Server { public boolean isLagging() { return getServer().lagging; } + + @Override + public long[] getTickTimes() { + return getServer().getTickTimes(); + } + + @Override + public double getAverageTickTime() { + long total = 0L; + long[] tickTimes = getTickTimes(); + for (long value : tickTimes) { + total += value; + } + return ((double) total / (double) tickTimes.length) * 1.0E-6D; + } // Purpur end } diff --git a/src/main/java/org/spigotmc/TicksPerSecondCommand.java b/src/main/java/org/spigotmc/TicksPerSecondCommand.java index 2eed9d0a4a..c29380e1c0 100644 --- a/src/main/java/org/spigotmc/TicksPerSecondCommand.java +++ b/src/main/java/org/spigotmc/TicksPerSecondCommand.java @@ -4,6 +4,12 @@ import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; +// Purpur start +import net.pl3x.purpur.gui.info.RAMDetails; +import org.apache.commons.lang.StringUtils; +import org.bukkit.Bukkit; +// Purpur end + public class TicksPerSecondCommand extends Command { @@ -30,12 +36,22 @@ public class TicksPerSecondCommand extends Command for ( int i = 0; i < tps.length; i++) { tpsAvg[i] = format( tps[i] ); } - sender.sendMessage( ChatColor.GOLD + "TPS from last 5s, 1m, 5m, 15m: " + org.apache.commons.lang.StringUtils.join(tpsAvg, ", ")); // Purpur + // Purpur start + double avg = Bukkit.getAverageTickTime(); + sender.sendMessage( ChatColor.GOLD + "TPS from last 5s, 1m, 5m, 15m: " + StringUtils.join(tpsAvg, ", ")); + sender.sendMessage( ChatColor.GOLD + "Average tick time: " + getColor(avg) + RAMDetails.DECIMAL_FORMAT.format(avg) + " ms"); + // Purpur end // Paper end return true; } + // Purpur start + private static String getColor(double avg) { + return (avg >= 50 ? ChatColor.RED : avg >= 40 ? ChatColor.YELLOW : ChatColor.GREEN).toString(); + } + // Purpur end + private static String format(double tps) // Paper - Made static { return ( ( tps > 18.0 ) ? ChatColor.GREEN : ( tps > 16.0 ) ? ChatColor.YELLOW : ChatColor.RED ).toString() -- 2.24.0