feat: 更新至2.6 完善能耗监控 添加事件Lag提示

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-07-14 15:34:38 +08:00
parent d029f96c31
commit 9746c2ea28
5 changed files with 31 additions and 31 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>pw.yumc</groupId> <groupId>pw.yumc</groupId>
<artifactId>Yum</artifactId> <artifactId>Yum</artifactId>
<version>2.5.1</version> <version>2.6</version>
<name>Yum</name> <name>Yum</name>
<description>Minecraft 服务器插件管理系统</description> <description>Minecraft 服务器插件管理系统</description>
<build> <build>
@ -60,10 +60,9 @@
<properties> <properties>
<update.description>&amp;a全新 2.X 版本 更多守护与优化</update.description> <update.description>&amp;a全新 2.X 版本 更多守护与优化</update.description>
<update.changes> <update.changes>
&amp;b2.5.1 &amp;6- &amp;b添加能耗监控系统(Bate)...; &amp;b2.6 &amp;6- &amp;a添加能耗监控系统 事件错误监控...;
&amp;b2.5 &amp;6- &amp;d修复主线程守护系统错误...; &amp;b2.5 &amp;6- &amp;d修复主线程守护系统错误...;
&amp;b2.4 &amp;6- &amp;c修复重载插件导致线程安全错误的问题...; &amp;b2.4 &amp;6- &amp;c修复重载插件导致线程安全错误的问题...;
&amp;b2.3 &amp;6- &amp;a网络监控添加域名白名单功能...;
</update.changes> </update.changes>
<env.GIT_COMMIT>DEBUG</env.GIT_COMMIT> <env.GIT_COMMIT>DEBUG</env.GIT_COMMIT>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View File

