diff options
author | Luck <git@lucko.me> | 2018-05-31 16:47:22 +0100 |
---|---|---|
committer | Luck <git@lucko.me> | 2018-05-31 16:47:22 +0100 |
commit | cbd5325a0837f3ee57682ada657d4cd7c3e6ee37 (patch) | |
tree | 689c6eb3f4dd9d965c5a87ec0be319497554639e /bukkit/src | |
parent | 8b5071cdc5bd2609ee4f3805e3a54ced7f3d4df8 (diff) | |
download | spark-cbd5325a0837f3ee57682ada657d4cd7c3e6ee37.tar.gz spark-cbd5325a0837f3ee57682ada657d4cd7c3e6ee37.tar.bz2 spark-cbd5325a0837f3ee57682ada657d4cd7c3e6ee37.zip |
Add '/profiler monitoring' command to monitor tick times, and '--only-ticks-over' argument to filter profiling output to ticks lasting over a certain duration
Diffstat (limited to 'bukkit/src')
-rw-r--r-- | bukkit/src/main/java/me/lucko/spark/bukkit/BukkitTickCounter.java | 55 | ||||
-rw-r--r-- | bukkit/src/main/java/me/lucko/spark/bukkit/SparkBukkitPlugin.java | 34 |
2 files changed, 86 insertions, 3 deletions
diff --git a/bukkit/src/main/java/me/lucko/spark/bukkit/BukkitTickCounter.java b/bukkit/src/main/java/me/lucko/spark/bukkit/BukkitTickCounter.java new file mode 100644 index 0000000..61a7690 --- /dev/null +++ b/bukkit/src/main/java/me/lucko/spark/bukkit/BukkitTickCounter.java @@ -0,0 +1,55 @@ +package me.lucko.spark.bukkit; + +import me.lucko.spark.profiler.TickCounter; + +import org.bukkit.plugin.Plugin; +import org.bukkit.scheduler.BukkitTask; + +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.atomic.LongAdder; + +public class BukkitTickCounter implements TickCounter, Runnable { + private final Plugin plugin; + private BukkitTask task; + + private final Set<Runnable> tasks = new HashSet<>(); + private final LongAdder tick = new LongAdder(); + + public BukkitTickCounter(Plugin plugin) { + this.plugin = plugin; + } + + @Override + public void run() { + this.tick.increment(); + for (Runnable r : this.tasks){ + r.run(); + } + } + + @Override + public void start() { + this.task = this.plugin.getServer().getScheduler().runTaskTimer(this.plugin, this, 1, 1); + } + + @Override + public void close() { + this.task.cancel(); + } + + @Override + public long getCurrentTick() { + return this.tick.longValue(); + } + + @Override + public void addTickTask(Runnable runnable) { + this.tasks.add(runnable); + } + + @Override + public void removeTickTask(Runnable runnable) { + this.tasks.remove(runnable); + } +} diff --git a/bukkit/src/main/java/me/lucko/spark/bukkit/SparkBukkitPlugin.java b/bukkit/src/main/java/me/lucko/spark/bukkit/SparkBukkitPlugin.java index 8b3ed28..dc432c5 100644 --- a/bukkit/src/main/java/me/lucko/spark/bukkit/SparkBukkitPlugin.java +++ b/bukkit/src/main/java/me/lucko/spark/bukkit/SparkBukkitPlugin.java @@ -2,23 +2,46 @@ package me.lucko.spark.bukkit; import me.lucko.spark.common.CommandHandler; import me.lucko.spark.profiler.ThreadDumper; +import me.lucko.spark.profiler.TickCounter; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; public class SparkBukkitPlugin extends JavaPlugin { private final CommandHandler<CommandSender> commandHandler = new CommandHandler<CommandSender>() { + + private String colorize(String message) { + return ChatColor.translateAlternateColorCodes('&', message); + } + + private void broadcast(String msg) { + getServer().getConsoleSender().sendMessage(msg); + for (Player player : getServer().getOnlinePlayers()) { + if (player.hasPermission("spark.profiler")) { + player.sendMessage(msg); + } + } + } + @Override protected void sendMessage(CommandSender sender, String message) { - sender.sendMessage(ChatColor.translateAlternateColorCodes('&', message)); + sender.sendMessage(colorize(message)); } @Override - protected void sendLink(CommandSender sender, String url) { - sendMessage(sender, "&7" + url); + protected void sendMessage(String message) { + String msg = colorize(message); + broadcast(msg); + } + + @Override + protected void sendLink(String url) { + String msg = colorize("&7" + url); + broadcast(msg); } @Override @@ -30,6 +53,11 @@ public class SparkBukkitPlugin extends JavaPlugin { protected ThreadDumper getDefaultThreadDumper() { return new ThreadDumper.Specific(new long[]{Thread.currentThread().getId()}); } + + @Override + protected TickCounter newTickCounter() { + return new BukkitTickCounter(SparkBukkitPlugin.this); + } }; @Override |