From 52fd155b5f7a093658b4c8fbee2c0a2a6bbfa459 Mon Sep 17 00:00:00 2001 From: ME1312 Date: Fri, 30 Jul 2021 21:42:05 -0400 Subject: [PATCH] Efficient stats for nerds --- .../SubServers/Bungee/Library/Metrics.java | 40 +++++++++------- .../SubServers/Sync/Library/Metrics.java | 36 ++++++++------ .../ME1312/SubServers/Velocity/ExProxy.java | 17 +------ .../SubServers/Velocity/Library/Metrics.java | 48 +++++++++++++++---- 4 files changed, 85 insertions(+), 56 deletions(-) diff --git a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Library/Metrics.java b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Library/Metrics.java index 1515ddcd..8122b675 100644 --- a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Library/Metrics.java +++ b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Library/Metrics.java @@ -1,6 +1,7 @@ package net.ME1312.SubServers.Bungee.Library; import net.ME1312.SubData.Server.DataServer; +import net.ME1312.SubServers.Bungee.BungeeCommon; import net.ME1312.SubServers.Bungee.SubAPI; import java.io.BufferedReader; @@ -24,10 +25,12 @@ import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Supplier; import java.util.logging.Level; +import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.zip.GZIPOutputStream; import javax.net.ssl.HttpsURLConnection; +import gnu.trove.map.hash.TIntObjectHashMap; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.plugin.Plugin; @@ -136,9 +139,10 @@ public class Metrics { private static final AdvancedPie PLAYER_VERSIONS; static { - final Map.Entry[] PROTOCOLS; + final int[] PROTOCOL_VERSIONS; + final String[] PROTOCOL_NAMES; { - TreeMap protocols = new TreeMap(Collections.reverseOrder()); + TIntObjectHashMap protocols = new TIntObjectHashMap(); try { for (Field f : ProtocolConstants.class.getDeclaredFields()) { int fm = f.getModifiers(); @@ -149,25 +153,29 @@ public class Metrics { } catch (Throwable e) { e.printStackTrace(); } - PROTOCOLS = protocols.entrySet().toArray(new Map.Entry[0]); + PROTOCOL_VERSIONS = protocols.keys(); + PROTOCOL_NAMES = new String[PROTOCOL_VERSIONS.length]; + + Arrays.sort(PROTOCOL_VERSIONS); + for (int i = 0; i < PROTOCOL_VERSIONS.length; ++i) { + PROTOCOL_NAMES[i] = protocols.get(PROTOCOL_VERSIONS[i]); + } } PLAYER_VERSIONS = new AdvancedPie("player_versions", () -> { - HashMap players = new HashMap(); + int[] players = new int[PROTOCOL_VERSIONS.length]; for (ProxiedPlayer player : ProxyServer.getInstance().getPlayers()) { - String name = null; - int protocol = player.getPendingConnection().getVersion(); - for (Map.Entry p : PROTOCOLS) { - if (protocol >= p.getKey()) { - name = p.getValue(); - break; - } - } - if (name != null) { - players.put(name, players.getOrDefault(name, 0) + 1); + int i = Arrays.binarySearch(PROTOCOL_VERSIONS, player.getPendingConnection().getVersion()); + if (i != -1) { + ++players[i]; } } - return players; + + HashMap map = new HashMap(); + for (int i = 0; i < PROTOCOL_NAMES.length; ++i) if (players[i] != 0) { + map.put(PROTOCOL_NAMES[i], players[i]); + } + return map; }); } @@ -191,7 +199,7 @@ public class Metrics { private void appendPlatformData(JsonObjectBuilder builder) { builder.appendField("playerAmount", plugin.getProxy().getOnlineCount()); - builder.appendField("managedServers", plugin.getProxy().getServers().size()); + builder.appendField("managedServers", ((BungeeCommon) plugin.getProxy()).getServersCopy().size()); builder.appendField("onlineMode", plugin.getProxy().getConfig().isOnlineMode() ? 1 : 0); builder.appendField("bungeecordVersion", plugin.getProxy().getVersion()); builder.appendField("javaVersion", System.getProperty("java.version")); diff --git a/SubServers.Sync/src/net/ME1312/SubServers/Sync/Library/Metrics.java b/SubServers.Sync/src/net/ME1312/SubServers/Sync/Library/Metrics.java index ebe83ddc..da084e27 100644 --- a/SubServers.Sync/src/net/ME1312/SubServers/Sync/Library/Metrics.java +++ b/SubServers.Sync/src/net/ME1312/SubServers/Sync/Library/Metrics.java @@ -2,6 +2,7 @@ package net.ME1312.SubServers.Sync.Library; import net.ME1312.SubServers.Sync.SubAPI; +import gnu.trove.map.hash.TIntObjectHashMap; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.plugin.Plugin; @@ -128,9 +129,10 @@ public class Metrics { private static final AdvancedPie PLAYER_VERSIONS; static { - final Map.Entry[] PROTOCOLS; + final int[] PROTOCOL_VERSIONS; + final String[] PROTOCOL_NAMES; { - TreeMap protocols = new TreeMap(Collections.reverseOrder()); + TIntObjectHashMap protocols = new TIntObjectHashMap(); try { for (Field f : ProtocolConstants.class.getDeclaredFields()) { int fm = f.getModifiers(); @@ -141,25 +143,29 @@ public class Metrics { } catch (Throwable e) { e.printStackTrace(); } - PROTOCOLS = protocols.entrySet().toArray(new Map.Entry[0]); + PROTOCOL_VERSIONS = protocols.keys(); + PROTOCOL_NAMES = new String[PROTOCOL_VERSIONS.length]; + + Arrays.sort(PROTOCOL_VERSIONS); + for (int i = 0; i < PROTOCOL_VERSIONS.length; ++i) { + PROTOCOL_NAMES[i] = protocols.get(PROTOCOL_VERSIONS[i]); + } } PLAYER_VERSIONS = new AdvancedPie("player_versions", () -> { - HashMap players = new HashMap(); + int[] players = new int[PROTOCOL_VERSIONS.length]; for (ProxiedPlayer player : ProxyServer.getInstance().getPlayers()) { - String name = null; - int protocol = player.getPendingConnection().getVersion(); - for (Map.Entry p : PROTOCOLS) { - if (protocol >= p.getKey()) { - name = p.getValue(); - break; - } - } - if (name != null) { - players.put(name, players.getOrDefault(name, 0) + 1); + int i = Arrays.binarySearch(PROTOCOL_VERSIONS, player.getPendingConnection().getVersion()); + if (i != -1) { + ++players[i]; } } - return players; + + HashMap map = new HashMap(); + for (int i = 0; i < PROTOCOL_NAMES.length; ++i) if (players[i] != 0) { + map.put(PROTOCOL_NAMES[i], players[i]); + } + return map; }); } diff --git a/SubServers.Sync/velocity/src/net/ME1312/SubServers/Velocity/ExProxy.java b/SubServers.Sync/velocity/src/net/ME1312/SubServers/Velocity/ExProxy.java index d10f529f..b9e6a490 100644 --- a/SubServers.Sync/velocity/src/net/ME1312/SubServers/Velocity/ExProxy.java +++ b/SubServers.Sync/velocity/src/net/ME1312/SubServers/Velocity/ExProxy.java @@ -20,7 +20,6 @@ import net.ME1312.SubServers.Velocity.Library.ConfigUpdater; import net.ME1312.SubServers.Velocity.Library.Fallback.FallbackState; import net.ME1312.SubServers.Velocity.Library.Fallback.SmartFallback; import net.ME1312.SubServers.Velocity.Library.Metrics; -import net.ME1312.SubServers.Velocity.Library.Metrics.AdvancedPie; import net.ME1312.SubServers.Velocity.Network.Packet.PacketExSyncPlayer; import net.ME1312.SubServers.Velocity.Network.SubProtocol; import net.ME1312.SubServers.Velocity.Server.CachedPlayer; @@ -57,7 +56,6 @@ import java.net.InetAddress; import java.net.URL; import java.nio.charset.Charset; import java.util.*; -import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; @Plugin(id = "subservers-sync", name = "SubServers-Sync", authors = "ME1312", version = "2.17.1a/pr1", url = "https://github.com/ME1312/SubServers-2", description = "Dynamically sync player and server connection info over multiple proxy instances") @@ -216,20 +214,7 @@ public class ExProxy { if (config.get().getMap("Settings").getMap("Smart-Fallback", new ObjectMap<>()).getBoolean("Enabled", true)) proxy.getEventManager().register(this, new SmartFallback(config.get().getMap("Settings").getMap("Smart-Fallback", new ObjectMap<>()))); - metrics.make(this, 11953).addCustomChart(new AdvancedPie("player_versions", new Callable>() { - @Override - public Map call() throws Exception { - HashMap players = new HashMap(); - for (Player player : proxy.getAllPlayers()) { - String name = player.getProtocolVersion().getVersionIntroducedIn(); - if (name != null) { - players.put(name, players.getOrDefault(name, 0) + 1); - } - } - return players; - } - })); - + metrics.make(this, 11953).appendAppData(); new Timer("SubServers.Sync::Routine_Update_Check").schedule(new TimerTask() { @SuppressWarnings("unchecked") @Override diff --git a/SubServers.Sync/velocity/src/net/ME1312/SubServers/Velocity/Library/Metrics.java b/SubServers.Sync/velocity/src/net/ME1312/SubServers/Velocity/Library/Metrics.java index 0516849e..51789b16 100644 --- a/SubServers.Sync/velocity/src/net/ME1312/SubServers/Velocity/Library/Metrics.java +++ b/SubServers.Sync/velocity/src/net/ME1312/SubServers/Velocity/Library/Metrics.java @@ -1,9 +1,14 @@ package net.ME1312.SubServers.Velocity.Library; +import net.ME1312.SubServers.Velocity.ExProxy; +import net.ME1312.SubServers.Velocity.SubAPI; + import com.google.inject.Inject; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.api.plugin.PluginContainer; import com.velocitypowered.api.plugin.PluginDescription; import com.velocitypowered.api.plugin.annotation.DataDirectory; +import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.ProxyServer; import java.io.BufferedReader; import java.io.BufferedWriter; @@ -17,15 +22,7 @@ import java.io.InputStreamReader; import java.net.URL; import java.nio.charset.StandardCharsets; import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; -import java.util.Set; -import java.util.UUID; +import java.util.*; import java.util.concurrent.Callable; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -137,6 +134,39 @@ public class Metrics { } } + private static final AdvancedPie PLAYER_VERSIONS; + static { + final ProtocolVersion[] PROTOCOLS = ProtocolVersion.SUPPORTED_VERSIONS.toArray(new ProtocolVersion[0]); + Arrays.sort(PROTOCOLS); + + PLAYER_VERSIONS = new AdvancedPie("player_versions", () -> { + int[] players = new int[PROTOCOLS.length]; + for (Player player : ExProxy.getInstance().getAllPlayers()) { + int i = Arrays.binarySearch(PROTOCOLS, player.getProtocolVersion()); + if (i != -1) { + ++players[i]; + } + } + + HashMap map = new HashMap(); + for (int i = 0; i < PROTOCOLS.length; ++i) if (players[i] != 0) { + map.put(PROTOCOLS[i].getVersionIntroducedIn(), players[i]); + } + return map; + }); + } + + public void appendAppData() { + addCustomChart(PLAYER_VERSIONS); + } + + public void appendPluginData() { + addCustomChart(new SimplePie("subservers_version", () -> { + return SubAPI.getInstance().getPluginVersion().toString(); + })); + addCustomChart(PLAYER_VERSIONS); + } + private void appendPlatformData(JsonObjectBuilder builder) { builder.appendField("playerAmount", server.getPlayerCount()); builder.appendField("managedServers", server.getAllServers().size());