@ -37,10 +37,12 @@ import pw.yumc.Yum.inject.TaskInjector;
*/ */
public class MonitorCommand implements HandlerCommands { public class MonitorCommand implements HandlerCommands {
private final String prefix = "§6[§bYum §a能耗监控§6] "; private final String prefix = "§6[§bYum §a能耗监控§6] ";
private final String total = "§6总耗时: §a%.2f"; private final String total = "§6总耗时: §a%.2f";
private final String count = "§6执行次数: §b%s次 "; private final String count = "§6执行次数: §b%s次 ";
private final String avg = "§6平均耗时: §d%.5f秒!"; private final String avg = "§6平均耗时: §d%.5f毫秒!";
private final String p_n_f = prefix + "§c插件不存在!"; private final String p_n_f = prefix + "§c插件 §b%s §c不存在!";
private final double um = 1000000.0;
public MonitorCommand(final Yum yum) { public MonitorCommand(final Yum yum) {
final InvokeSubCommand cmdhandler = new InvokeSubCommand(yum, "monitor"); final InvokeSubCommand cmdhandler = new InvokeSubCommand(yum, "monitor");
@ -48,17 +50,12 @@ public class MonitorCommand implements HandlerCommands {
cmdhandler.registerCommands(PluginTabComplete.instence); cmdhandler.registerCommands(PluginTabComplete.instence);
} }
@HandlerCommand(name = "a")
public void a(final InvokeCommandEvent e) {
}
@HandlerCommand(name = "cmd", minimumArguments = 1, possibleArguments = "插件名称") @HandlerCommand(name = "cmd", minimumArguments = 1, possibleArguments = "插件名称")
public void cmd(final InvokeCommandEvent e) { public void cmd(final InvokeCommandEvent e) {
final String pname = e.getArgs()[0]; final String pname = e.getArgs()[0];
final CommandSender sender = e.getSender(); final CommandSender sender = e.getSender();
if (Bukkit.getPluginManager().getPlugin(pname) == null) { if (Bukkit.getPluginManager().getPlugin(pname) == null) {
sender.sendMessage(p_n_f); sender.sendMessage(String.format(p_n_f, pname));
return; return;
} }
final PluginManager pluginManager = Bukkit.getPluginManager(); final PluginManager pluginManager = Bukkit.getPluginManager();
@ -80,10 +77,10 @@ public class MonitorCommand implements HandlerCommands {
final CommandInjector injected = (CommandInjector) executor; final CommandInjector injected = (CommandInjector) executor;
final StringBuffer str = new StringBuffer(); final StringBuffer str = new StringBuffer();
str.append("§6- §e" + command.getValue().getName() + " "); str.append("§6- §e" + command.getValue().getName() + " ");
str.append(String.format(total, injected.totalTime / 1000000.0)); str.append(String.format(total, injected.totalTime / um));
str.append(String.format(count, injected.count)); str.append(String.format(count, injected.count));
if (injected.count != 0) { if (injected.count != 0) {
str.append(String.format(avg, injected.totalTime / 1000000.0 / injected.count)); str.append(String.format(avg, injected.totalTime / um / injected.count));
} }
e.getSender().sendMessage(str.toString()); e.getSender().sendMessage(str.toString());
} }
@ -96,7 +93,7 @@ public class MonitorCommand implements HandlerCommands {
final CommandSender sender = e.getSender(); final CommandSender sender = e.getSender();
final Plugin plugin = Bukkit.getPluginManager().getPlugin(pname); final Plugin plugin = Bukkit.getPluginManager().getPlugin(pname);
if (plugin == null) { if (plugin == null) {
sender.sendMessage(p_n_f); sender.sendMessage(String.format(p_n_f, pname));
return; return;
} }
sender.sendMessage(prefix + "§6插件 §b" + pname + " §6的事件能耗如下!"); sender.sendMessage(prefix + "§6插件 §b" + pname + " §6的事件能耗如下!");
@ -106,8 +103,8 @@ public class MonitorCommand implements HandlerCommands {
for (final RegisteredListener listener : listeners) { for (final RegisteredListener listener : listeners) {
if (listener instanceof TimedRegisteredListener) { if (listener instanceof TimedRegisteredListener) {
final TimedRegisteredListener trl = (TimedRegisteredListener) listener; final TimedRegisteredListener trl = (TimedRegisteredListener) listener;
eventTotalTime.put(trl.getEventClass().newInstance().getEventName(), trl.getTotalTime()); eventTotalTime.put(trl.getEventClass().getSimpleName(), trl.getTotalTime());
eventCount.put(trl.getEventClass().newInstance().getEventName(), trl.getCount()); eventCount.put(trl.getEventClass().getSimpleName(), trl.getCount());
continue; continue;
} }
final EventExecutor executor = Reflect.on(listener).get("executor"); final EventExecutor executor = Reflect.on(listener).get("executor");
@ -127,10 +124,10 @@ public class MonitorCommand implements HandlerCommands {
for (final String event : eventTotalTime.keySet()) { for (final String event : eventTotalTime.keySet()) {
final StringBuffer str = new StringBuffer(); final StringBuffer str = new StringBuffer();
str.append("§6- §e" + event + " "); str.append("§6- §e" + event + " ");
str.append(String.format(total, eventTotalTime.get(event) / 1000000.0)); str.append(String.format(total, eventTotalTime.get(event) / um));
str.append(String.format(count, eventCount.get(event))); str.append(String.format(count, eventCount.get(event)));
if (eventCount.get(event) != 0) { if (eventCount.get(event) != 0) {
str.append(String.format(avg, eventTotalTime.get(event) / 1000000.0 / eventCount.get(event))); str.append(String.format(avg, eventTotalTime.get(event) / um / eventCount.get(event)));
} }
e.getSender().sendMessage(str.toString()); e.getSender().sendMessage(str.toString());
} }
@ -142,7 +139,7 @@ public class MonitorCommand implements HandlerCommands {
final CommandSender sender = e.getSender(); final CommandSender sender = e.getSender();
final Plugin plugin = Bukkit.getPluginManager().getPlugin(pname); final Plugin plugin = Bukkit.getPluginManager().getPlugin(pname);
if (plugin == null) { if (plugin == null) {
sender.sendMessage(p_n_f); sender.sendMessage(String.format(p_n_f, pname));
return; return;
} }
final List<BukkitTask> pendingTasks = Bukkit.getScheduler().getPendingTasks(); final List<BukkitTask> pendingTasks = Bukkit.getScheduler().getPendingTasks();
@ -155,10 +152,10 @@ public class MonitorCommand implements HandlerCommands {
final StringBuffer str = new StringBuffer(); final StringBuffer str = new StringBuffer();
final Class<? extends Runnable> taskName = executor.getOriginalTask().getClass(); final Class<? extends Runnable> taskName = executor.getOriginalTask().getClass();
str.append("§6- §e" + (StrKit.isBlank(taskName.getSimpleName()) ? taskName.getName() : taskName.getSimpleName()) + " "); str.append("§6- §e" + (StrKit.isBlank(taskName.getSimpleName()) ? taskName.getName() : taskName.getSimpleName()) + " ");
str.append(String.format(total, executor.totalTime / 1000000.0)); str.append(String.format(total, executor.totalTime / um));
str.append(String.format(count, executor.count)); str.append(String.format(count, executor.count));
if (executor.count != 0) { if (executor.count != 0) {
str.append(String.format(avg, executor.totalTime / 1000000.0 / executor.count)); str.append(String.format(avg, executor.totalTime / um / executor.count));
} }
e.getSender().sendMessage(str.toString()); e.getSender().sendMessage(str.toString());
} }

View File

@ -84,7 +84,7 @@ public class CommandInjector implements TabExecutor {
@Override @Override
public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) { public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) {
final long start = System.nanoTime(); final long start = System.nanoTime();
// TODO add a more aggressive 10 ms cpu sample // TODO 当操作大于10ms的时候添加一个Lag提示
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;

View File

@ -65,12 +65,16 @@ public class ListenerInjector implements EventExecutor {
try { 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 当操作大于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;
if (lag / 1000000 > 10) {
PluginKit.sc("§6[§bYum §a能耗监控§6] §c注意! §6插件 §b" + plugin.getName() + " §6处理 §d" + event.getEventName() + " §6事件时§c耗时超过 §410ms!");
}
if (eventTotalTime.containsKey(en)) { if (eventTotalTime.containsKey(en)) {
eventTotalTime.put(en, eventTotalTime.get(en) + end - start); 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);
@ -83,7 +87,7 @@ public class ListenerInjector implements EventExecutor {
while (e.getCause() != null) { while (e.getCause() != null) {
e = e.getCause(); e = e.getCause();
} }
PluginKit.sc(prefix + "插件 §b" + plugin.getName() + " §6处理 " + event.getEventName() + " §a事件时发生异常 §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++) {

View File

@ -20,13 +20,13 @@ public class TaskInjector implements Runnable {
this.originalTask = originalTask; this.originalTask = originalTask;
} }
// sadly it works only with interval tasks // 当前注入只能对TimerTask有效
// for single runs we would have to register a dynamic proxy // 对于单次执行的任务 我们需要注册一个动态的代理
public static void inject(final Plugin plugin) { public static void inject(final Plugin plugin) {
final BukkitScheduler scheduler = Bukkit.getScheduler(); final BukkitScheduler scheduler = Bukkit.getScheduler();
final List<BukkitTask> pendingTasks = scheduler.getPendingTasks(); final List<BukkitTask> pendingTasks = scheduler.getPendingTasks();
for (final BukkitTask pendingTask : pendingTasks) { for (final BukkitTask pendingTask : pendingTasks) {
// we could ignore async tasks for now // 忽略异步任务
if (pendingTask.isSync() && pendingTask.getOwner().equals(plugin)) { if (pendingTask.isSync() && pendingTask.getOwner().equals(plugin)) {
final Runnable originalTask = Reflect.on(pendingTask).get("task"); final Runnable originalTask = Reflect.on(pendingTask).get("task");
if (originalTask instanceof TaskInjector) { if (originalTask instanceof TaskInjector) {
@ -42,7 +42,7 @@ public class TaskInjector implements Runnable {
final BukkitScheduler scheduler = Bukkit.getScheduler(); final BukkitScheduler scheduler = Bukkit.getScheduler();
final List<BukkitTask> pendingTasks = scheduler.getPendingTasks(); final List<BukkitTask> pendingTasks = scheduler.getPendingTasks();
for (final BukkitTask pendingTask : pendingTasks) { for (final BukkitTask pendingTask : pendingTasks) {
// we could ignore async tasks for now // 忽略异步任务
if (pendingTask.isSync() && pendingTask.getOwner().equals(plugin)) { if (pendingTask.isSync() && pendingTask.getOwner().equals(plugin)) {
final Runnable originalTask = Reflect.on(pendingTask).get("task"); final Runnable originalTask = Reflect.on(pendingTask).get("task");
if (originalTask instanceof TaskInjector) { if (originalTask instanceof TaskInjector) {
@ -59,7 +59,7 @@ public class TaskInjector implements Runnable {
@Override @Override
public void run() { public void run() {
final long start = System.nanoTime(); final long start = System.nanoTime();
// TODO add a more aggressive 10 ms cpu sample // TODO 当操作大于10ms的时候添加一个Lag提示
originalTask.run(); originalTask.run();
final long end = System.nanoTime(); final long end = System.nanoTime();
totalTime += end - start; totalTime += end - start;