Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
TimingsCommand.java
1 package org.bukkit.command.defaults;
2 
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.PrintStream;
6 import java.util.ArrayList;
7 import java.util.List;
8 
9 import org.apache.commons.lang.Validate;
10 import org.bukkit.Bukkit;
11 import org.bukkit.ChatColor;
13 import org.bukkit.event.Event;
15 import org.bukkit.plugin.Plugin;
18 import org.bukkit.util.StringUtil;
19 
20 import com.google.common.collect.ImmutableList;
21 
22 public class TimingsCommand extends BukkitCommand {
23  private static final List<String> TIMINGS_SUBCOMMANDS = ImmutableList.of("merged", "reset", "separate");
24 
25  public TimingsCommand(String name) {
26  super(name);
27  this.description = "Records timings for all plugin events";
28  this.usageMessage = "/timings <reset|merged|separate>";
29  this.setPermission("bukkit.command.timings");
30  }
31 
32  @Override
33  public boolean execute(CommandSender sender, String currentAlias, String[] args) {
34  if (!testPermission(sender)) return true;
35  if (args.length != 1) {
36  sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
37  return false;
38  }
39  if (!sender.getServer().getPluginManager().useTimings()) {
40  sender.sendMessage("Please enable timings by setting \"settings.plugin-profiling\" to true in bukkit.yml");
41  return true;
42  }
43 
44  boolean separate = "separate".equals(args[0]);
45  if ("reset".equals(args[0])) {
46  for (HandlerList handlerList : HandlerList.getHandlerLists()) {
47  for (RegisteredListener listener : handlerList.getRegisteredListeners()) {
48  if (listener instanceof TimedRegisteredListener) {
49  ((TimedRegisteredListener)listener).reset();
50  }
51  }
52  }
53  sender.sendMessage("Timings reset");
54  } else if ("merged".equals(args[0]) || separate) {
55 
56  int index = 0;
57  int pluginIdx = 0;
58  File timingFolder = new File("timings");
59  timingFolder.mkdirs();
60  File timings = new File(timingFolder, "timings.txt");
61  File names = null;
62  while (timings.exists()) timings = new File(timingFolder, "timings" + (++index) + ".txt");
63  PrintStream fileTimings = null;
64  PrintStream fileNames = null;
65  try {
66  fileTimings = new PrintStream(timings);
67  if (separate) {
68  names = new File(timingFolder, "names" + index + ".txt");
69  fileNames = new PrintStream(names);
70  }
71  for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
72  pluginIdx++;
73  long totalTime = 0;
74  if (separate) {
75  fileNames.println(pluginIdx + " " + plugin.getDescription().getFullName());
76  fileTimings.println("Plugin " + pluginIdx);
77  }
78  else fileTimings.println(plugin.getDescription().getFullName());
79  for (RegisteredListener listener : HandlerList.getRegisteredListeners(plugin)) {
80  if (listener instanceof TimedRegisteredListener) {
81  TimedRegisteredListener trl = (TimedRegisteredListener) listener;
82  long time = trl.getTotalTime();
83  int count = trl.getCount();
84  if (count == 0) continue;
85  long avg = time / count;
86  totalTime += time;
87  Class<? extends Event> eventClass = trl.getEventClass();
88  if (count > 0 && eventClass != null) {
89  fileTimings.println(" " + eventClass.getSimpleName() + (trl.hasMultiple() ? " (and sub-classes)" : "") + " Time: " + time + " Count: " + count + " Avg: " + avg);
90  }
91  }
92  }
93  fileTimings.println(" Total time " + totalTime + " (" + totalTime / 1000000000 + "s)");
94  }
95  sender.sendMessage("Timings written to " + timings.getPath());
96  if (separate) sender.sendMessage("Names written to " + names.getPath());
97  } catch (IOException e) {
98  } finally {
99  if (fileTimings != null) {
100  fileTimings.close();
101  }
102  if (fileNames != null) {
103  fileNames.close();
104  }
105  }
106  }
107  return true;
108  }
109 
110  @Override
111  public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
112  Validate.notNull(sender, "Sender cannot be null");
113  Validate.notNull(args, "Arguments cannot be null");
114  Validate.notNull(alias, "Alias cannot be null");
115 
116  if (args.length == 1) {
117  return StringUtil.copyPartialMatches(args[0], TIMINGS_SUBCOMMANDS, new ArrayList<String>(TIMINGS_SUBCOMMANDS.size()));
118  }
119  return ImmutableList.of();
120  }
121 }
RegisteredListener[] getRegisteredListeners()
boolean testPermission(CommandSender target)
static< TextendsCollection<?superString > T copyPartialMatches(final String token, final Iterable< String > originals, final T collection)
Definition: StringUtil.java:24
PluginManager getPluginManager()
void sendMessage(String message)
void setPermission(String permission)
static ArrayList< HandlerList > getHandlerLists()
static PluginManager getPluginManager()
Definition: Bukkit.java:208