From 697b1733591d9ba6681aa8a09ec7c77dc421c886 Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Thu, 2 Jul 2020 11:26:38 +0200 Subject: [PATCH] Refactoring --- .../services/profiler/PerformanceRating.java | 5 ++++ .../services/profiler/PerformanceSummary.java | 30 +++++++++++-------- .../services/profiler/SlimefunProfiler.java | 16 +++++++--- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/PerformanceRating.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/PerformanceRating.java index 0ebfbc0fe..1b88383f4 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/PerformanceRating.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/PerformanceRating.java @@ -36,6 +36,11 @@ public enum PerformanceRating implements Predicate { @Override public boolean test(Float value) { + if (value == null) { + // null will only test true for UNKNOWN + return threshold < 0; + } + return value <= threshold; } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/PerformanceSummary.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/PerformanceSummary.java index b87d04973..4e8b72650 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/PerformanceSummary.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/PerformanceSummary.java @@ -31,6 +31,7 @@ class PerformanceSummary { private final PerformanceRating rating; private final long totalElapsedTime; private final int totalTickedBlocks; + private final float percentage; private final Map chunks; private final Map items; @@ -38,6 +39,7 @@ class PerformanceSummary { PerformanceSummary(SlimefunProfiler profiler, long totalElapsedTime, int totalTickedBlocks) { this.profiler = profiler; this.rating = profiler.getPerformance(); + this.percentage = profiler.getPercentageOfTick(); this.totalElapsedTime = totalElapsedTime; this.totalTickedBlocks = totalTickedBlocks; @@ -48,13 +50,13 @@ class PerformanceSummary { public void send(CommandSender sender) { sender.sendMessage(""); sender.sendMessage(ChatColor.GREEN + "===== Slimefun Lag Profiler ====="); - sender.sendMessage(ChatColors.color("&6Total: &e" + NumberUtils.getAsMillis(totalElapsedTime))); - sender.sendMessage(ChatColors.color("&6Performance: " + getPerformanceRating())); - sender.sendMessage(ChatColors.color("&6Active Chunks: &e" + chunks.size())); - sender.sendMessage(ChatColors.color("&6Active Blocks: &e" + totalTickedBlocks)); + sender.sendMessage(ChatColor.GOLD + "Total: " + ChatColor.YELLOW + NumberUtils.getAsMillis(totalElapsedTime)); + sender.sendMessage(ChatColor.GOLD + "Performance: " + getPerformanceRating()); + sender.sendMessage(ChatColor.GOLD + "Active Chunks: " + ChatColor.YELLOW + chunks.size()); + sender.sendMessage(ChatColor.GOLD + "Active Blocks: " + ChatColor.YELLOW + totalTickedBlocks); sender.sendMessage(""); - summarizeTimings("Chunks", sender, entry -> { + summarizeTimings("Blocks", sender, entry -> { int count = profiler.getBlocksOfId(entry.getKey()); String time = NumberUtils.getAsMillis(entry.getValue()); @@ -70,7 +72,7 @@ class PerformanceSummary { sender.sendMessage(""); - summarizeTimings("Blocks", sender, entry -> { + summarizeTimings("Chunks", sender, entry -> { int count = profiler.getBlocksInChunk(entry.getKey()); String time = NumberUtils.getAsMillis(entry.getValue()); @@ -93,7 +95,7 @@ class PerformanceSummary { for (Map.Entry entry : results) { if (entry.getValue() > VISIBILITY_THRESHOLD) { - builder.append("\n&e").append(formatter.apply(entry)); + builder.append("\n").append(ChatColor.YELLOW).append(formatter.apply(entry)); } else { hidden++; @@ -108,26 +110,28 @@ class PerformanceSummary { } else { int hidden = 0; - - sender.sendMessage(ChatColor.GOLD + prefix); + StringBuilder builder = new StringBuilder(); + builder.append(ChatColor.GOLD + prefix); for (Map.Entry entry : results) { if (entry.getValue() > VISIBILITY_THRESHOLD) { - sender.sendMessage(" " + ChatColor.stripColor(formatter.apply(entry))); + builder.append(" "); + builder.append(ChatColor.stripColor(formatter.apply(entry))); } else { hidden++; } } - sender.sendMessage("+ " + hidden + " more"); + builder.append("\n+ "); + builder.append(hidden); + builder.append(" more..."); + sender.sendMessage(builder.toString()); } } private String getPerformanceRating() { StringBuilder builder = new StringBuilder(); - - float percentage = Math.round(((((totalElapsedTime / 1000000.0) * 100.0F) / MAX_TICK_DURATION) * 100.0F) / 100.0F); builder.append(NumberUtils.getColorFromPercentage(100 - Math.min(percentage, 100))); int rest = 20; diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/SlimefunProfiler.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/SlimefunProfiler.java index 614885e65..084e7a99f 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/SlimefunProfiler.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/profiler/SlimefunProfiler.java @@ -28,11 +28,11 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; import me.mrCookieSlime.Slimefun.api.Slimefun; /** - * The {@link SlimefunProfiler} works closely to the {@link TickerTask} and is responsible for - * monitoring that task. + * The {@link SlimefunProfiler} works closely to the {@link TickerTask} and is + * responsible for monitoring that task. * It collects timings data for any ticked {@link Block} and the corresponding {@link SlimefunItem}. * This allows developers to identify laggy {@link SlimefunItem SlimefunItems} or {@link SlimefunAddon SlimefunAddons}. - * But it also enabled Server Admins to locate lag-inducing areas on the {@link Server}. + * But it also enables Server Admins to locate lag-inducing areas on the {@link Server}. * * @author TheBusyBiscuit * @@ -151,6 +151,8 @@ public class SlimefunProfiler { * The {@link CommandSender} who shall receive this summary. */ public void requestSummary(CommandSender sender) { + Validate.notNull(sender, "Cannot request a summary for null"); + requests.add(sender); } @@ -206,13 +208,19 @@ public class SlimefunProfiler { return blocks; } + protected float getPercentageOfTick() { + float millis = totalElapsedTime / 1000000.0F; + float fraction = (millis * 100.0F) / PerformanceSummary.MAX_TICK_DURATION; + return Math.round((fraction * 100.0F) / 100.0F); + } + /** * This method returns the current {@link PerformanceRating}. * * @return The current performance grade */ public PerformanceRating getPerformance() { - float percentage = Math.round(((((totalElapsedTime / 1000000.0) * 100.0F) / PerformanceSummary.MAX_TICK_DURATION) * 100.0F) / 100.0F); + float percentage = getPercentageOfTick(); for (PerformanceRating rating : PerformanceRating.values()) { if (rating.test(percentage)) {