Add paste flag to timings.

This commit is contained in:
md_5 2013-02-23 09:33:01 +11:00
parent aa42a15d01
commit 9c7cbd37c9

View file

@ -1,4 +1,4 @@
From d86adddc5c61aca7992407141354dcbadebf4723 Mon Sep 17 00:00:00 2001
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
@ -6,9 +6,9 @@ Subject: [PATCH] Add CustomTimingsHandler for adding new CraftBukkit custom
---
.../org/bukkit/command/defaults/ReloadCommand.java | 2 +
.../bukkit/command/defaults/TimingsCommand.java | 4 ++
.../org/bukkit/event/CustomTimingsHandler.java | 60 ++++++++++++++++++++++
3 files changed, 66 insertions(+)
.../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
@ -32,7 +32,7 @@ index fb3c90f..fffafa5 100644
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..e0628d0 100644
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;
@ -43,25 +43,123 @@ index 94cd62c..e0628d0 100644
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.Plugin;
@@ -51,6 +52,7 @@ public class TimingsCommand extends BukkitCommand {
@@ -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) {
@@ -95,8 +97,10 @@ public class TimingsCommand extends BukkitCommand {
- } 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)");
}
+ CustomTimingsHandler.printTimings(fileTimings); // Spigot
fileTimings.println("Sample time " + sampleTime + " (" + sampleTime / 1000000000 + "s)"); // Spigot
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
- 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