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.PluginManager;
import org.bukkit.plugin.RegisteredListener;
import org.bukkit.plugin.TimedRegisteredListener;
import org.bukkit.scheduler.BukkitTask;
import cn.citycraft.PluginHelper.commands.HandlerCommand;
@ -43,7 +44,6 @@ public class MonitorCommand implements HandlerCommands {
public MonitorCommand(final Yum yum) {
final InvokeSubCommand cmdhandler = new InvokeSubCommand(yum, "monitor");
cmdhandler.setAllCommandOnlyConsole(yum.getConfig().getBoolean("onlyFileCommandConsole", true));
cmdhandler.registerCommands(this);
cmdhandler.registerCommands(PluginTabComplete.instence);
}
@ -91,7 +91,7 @@ public class MonitorCommand implements HandlerCommands {
}
@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 CommandSender sender = e.getSender();
final Plugin plugin = Bukkit.getPluginManager().getPlugin(pname);
@ -102,8 +102,14 @@ public class MonitorCommand implements HandlerCommands {
sender.sendMessage(prefix + "§6插件 §b" + pname + " §6的事件能耗如下!");
final List<RegisteredListener> listeners = HandlerList.getRegisteredListeners(plugin);
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) {
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");
if (executor instanceof ListenerInjector) {
final ListenerInjector injected = (ListenerInjector) executor;

View File

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

View File

@ -1,5 +1,6 @@
package pw.yumc.Yum.inject;
import java.util.Collections;
import java.util.List;
import org.bukkit.Bukkit;
@ -21,7 +22,7 @@ public class CommandInjector implements TabExecutor {
private final TabCompleter originalCompleter;
public long totalTime;
public long count;
public int count;
public CommandInjector(final CommandExecutor originalCommandExecutor, final TabCompleter originalTabCompleter) {
this.originalExecutor = originalCommandExecutor;
@ -86,7 +87,6 @@ public class CommandInjector implements TabExecutor {
// TODO add a more aggressive 10 ms cpu sample
final boolean result = originalExecutor.onCommand(sender, command, label, args);
final long end = System.nanoTime();
totalTime += end - start;
count++;
return result;
@ -94,6 +94,9 @@ public class CommandInjector implements TabExecutor {
@Override
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();
// TODO add a more aggressive 10 ms cpu sample
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 cn.citycraft.PluginHelper.ext.kit.Reflect;
import cn.citycraft.PluginHelper.kit.PluginKit;
public class ListenerInjector implements EventExecutor {
private final String prefix = "§6[§bYum §a事件监控§6] ";
private final EventExecutor originalExecutor;
public Map<String, Long> eventTotalTime;
public Map<String, Long> eventCount;
private final Plugin plugin;
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.plugin = plugin;
eventTotalTime = new HashMap<>();
eventCount = new HashMap<>();
}
@ -38,7 +42,7 @@ public class ListenerInjector implements EventExecutor {
if (originalExecutor instanceof ListenerInjector) {
return;
}
final ListenerInjector listenerInjector = new ListenerInjector(originalExecutor);
final ListenerInjector listenerInjector = new ListenerInjector(originalExecutor, plugin);
Reflect.on(listener).set("executor", listenerInjector);
}
}
@ -58,21 +62,34 @@ public class ListenerInjector implements EventExecutor {
@Override
public void execute(final Listener listener, final Event event) throws EventException {
if (!event.isAsynchronous()) {
final long start = System.nanoTime();
// TODO add a more aggressive 10 ms cpu sample
originalExecutor.execute(listener, event);
final long end = System.nanoTime();
final String en = event.getEventName();
if (eventTotalTime.containsKey(en)) {
eventTotalTime.put(en, eventTotalTime.get(en) + end - start);
eventCount.put(en, eventCount.get(en) + 1);
try {
if (!event.isAsynchronous()) {
final long start = System.nanoTime();
// TODO add a more aggressive 10 ms cpu sample
originalExecutor.execute(listener, event);
final long end = System.nanoTime();
final String en = event.getEventName();
if (eventTotalTime.containsKey(en)) {
eventTotalTime.put(en, eventTotalTime.get(en) + end - start);
eventCount.put(en, eventCount.get(en) + 1);
} else {
eventTotalTime.put(en, end - start);
eventCount.put(en, 1);
}
} else {
eventTotalTime.put(en, end - start);
eventCount.put(en, 1L);
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)");
}
} else {
originalExecutor.execute(listener, event);
}
}

View File

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