testserver/Bukkit-Patches/0003-Add-CustomTimingsHandl...

232 lines
9.5 KiB
Diff

From d03d33ba1d6f53a41a2c17fdafa8e8b45653e8f0 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Wed, 9 Jan 2013 22:18:26 -0500
Subject: [PATCH] Add CustomTimingsHandler for adding new CraftBukkit custom
timings
---
.../org/bukkit/command/defaults/ReloadCommand.java | 2 +
.../bukkit/command/defaults/TimingsCommand.java | 65 ++++++++++++++++++++--
.../org/bukkit/event/CustomTimingsHandler.java | 60 ++++++++++++++++++++
3 files changed, 121 insertions(+), 6 deletions(-)
create mode 100644 src/main/java/org/bukkit/event/CustomTimingsHandler.java
diff --git a/src/main/java/org/bukkit/command/defaults/ReloadCommand.java b/src/main/java/org/bukkit/command/defaults/ReloadCommand.java
index fb3c90f..fffafa5 100644
--- a/src/main/java/org/bukkit/command/defaults/ReloadCommand.java
+++ b/src/main/java/org/bukkit/command/defaults/ReloadCommand.java
@@ -6,6 +6,7 @@ import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
+import org.bukkit.event.CustomTimingsHandler;
public class ReloadCommand extends BukkitCommand {
public ReloadCommand(String name) {
@@ -20,6 +21,7 @@ public class ReloadCommand extends BukkitCommand {
public boolean execute(CommandSender sender, String currentAlias, String[] args) {
if (!testPermission(sender)) return true;
+ CustomTimingsHandler.reload(); // Spigot
Bukkit.reload();
Command.broadcastCommandMessage(sender, ChatColor.GREEN + "Reload complete.");
diff --git a/src/main/java/org/bukkit/command/defaults/TimingsCommand.java b/src/main/java/org/bukkit/command/defaults/TimingsCommand.java
index 94cd62c..66edad9 100644
--- a/src/main/java/org/bukkit/command/defaults/TimingsCommand.java
+++ b/src/main/java/org/bukkit/command/defaults/TimingsCommand.java
@@ -10,6 +10,7 @@ import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
+import org.bukkit.event.CustomTimingsHandler;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.Plugin;
@@ -18,15 +19,21 @@ import org.bukkit.plugin.TimedRegisteredListener;
import org.bukkit.util.StringUtil;
import com.google.common.collect.ImmutableList;
+import java.io.ByteArrayOutputStream;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLEncoder;
+import java.util.logging.Level;
public class TimingsCommand extends BukkitCommand {
- private static final List<String> TIMINGS_SUBCOMMANDS = ImmutableList.of("merged", "reset", "separate");
+ private static final List<String> TIMINGS_SUBCOMMANDS = ImmutableList.of("merged", "reset", "separate", "paste"); // Spigot
public static long timingStart = 0; // Spigot
public TimingsCommand(String name) {
super(name);
this.description = "Records timings for all plugin events";
- this.usageMessage = "/timings <reset|merged|separate>";
+ this.usageMessage = "/timings <reset|merged|separate|paste>";
this.setPermission("bukkit.command.timings");
}
@@ -43,6 +50,7 @@ public class TimingsCommand extends BukkitCommand {
}
boolean separate = "separate".equals(args[0]);
+ boolean paste = "paste".equals(args[0]);
if ("reset".equals(args[0])) {
for (HandlerList handlerList : HandlerList.getHandlerLists()) {
for (RegisteredListener listener : handlerList.getRegisteredListeners()) {
@@ -51,9 +59,10 @@ public class TimingsCommand extends BukkitCommand {
}
}
}
+ CustomTimingsHandler.reload(); // Spigot
timingStart = System.nanoTime(); // Spigot
sender.sendMessage("Timings reset");
- } else if ("merged".equals(args[0]) || separate) {
+ } else if ("merged".equals(args[0]) || separate || paste) {
long sampleTime = System.nanoTime() - timingStart; // Spigot
int index = 0;
@@ -62,11 +71,12 @@ public class TimingsCommand extends BukkitCommand {
timingFolder.mkdirs();
File timings = new File(timingFolder, "timings.txt");
File names = null;
+ ByteArrayOutputStream bout = (paste) ? new ByteArrayOutputStream() : null;
while (timings.exists()) timings = new File(timingFolder, "timings" + (++index) + ".txt");
PrintStream fileTimings = null;
PrintStream fileNames = null;
try {
- fileTimings = new PrintStream(timings);
+ fileTimings = (paste) ? new PrintStream(bout) : new PrintStream(timings);
if (separate) {
names = new File(timingFolder, "names" + index + ".txt");
fileNames = new PrintStream(names);
@@ -95,8 +105,17 @@ public class TimingsCommand extends BukkitCommand {
}
fileTimings.println(" Total time " + totalTime + " (" + totalTime / 1000000000 + "s)");
}
- fileTimings.println("Sample time " + sampleTime + " (" + sampleTime / 1000000000 + "s)"); // Spigot
- sender.sendMessage("Timings written to " + timings.getPath());
+
+ // Spigot start
+ CustomTimingsHandler.printTimings(fileTimings);
+ fileTimings.println("Sample time " + sampleTime + " (" + sampleTime / 1000000000 + "s)");
+ if (paste) {
+ new PasteThread(sender, bout).start();
+ } else {
+ sender.sendMessage("Timings written to " + timings.getPath());
+ sender.sendMessage("Paste contents of file into form at http://aikar.co/timings.php to read results.");
+ }
+ // Spigot end
if (separate) sender.sendMessage("Names written to " + names.getPath());
} catch (IOException e) {
} finally {
@@ -122,4 +141,38 @@ public class TimingsCommand extends BukkitCommand {
}
return ImmutableList.of();
}
+
+ private static class PasteThread extends Thread {
+
+ private final CommandSender sender;
+ private final ByteArrayOutputStream bout;
+
+ public PasteThread(CommandSender sender, ByteArrayOutputStream bout) {
+ super("Timings paste thread");
+ this.sender = sender;
+ this.bout = bout;
+ }
+
+ @Override
+ public void run() {
+ try {
+ HttpURLConnection con = (HttpURLConnection) new URL("http://paste.ubuntu.com/").openConnection();
+ con.setDoOutput(true);
+ con.setRequestMethod("POST");
+ con.setInstanceFollowRedirects(false);
+
+ OutputStream out = con.getOutputStream();
+ out.write("poster=Spigot&syntax=text&content=".getBytes("UTF-8"));
+ out.write(URLEncoder.encode(bout.toString("UTF-8"), "UTF-8").getBytes("UTF-8"));
+ out.close();
+ con.getInputStream().close();
+
+ String location = con.getHeaderField("Location");
+ sender.sendMessage(ChatColor.GREEN + "Your timings have been pasted to " + location + ". Upload to http://aikar.co/timings.php to read the results.");
+ } catch (IOException ex) {
+ sender.sendMessage(ChatColor.RED + "Error pasting timings, check your console for more information");
+ Bukkit.getServer().getLogger().log(Level.WARNING, "Could not paste timings", ex);
+ }
+ }
+ }
}
diff --git a/src/main/java/org/bukkit/event/CustomTimingsHandler.java b/src/main/java/org/bukkit/event/CustomTimingsHandler.java
new file mode 100644
index 0000000..ff56673
--- /dev/null
+++ b/src/main/java/org/bukkit/event/CustomTimingsHandler.java
@@ -0,0 +1,60 @@
+package org.bukkit.event;
+
+
+import org.bukkit.Bukkit;
+
+import java.io.PrintStream;
+import java.util.ArrayList;
+
+/**
+ * Extends RegisteredListener to include timing information
+ */
+public class CustomTimingsHandler {
+
+ private final String name;
+ public int count = 0;
+ public long totalTime = 0;
+ long start = 0;
+
+ public static ArrayList<CustomTimingsHandler> allList = new ArrayList<CustomTimingsHandler>();
+ public CustomTimingsHandler(String name) {
+ this.name = name;
+ allList.add(this);
+ }
+
+ static public void printTimings(PrintStream printStream) {
+ printStream.println("Minecraft - ** indicates it's already counted by another timing");
+ for (CustomTimingsHandler t : allList) {
+ long time = t.totalTime;
+ int count = t.count;
+ if (count == 0) continue;
+ long avg = time / count;
+
+ printStream.println(" " + t.name + " Time: " + time + " Count: " + count + " Avg: " + avg);
+ }
+ }
+
+ static public void reload() {
+ if (!Bukkit.getServer().getPluginManager().useTimings()) return;
+ for (CustomTimingsHandler t : allList) {
+ t.reset();
+ }
+ }
+
+ public void startTiming() {
+ if (!Bukkit.getServer().getPluginManager().useTimings()) return;
+ start = System.nanoTime();
+ }
+
+ public void stopTiming() {
+ if (!Bukkit.getServer().getPluginManager().useTimings()) return;
+ totalTime += System.nanoTime() - start;
+ count++;
+ }
+
+ public void reset() {
+ count = 0;
+ totalTime = 0;
+ }
+}
+
--
1.8.1-rc2