Permalink
Browse files

added timings command

  • Loading branch information...
1 parent c80b2d2 commit 06d60be519f7be15e7e331cf19eee1ec52885fac @tahg committed Jan 9, 2012
@@ -54,6 +54,7 @@ private void setDefaultCommands(final Server server) {
register("bukkit", new VersionCommand("version"));
register("bukkit", new ReloadCommand("reload"));
register("bukkit", new PluginsCommand("plugins"));
+ register("bukkit", new TimingsCommand("timings"));
}
/**
@@ -96,7 +97,7 @@ public boolean register(String label, String fallbackPrefix, Command command) {
/**
* Registers a command with the given name is possible, otherwise uses fallbackPrefix to create a unique name if its not an alias
*
- * @param name the name of the command, without the '/'-prefix.
+ * @param label the name of the command, without the '/'-prefix.
* @param fallbackPrefix a prefix which is prepended to the command with a ':' one or more times to make the command unique
* @param command the command to register
* @return true if command was registered with the passed in label, false otherwise.
@@ -0,0 +1,86 @@
+package org.bukkit.command.defaults;
+
+import org.bukkit.Bukkit;
+import org.bukkit.ChatColor;
+import org.bukkit.command.Command;
+import org.bukkit.command.CommandSender;
+import org.bukkit.event.Event;
+import org.bukkit.event.entity.FoodLevelChangeEvent;
+import org.bukkit.map.MapCursor;
+import org.bukkit.plugin.Plugin;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.PrintStream;
+
+public class TimingsCommand extends Command {
+ public TimingsCommand(String name) {
+ super(name);
+ this.description = "Records timings for all plugin events";
+ this.usageMessage = "/timings <function>";
+ this.setPermission("bukkit.command.timings");
+ }
+
+ @Override
+ public boolean execute(CommandSender sender, String currentAlias, String[] args) {
+ if (!testPermission(sender)) return true;
+ if (args.length != 1) return false;
+
+ if ("flush".equals(args[0])) {
+ for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
+ plugin.flushTimings();
+ }
+ }
+ else if("merged".equals(args[0])) {
+ int index = 0;
+ File timingFolder = new File("timings");
+ timingFolder.mkdirs();
+ File timings = new File(timingFolder, "timings.txt");
+ while (timings.exists()) timings = new File(timingFolder, "timinigs" + (++index) + ".txt");
+ try {
+ PrintStream printer = new PrintStream(timings);
+ for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
+ printer.println(plugin.getDescription().getFullName());
+ for (Event.Type type : Event.Type.values()) {
+ long time = plugin.getTiming(type);
+ if (time > 0) {
+ printer.println(" " + type.name() + " " + time);
+ }
+ }
+ }
+ sender.sendMessage("Timings written to " + timings.getPath());
+ } catch (IOException e) {
+ }
+ }
+ else if("seperate".equals(args[0])) {
+ int index = 0;
+ File timingFolder = new File("timings");
+ timingFolder.mkdirs();
+ File timings = new File(timingFolder, "timings.txt");
+ while (timings.exists()) timings = new File(timingFolder, "timinigs" + (++index) + ".txt");
+ File names = new File(timingFolder, "names" + index + ".txt");
+ int pluginIdx = 0;
+ try {
+ PrintStream fileTimings = new PrintStream(timings);
+ PrintStream fileNames = new PrintStream(names);
+ for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
+ pluginIdx++;
+ fileNames.println(pluginIdx + " " + plugin.getDescription().getFullName());
+ fileTimings.println("Plugin " + pluginIdx);
+ for (Event.Type type : Event.Type.values()) {
+ long time = plugin.getTiming(type);
+ if (time > 0) {
+ fileTimings.println(" " + type.name() + " " + time);
+ }
+ }
+ }
+ sender.sendMessage("Timings written to " + timings.getPath());
+ sender.sendMessage("Names written to " + names.getPath());
+ } catch (IOException e) {
+ }
+ }
+
+ return true;
+ }
+}
@@ -6,6 +6,7 @@
import org.bukkit.Server;
import org.bukkit.command.CommandExecutor;
import org.bukkit.configuration.file.FileConfiguration;
+import org.bukkit.event.Event;
import org.bukkit.generator.ChunkGenerator;
import org.bukkit.util.config.Configuration;
@@ -147,4 +148,10 @@
* @return ChunkGenerator for use in the default world generation
*/
public ChunkGenerator getDefaultWorldGenerator(String worldName, String id);
+
+ public long getTiming(Event.Type type);
+
+ public void incTiming(Event.Type type, long delta);
+
+ public void flushTimings();
}
@@ -336,7 +336,9 @@ public synchronized void callEvent(Event event) {
if (eventListeners != null) {
for (RegisteredListener registration : eventListeners) {
try {
+ long start = System.nanoTime();
registration.callEvent(event);
+ registration.getPlugin().incTiming(event.getType(), System.nanoTime() - start);
} catch (AuthorNagException ex) {
Plugin plugin = registration.getPlugin();
@@ -20,6 +20,7 @@
import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
+import org.bukkit.event.Event;
import org.bukkit.generator.ChunkGenerator;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
@@ -43,6 +44,7 @@
private EbeanServer ebean = null;
private FileConfiguration newConfig = null;
private File configFile = null;
+ private long[] timings = new long[Event.Type.values().length];
public JavaPlugin() {}
@@ -367,4 +369,16 @@ protected void removeDDL() {
public String toString() {
return getDescription().getFullName();
}
+
+ public long getTiming(Event.Type type) {
+ return timings[type.ordinal()];
+ }
+
+ public void incTiming(Event.Type type, long delta) {
+ timings[type.ordinal()] += delta;
+ }
+
+ public void flushTimings() {
+ timings = new long[Event.Type.values().length];
+ }
}

0 comments on commit 06d60be

Please sign in to comment.