summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/bukkit/command/SimpleCommandMap.java3
-rw-r--r--src/main/java/org/bukkit/command/defaults/TimingsCommand.java73
-rw-r--r--src/main/java/org/bukkit/plugin/Plugin.java7
-rw-r--r--src/main/java/org/bukkit/plugin/SimplePluginManager.java2
-rw-r--r--src/main/java/org/bukkit/plugin/java/JavaPlugin.java14
5 files changed, 98 insertions, 1 deletions
diff --git a/src/main/java/org/bukkit/command/SimpleCommandMap.java b/src/main/java/org/bukkit/command/SimpleCommandMap.java
index 5fa0f07a..4ce74d3c 100644
--- a/src/main/java/org/bukkit/command/SimpleCommandMap.java
+++ b/src/main/java/org/bukkit/command/SimpleCommandMap.java
@@ -54,6 +54,7 @@ public class SimpleCommandMap implements CommandMap {
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 class SimpleCommandMap implements CommandMap {
/**
* 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.
diff --git a/src/main/java/org/bukkit/command/defaults/TimingsCommand.java b/src/main/java/org/bukkit/command/defaults/TimingsCommand.java
new file mode 100644
index 00000000..3798f5e9
--- /dev/null
+++ b/src/main/java/org/bukkit/command/defaults/TimingsCommand.java
@@ -0,0 +1,73 @@
+package org.bukkit.command.defaults;
+
+import org.bukkit.Bukkit;
+import org.bukkit.command.Command;
+import org.bukkit.command.CommandSender;
+import org.bukkit.event.Event;
+import org.bukkit.plugin.Plugin;
+
+import java.io.File;
+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;
+
+ boolean seperate = "seperate".equals(args[0]);
+ if ("reset".equals(args[0])) {
+ for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
+ plugin.resetTimings();
+ }
+ sender.sendMessage("Timings reset");
+ }
+ else if("merged".equals(args[0]) || seperate) {
+
+ int index = 0;
+ int pluginIdx = 0;
+ File timingFolder = new File("timings");
+ timingFolder.mkdirs();
+ File timings = new File(timingFolder, "timings.txt");
+ File names = null;
+ while (timings.exists()) timings = new File(timingFolder, "timinigs" + (++index) + ".txt");
+ try {
+ PrintStream fileTimings = new PrintStream(timings);
+ PrintStream fileNames = null;
+ if (seperate) {
+ names = new File(timingFolder, "names" + index + ".txt");
+ fileNames = new PrintStream(names);
+ }
+ for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
+ pluginIdx++;
+ long totalTime = 0;
+ if (seperate) {
+ fileNames.println(pluginIdx + " " + plugin.getDescription().getFullName());
+ fileTimings.println("Plugin " + pluginIdx);
+ }
+ else fileTimings.println(plugin.getDescription().getFullName());
+ for (Event.Type type : Event.Type.values()) {
+ long time = plugin.getTiming(type);
+ totalTime += time;
+ if (time > 0) {
+ fileTimings.println(" " + type.name() + " " + time);
+ }
+ }
+ fileTimings.println(" Total time " + totalTime + " (" + totalTime / 1000000000 + "s)");
+ }
+ sender.sendMessage("Timings written to " + timings.getPath());
+ if (seperate) sender.sendMessage("Names written to " + names.getPath());
+ } catch (IOException e) {
+ }
+ }
+ return true;
+ }
+}
diff --git a/src/main/java/org/bukkit/plugin/Plugin.java b/src/main/java/org/bukkit/plugin/Plugin.java
index 4bf8c11d..c03fbf3b 100644
--- a/src/main/java/org/bukkit/plugin/Plugin.java
+++ b/src/main/java/org/bukkit/plugin/Plugin.java
@@ -6,6 +6,7 @@ import java.io.InputStream;
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 @@ public interface Plugin extends CommandExecutor {
* @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 resetTimings();
}
diff --git a/src/main/java/org/bukkit/plugin/SimplePluginManager.java b/src/main/java/org/bukkit/plugin/SimplePluginManager.java
index f0888554..addbca3e 100644
--- a/src/main/java/org/bukkit/plugin/SimplePluginManager.java
+++ b/src/main/java/org/bukkit/plugin/SimplePluginManager.java
@@ -336,7 +336,9 @@ public final class SimplePluginManager implements PluginManager {
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();
diff --git a/src/main/java/org/bukkit/plugin/java/JavaPlugin.java b/src/main/java/org/bukkit/plugin/java/JavaPlugin.java
index 9becc234..07b89bba 100644
--- a/src/main/java/org/bukkit/plugin/java/JavaPlugin.java
+++ b/src/main/java/org/bukkit/plugin/java/JavaPlugin.java
@@ -20,6 +20,7 @@ import org.bukkit.command.CommandSender;
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 @@ public abstract class JavaPlugin implements Plugin {
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 @@ public abstract class JavaPlugin implements Plugin {
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 resetTimings() {
+ timings = new long[Event.Type.values().length];
+ }
}