feat: 修改事件lag提示

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-07-14 15:47:13 +08:00
parent dda778c61f
commit bd79d2013a

View File

@ -1,103 +1,103 @@
package pw.yumc.Yum.inject; package pw.yumc.Yum.inject;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.EventException; import org.bukkit.event.EventException;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.plugin.EventExecutor; import org.bukkit.plugin.EventExecutor;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredListener; 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; 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 String prefix = "§6[§bYum §a事件监控§6] ";
private final EventExecutor originalExecutor; private final EventExecutor originalExecutor;
private final Plugin plugin; private final Plugin plugin;
public Map<String, Long> eventTotalTime; public Map<String, Long> eventTotalTime;
public Map<String, Integer> eventCount; public Map<String, Integer> eventCount;
public ListenerInjector(final EventExecutor originalExecutor, final Plugin plugin) { public ListenerInjector(final EventExecutor originalExecutor, final Plugin plugin) {
this.originalExecutor = originalExecutor; this.originalExecutor = originalExecutor;
this.plugin = plugin; this.plugin = plugin;
eventTotalTime = new HashMap<>(); eventTotalTime = new HashMap<>();
eventCount = new HashMap<>(); eventCount = new HashMap<>();
} }
public static void inject(final Plugin plugin) { public static void inject(final Plugin plugin) {
final List<RegisteredListener> listeners = HandlerList.getRegisteredListeners(plugin); final List<RegisteredListener> listeners = HandlerList.getRegisteredListeners(plugin);
for (final RegisteredListener listener : listeners) { for (final RegisteredListener listener : listeners) {
if (listener instanceof TimedRegisteredListener) { if (listener instanceof TimedRegisteredListener) {
return; return;
} }
final EventExecutor originalExecutor = Reflect.on(listener).get("executor"); final EventExecutor originalExecutor = Reflect.on(listener).get("executor");
if (originalExecutor instanceof ListenerInjector) { if (originalExecutor instanceof ListenerInjector) {
return; return;
} }
final ListenerInjector listenerInjector = new ListenerInjector(originalExecutor, plugin); final ListenerInjector listenerInjector = new ListenerInjector(originalExecutor, plugin);
Reflect.on(listener).set("executor", listenerInjector); Reflect.on(listener).set("executor", listenerInjector);
} }
} }
public static void uninject(final Plugin plugin) { public static void uninject(final Plugin plugin) {
final List<RegisteredListener> listeners = HandlerList.getRegisteredListeners(plugin); final List<RegisteredListener> listeners = HandlerList.getRegisteredListeners(plugin);
for (final RegisteredListener listener : listeners) { for (final RegisteredListener listener : listeners) {
if (listener instanceof TimedRegisteredListener) { if (listener instanceof TimedRegisteredListener) {
return; return;
} }
final EventExecutor executor = Reflect.on(listener).get("executor"); final EventExecutor executor = Reflect.on(listener).get("executor");
if (executor instanceof ListenerInjector) { if (executor instanceof ListenerInjector) {
Reflect.on(listener).set("executor", ((ListenerInjector) executor).getOriginalExecutor()); Reflect.on(listener).set("executor", ((ListenerInjector) executor).getOriginalExecutor());
} }
} }
} }
@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 { try {
if (!event.isAsynchronous()) { if (!event.isAsynchronous()) {
final long start = System.nanoTime(); final long start = System.nanoTime();
// TODO 当操作大于10ms的时候添加一个Lag提示 // TODO 当操作大于10ms的时候添加一个Lag提示
originalExecutor.execute(listener, event); originalExecutor.execute(listener, event);
final long end = System.nanoTime(); final long end = System.nanoTime();
final String en = event.getEventName(); final String en = event.getEventName();
final long lag = end - start; final long lag = end - start;
if (lag / 1000000 > 10) { if (lag / 1000000 > 10) {
PluginKit.sc("§6[§bYum §a能耗监控§6] §c注意! §6插件 §b" + plugin.getName() + " §6处理 §d" + event.getEventName() + " §6事件时§c耗时超过 §410ms!"); PluginKit.sc("§6[§bYum §a能耗监控§6] §c注意! §6插件 §b" + plugin.getName() + " §6处理 §d" + event.getEventName() + " §6事件时§c耗时 §4" + lag / 1000000 + "ms!");
} }
if (eventTotalTime.containsKey(en)) { if (eventTotalTime.containsKey(en)) {
eventTotalTime.put(en, eventTotalTime.get(en) + lag); eventTotalTime.put(en, eventTotalTime.get(en) + lag);
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, 1); eventCount.put(en, 1);
} }
} else { } else {
originalExecutor.execute(listener, event); originalExecutor.execute(listener, event);
} }
} catch (Throwable e) { } catch (Throwable e) {
while (e.getCause() != null) { while (e.getCause() != null) {
e = e.getCause(); e = e.getCause();
} }
PluginKit.sc(prefix + "§6插件 §b" + plugin.getName() + " §6处理 §d" + event.getEventName() + " §6事件时发生异常 §c" + e.getClass().getName() + ": " + e.getMessage()); PluginKit.sc(prefix + "§6插件 §b" + plugin.getName() + " §6处理 §d" + event.getEventName() + " §6事件时发生异常 §c" + e.getClass().getName() + ": " + e.getMessage());
PluginKit.sc("§c错误信息如下:"); PluginKit.sc("§c错误信息如下:");
final int l = e.getStackTrace().length > 5 ? 5 : e.getStackTrace().length; final int l = e.getStackTrace().length > 5 ? 5 : e.getStackTrace().length;
for (int i = 0; i < l; i++) { for (int i = 0; i < l; i++) {
final StackTraceElement ste = e.getStackTrace()[i]; final StackTraceElement ste = e.getStackTrace()[i];
PluginKit.sc(" §e位于 §c" + ste.getClassName() + "." + ste.getMethodName() + "(§4" + ste.getFileName() + ":" + ste.getLineNumber() + "§c)"); PluginKit.sc(" §e位于 §c" + ste.getClassName() + "." + ste.getMethodName() + "(§4" + ste.getFileName() + ":" + ste.getLineNumber() + "§c)");
} }
} }
} }
public EventExecutor getOriginalExecutor() { public EventExecutor getOriginalExecutor() {
return originalExecutor; return originalExecutor;
} }
} }