feat: 添加Timings事件监控数据 添加事件异常拦截

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-07-14 00:21:01 +08:00
parent f8a987606a
commit d029f96c31
5 changed files with 50 additions and 26 deletions

View File

@ -16,6 +16,7 @@ import org.bukkit.plugin.EventExecutor;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredListener; import org.bukkit.plugin.RegisteredListener;
import org.bukkit.plugin.TimedRegisteredListener;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import cn.citycraft.PluginHelper.commands.HandlerCommand; import cn.citycraft.PluginHelper.commands.HandlerCommand;
@ -43,7 +44,6 @@ public class MonitorCommand implements HandlerCommands {
public MonitorCommand(final Yum yum) { public MonitorCommand(final Yum yum) {
final InvokeSubCommand cmdhandler = new InvokeSubCommand(yum, "monitor"); final InvokeSubCommand cmdhandler = new InvokeSubCommand(yum, "monitor");
cmdhandler.setAllCommandOnlyConsole(yum.getConfig().getBoolean("onlyFileCommandConsole", true));
cmdhandler.registerCommands(this); cmdhandler.registerCommands(this);
cmdhandler.registerCommands(PluginTabComplete.instence); cmdhandler.registerCommands(PluginTabComplete.instence);
} }
@ -91,7 +91,7 @@ public class MonitorCommand implements HandlerCommands {
} }
@HandlerCommand(name = "event", minimumArguments = 1, possibleArguments = "插件名称") @HandlerCommand(name = "event", minimumArguments = 1, possibleArguments = "插件名称")
public void event(final InvokeCommandEvent e) { public void event(final InvokeCommandEvent e) throws InstantiationException, IllegalAccessException {
final String pname = e.getArgs()[0]; final String pname = e.getArgs()[0];
final CommandSender sender = e.getSender(); final CommandSender sender = e.getSender();
final Plugin plugin = Bukkit.getPluginManager().getPlugin(pname); final Plugin plugin = Bukkit.getPluginManager().getPlugin(pname);
@ -102,8 +102,14 @@ public class MonitorCommand implements HandlerCommands {
sender.sendMessage(prefix + "§6插件 §b" + pname + " §6的事件能耗如下!"); sender.sendMessage(prefix + "§6插件 §b" + pname + " §6的事件能耗如下!");
final List<RegisteredListener> listeners = HandlerList.getRegisteredListeners(plugin); final List<RegisteredListener> listeners = HandlerList.getRegisteredListeners(plugin);
final Map<String, Long> eventTotalTime = new HashMap<>(); final Map<String, Long> eventTotalTime = new HashMap<>();
final Map<String, Long> eventCount = new HashMap<>(); final Map<String, Integer> eventCount = new HashMap<>();
for (final RegisteredListener listener : listeners) { for (final RegisteredListener listener : listeners) {
if (listener instanceof TimedRegisteredListener) {
final TimedRegisteredListener trl = (TimedRegisteredListener) listener;
eventTotalTime.put(trl.getEventClass().newInstance().getEventName(), trl.getTotalTime());
eventCount.put(trl.getEventClass().newInstance().getEventName(), trl.getCount());
continue;
}
final EventExecutor executor = Reflect.on(listener).get("executor"); final EventExecutor executor = Reflect.on(listener).get("executor");
if (executor instanceof ListenerInjector) { if (executor instanceof ListenerInjector) {
final ListenerInjector injected = (ListenerInjector) executor; final ListenerInjector injected = (ListenerInjector) executor;

View File

@ -10,7 +10,6 @@ public class NetCommand implements HandlerCommands {
public NetCommand(final Yum yum) { public NetCommand(final Yum yum) {
final InvokeSubCommand cmdhandler = new InvokeSubCommand(yum, "net"); final InvokeSubCommand cmdhandler = new InvokeSubCommand(yum, "net");
cmdhandler.setAllCommandOnlyConsole(yum.getConfig().getBoolean("onlyNetCommandConsole", false));
cmdhandler.registerCommands(this); cmdhandler.registerCommands(this);
cmdhandler.registerCommands(PluginTabComplete.instence); cmdhandler.registerCommands(PluginTabComplete.instence);
} }

View File

@ -1,5 +1,6 @@
package pw.yumc.Yum.inject; package pw.yumc.Yum.inject;
import java.util.Collections;
import java.util.List; import java.util.List;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -21,7 +22,7 @@ public class CommandInjector implements TabExecutor {
private final TabCompleter originalCompleter; private final TabCompleter originalCompleter;
public long totalTime; public long totalTime;
public long count; public int count;
public CommandInjector(final CommandExecutor originalCommandExecutor, final TabCompleter originalTabCompleter) { public CommandInjector(final CommandExecutor originalCommandExecutor, final TabCompleter originalTabCompleter) {
this.originalExecutor = originalCommandExecutor; this.originalExecutor = originalCommandExecutor;
@ -86,7 +87,6 @@ public class CommandInjector implements TabExecutor {
// TODO add a more aggressive 10 ms cpu sample // TODO add a more aggressive 10 ms cpu sample
final boolean result = originalExecutor.onCommand(sender, command, label, args); final boolean result = originalExecutor.onCommand(sender, command, label, args);
final long end = System.nanoTime(); final long end = System.nanoTime();
totalTime += end - start; totalTime += end - start;
count++; count++;
return result; return result;
@ -94,6 +94,9 @@ public class CommandInjector implements TabExecutor {
@Override @Override
public List<String> onTabComplete(final CommandSender sender, final Command command, final String alias, final String[] args) { public List<String> onTabComplete(final CommandSender sender, final Command command, final String alias, final String[] args) {
if (originalCompleter == null) {
return Collections.emptyList();
}
final long start = System.nanoTime(); final long start = System.nanoTime();
// TODO add a more aggressive 10 ms cpu sample // TODO add a more aggressive 10 ms cpu sample
final List<String> result = originalCompleter.onTabComplete(sender, command, alias, args); final List<String> result = originalCompleter.onTabComplete(sender, command, alias, args);

View File

@ -14,16 +14,20 @@ import org.bukkit.plugin.RegisteredListener;
import org.bukkit.plugin.TimedRegisteredListener; import org.bukkit.plugin.TimedRegisteredListener;
import cn.citycraft.PluginHelper.ext.kit.Reflect; import cn.citycraft.PluginHelper.ext.kit.Reflect;
import cn.citycraft.PluginHelper.kit.PluginKit;
public class ListenerInjector implements EventExecutor { public class ListenerInjector implements EventExecutor {
private final String prefix = "§6[§bYum §a事件监控§6] ";
private final EventExecutor originalExecutor; private final EventExecutor originalExecutor;
public Map<String, Long> eventTotalTime; private final Plugin plugin;
public Map<String, Long> eventCount;
public ListenerInjector(final EventExecutor originalExecutor) { public Map<String, Long> eventTotalTime;
public Map<String, Integer> eventCount;
public ListenerInjector(final EventExecutor originalExecutor, final Plugin plugin) {
this.originalExecutor = originalExecutor; this.originalExecutor = originalExecutor;
this.plugin = plugin;
eventTotalTime = new HashMap<>(); eventTotalTime = new HashMap<>();
eventCount = new HashMap<>(); eventCount = new HashMap<>();
} }
@ -38,7 +42,7 @@ public class ListenerInjector implements EventExecutor {
if (originalExecutor instanceof ListenerInjector) { if (originalExecutor instanceof ListenerInjector) {
return; return;
} }
final ListenerInjector listenerInjector = new ListenerInjector(originalExecutor); final ListenerInjector listenerInjector = new ListenerInjector(originalExecutor, plugin);
Reflect.on(listener).set("executor", listenerInjector); Reflect.on(listener).set("executor", listenerInjector);
} }
} }
@ -58,6 +62,7 @@ public class ListenerInjector implements EventExecutor {
@Override @Override
public void execute(final Listener listener, final Event event) throws EventException { public void execute(final Listener listener, final Event event) throws EventException {
try {
if (!event.isAsynchronous()) { if (!event.isAsynchronous()) {
final long start = System.nanoTime(); final long start = System.nanoTime();
// TODO add a more aggressive 10 ms cpu sample // TODO add a more aggressive 10 ms cpu sample
@ -69,11 +74,23 @@ public class ListenerInjector implements EventExecutor {
eventCount.put(en, eventCount.get(en) + 1); eventCount.put(en, eventCount.get(en) + 1);
} else { } else {
eventTotalTime.put(en, end - start); eventTotalTime.put(en, end - start);
eventCount.put(en, 1L); eventCount.put(en, 1);
} }
} else { } else {
originalExecutor.execute(listener, event); originalExecutor.execute(listener, event);
} }
} catch (Throwable e) {
while (e.getCause() != null) {
e = e.getCause();
}
PluginKit.sc(prefix + "插件 §b" + plugin.getName() + " §6处理 " + event.getEventName() + " §a事件时发生异常 §c" + e.getClass().getName() + ": " + e.getMessage());
PluginKit.sc("§c错误信息如下:");
final int l = e.getStackTrace().length > 5 ? 5 : e.getStackTrace().length;
for (int i = 0; i < l; i++) {
final StackTraceElement ste = e.getStackTrace()[i];
PluginKit.sc(" §e位于 §c" + ste.getClassName() + "." + ste.getMethodName() + "(§4" + ste.getFileName() + ":" + ste.getLineNumber() + "§c)");
}
}
} }
public EventExecutor getOriginalExecutor() { public EventExecutor getOriginalExecutor() {

View File

@ -14,7 +14,7 @@ public class TaskInjector implements Runnable {
private final Runnable originalTask; private final Runnable originalTask;
public long totalTime; public long totalTime;
public long count; public int count;
public TaskInjector(final Runnable originalTask) { public TaskInjector(final Runnable originalTask) {
this.originalTask = originalTask; this.originalTask = originalTask;
@ -62,7 +62,6 @@ public class TaskInjector implements Runnable {
// TODO add a more aggressive 10 ms cpu sample // TODO add a more aggressive 10 ms cpu sample
originalTask.run(); originalTask.run();
final long end = System.nanoTime(); final long end = System.nanoTime();
totalTime += end - start; totalTime += end - start;
count++; count++;
} }