feat: 优化监控代码

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-07-20 20:39:15 +08:00
parent e65246f55e
commit b066e8a81e
6 changed files with 37 additions and 48 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>pw.yumc</groupId>
<artifactId>Yum</artifactId>
<version>2.6.7</version>
<version>2.6.8</version>
<name>Yum</name>
<description>Minecraft 服务器插件管理系统</description>
<build>
@ -60,10 +60,10 @@
<properties>
<update.description>&amp;a全新 2.X 版本 更多守护与优化</update.description>
<update.changes>
&amp;c注意 &amp;6- &amp;cYum更新需要重启服务器!重启服务器!重启服务器!;
&amp;c注意 &amp;6- &amp;aYum更新需要&amp;d重启服务器!&amp;c重启服务器!&amp;4重启服务器!;
&amp;b2.6.8 &amp;6- &amp;c修复PerWorldPlugins的监控错误...;
&amp;b2.6.7 &amp;6- &amp;d兼容PerWorldPlugins...;
&amp;b2.6.6 &amp;6- &amp;c修复快捷点击按钮触发命令错误...;
&amp;b2.6.5 &amp;6- &amp;c修复list命令 &amp;a添加全局能耗统计...;
</update.changes>
<env.GIT_COMMIT>DEBUG</env.GIT_COMMIT>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View File

@ -1,5 +1,6 @@
package pw.yumc.Yum.commands;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -31,7 +32,6 @@ import pw.yumc.Yum.api.YumAPI;
import pw.yumc.Yum.inject.CommandInjector;
import pw.yumc.Yum.inject.ListenerInjector;
import pw.yumc.Yum.inject.TaskInjector;
import pw.yumc.Yum.managers.ConfigManager;
import pw.yumc.Yum.managers.MonitorManager;
import pw.yumc.Yum.managers.MonitorManager.MonitorInfo;
@ -41,7 +41,6 @@ import pw.yumc.Yum.managers.MonitorManager.MonitorInfo;
* @author
*/
public class MonitorCommand implements HandlerCommands {
public static boolean debug = false;
public static Throwable lastError = null;
private final String prefix = "§6[§bYum §a能耗监控§6] ";
@ -70,7 +69,6 @@ public class MonitorCommand implements HandlerCommands {
final InvokeSubCommand cmdhandler = new InvokeSubCommand(yum, "monitor");
cmdhandler.registerCommands(this);
cmdhandler.registerCommands(PluginTabComplete.instence);
debug = ConfigManager.i().isMonitorDebug();
}
@HandlerCommand(name = "cmd", aliases = "c", description = "查看插件命令能耗", minimumArguments = 1, possibleArguments = "[插件名称]")
@ -111,7 +109,7 @@ public class MonitorCommand implements HandlerCommands {
}
@HandlerCommand(name = "event", aliases = "e", description = "查看插件事件能耗", minimumArguments = 1, possibleArguments = "[插件名称]")
public void event(final InvokeCommandEvent e) throws InstantiationException, IllegalAccessException {
public void event(final InvokeCommandEvent e) throws InstantiationException, IllegalAccessException, NoSuchFieldException {
final String pname = e.getArgs()[0];
final CommandSender sender = e.getSender();
final Plugin plugin = Bukkit.getPluginManager().getPlugin(pname);
@ -130,7 +128,12 @@ public class MonitorCommand implements HandlerCommands {
eventCount.put(trl.getEventClass().getSimpleName(), trl.getCount());
continue;
}
final EventExecutor executor = Reflect.on(listener).get("executor");
EventExecutor executor = Reflect.on(listener).get("executor");
if (listener.getClass().getName().contains("PWPRegisteredListener")) {
final Field f = Reflect.on(listener).getDeclaredField(RegisteredListener.class, "executor");
f.setAccessible(true);
executor = (EventExecutor) f.get(listener);
}
if (executor instanceof ListenerInjector) {
final ListenerInjector injected = (ListenerInjector) executor;
for (final String entry : injected.eventTotalTime.keySet()) {

View File

@ -108,7 +108,7 @@ public class CommandInjector implements TabExecutor {
final boolean result = originalExecutor.onCommand(sender, command, label, args);
final long end = System.nanoTime();
final long lag = end - start;
if (Bukkit.isPrimaryThread() && lag / 1000000 > 10) {
if (Bukkit.isPrimaryThread() && lag / 1000000 > MonitorManager.lagTime) {
PluginKit.sc("§6[§bYum §a能耗监控§6] §c注意! §6玩家 §a" + sender.getName() + " §6执行 §b" + plugin.getName() + " §6插件 §d" + label + " " + StrKit.join(args, " ") + " §6命令 §c耗时 §4" + lag / 1000000 + "ms!");
}
totalTime += lag;
@ -125,18 +125,7 @@ public class CommandInjector implements TabExecutor {
sender.sendMessage("§6异常说明: §3" + e.getMessage());
MonitorCommand.lastError = e;
PluginKit.sc(prefix + "§6玩家 §a" + sender.getName() + " §6执行 §b" + plugin.getName() + " §6插件 §d" + label + " " + StrKit.join(args, " ") + " §6命令时发生异常!");
PluginKit.sc("§6异常名称: §c" + e.getClass().getName());
PluginKit.sc("§6异常说明: §3" + e.getMessage());
PluginKit.sc("§6简易错误信息如下:");
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)");
}
if (MonitorCommand.debug) {
PluginKit.sc("§c开发人员调试信息如下:");
e.printStackTrace();
}
MonitorManager.print(e);
}
return false;
}

View File

@ -98,7 +98,7 @@ public class ListenerInjector implements EventExecutor {
final long end = System.nanoTime();
final String en = event.getEventName();
final long lag = end - start;
if (lag / 1000000 > 10 && !ConfigManager.i().getMonitorIgnoreList().contains(plugin.getName())) {
if (lag / 1000000 > MonitorManager.lagTime && !ConfigManager.i().getMonitorIgnoreList().contains(plugin.getName())) {
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)) {
@ -118,18 +118,7 @@ public class ListenerInjector implements EventExecutor {
}
MonitorCommand.lastError = e;
PluginKit.sc(prefix + "§6插件 §b" + plugin.getName() + " §6处理 §d" + event.getEventName() + " §6事件时发生异常!");
PluginKit.sc("§6异常名称: §c" + e.getClass().getName());
PluginKit.sc("§6异常说明: §3" + e.getMessage());
PluginKit.sc("§6简易错误信息如下:");
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)");
}
if (MonitorCommand.debug) {
PluginKit.sc("§c开发人员调试信息如下:");
e.printStackTrace();
}
MonitorManager.print(e);
}
}

View File

@ -80,14 +80,13 @@ public class TaskInjector implements Runnable {
@Override
public void run() {
try {
final long start = System.nanoTime();
// TODO 当操作大于10ms的时候添加一个Lag提示
originalTask.run();
final long end = System.nanoTime();
final long lag = end - start;
if (Bukkit.isPrimaryThread() && lag / 1000000 > 10) {
if (Bukkit.isPrimaryThread() && lag / 1000000 > MonitorManager.lagTime) {
PluginKit.sc("§6[§bYum §a能耗监控§6] §c注意! §6插件 §b" + plugin.getName() + " §6执行 §d" + taskName + " §6任务 §c耗时 §4" + lag / 1000000 + "ms!");
}
totalTime += lag;
@ -99,18 +98,7 @@ public class TaskInjector implements Runnable {
}
MonitorCommand.lastError = e;
PluginKit.sc(prefix + "§6插件 §b" + plugin.getName() + " §6执行 §d" + taskName + " §6任务时发生异常!");
PluginKit.sc("§6异常名称: §c" + e.getClass().getName());
PluginKit.sc("§6异常说明: §3" + e.getMessage());
PluginKit.sc("§6简易错误信息如下:");
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)");
}
if (MonitorCommand.debug) {
PluginKit.sc("§c开发人员调试信息如下:");
e.printStackTrace();
}
MonitorManager.print(e);
}
}

View File

@ -10,12 +10,17 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import cn.citycraft.PluginHelper.kit.PluginKit;
/**
*
* @since 2016年7月19日 下午3:55:54
* @author
*/
public class MonitorManager {
public static int lagTime = 20;
public static boolean debug = ConfigManager.i().isMonitorDebug();
private static Map<String, Long> monitor = new HashMap<>();
private static Map<String, Long> task = new HashMap<>();
private static Map<String, Long> event = new HashMap<>();
@ -46,6 +51,21 @@ public class MonitorManager {
return new MonitorInfo(monitor.get(pname) / um, cmd.get(pname) / um, event.get(pname) / um, task.get(pname) / um);
}
public static void print(final Throwable e) {
PluginKit.sc("§6异常名称: §c" + e.getClass().getName());
PluginKit.sc("§6异常说明: §3" + e.getMessage());
PluginKit.sc("§6简易错误信息如下:");
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)");
}
if (debug) {
PluginKit.sc("§c开发人员调试信息如下:");
e.printStackTrace();
}
}
public static void reset(final String pname) {
monitor.put(pname, 0L);
task.put(pname, 0L);