mirror of
https://e.coding.net/circlecloud/Yum.git
synced 2024-12-22 20:58:47 +00:00
feat: 更新至2.6 完善能耗监控 添加事件Lag提示
Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
parent
d029f96c31
commit
9746c2ea28
5
pom.xml
5
pom.xml
@ -3,7 +3,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>pw.yumc</groupId>
|
||||
<artifactId>Yum</artifactId>
|
||||
<version>2.5.1</version>
|
||||
<version>2.6</version>
|
||||
<name>Yum</name>
|
||||
<description>Minecraft 服务器插件管理系统</description>
|
||||
<build>
|
||||
@ -60,10 +60,9 @@
|
||||
<properties>
|
||||
<update.description>&a全新 2.X 版本 更多守护与优化</update.description>
|
||||
<update.changes>
|
||||
&b2.5.1 &6- &b添加能耗监控系统(Bate)...;
|
||||
&b2.6 &6- &a添加能耗监控系统 事件错误监控...;
|
||||
&b2.5 &6- &d修复主线程守护系统错误...;
|
||||
&b2.4 &6- &c修复重载插件导致线程安全错误的问题...;
|
||||
&b2.3 &6- &a网络监控添加域名白名单功能...;
|
||||
</update.changes>
|
||||
<env.GIT_COMMIT>DEBUG</env.GIT_COMMIT>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
|
@ -37,10 +37,12 @@ import pw.yumc.Yum.inject.TaskInjector;
|
||||
*/
|
||||
public class MonitorCommand implements HandlerCommands {
|
||||
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 avg = "§6平均耗时: §d%.5f秒!";
|
||||
private final String p_n_f = prefix + "§c插件不存在!";
|
||||
private final String avg = "§6平均耗时: §d%.5f毫秒!";
|
||||
private final String p_n_f = prefix + "§c插件 §b%s §c不存在!";
|
||||
|
||||
private final double um = 1000000.0;
|
||||
|
||||
public MonitorCommand(final Yum yum) {
|
||||
final InvokeSubCommand cmdhandler = new InvokeSubCommand(yum, "monitor");
|
||||
@ -48,17 +50,12 @@ public class MonitorCommand implements HandlerCommands {
|
||||
cmdhandler.registerCommands(PluginTabComplete.instence);
|
||||
}
|
||||
|
||||
@HandlerCommand(name = "a")
|
||||
public void a(final InvokeCommandEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@HandlerCommand(name = "cmd", minimumArguments = 1, possibleArguments = "插件名称")
|
||||
public void cmd(final InvokeCommandEvent e) {
|
||||
final String pname = e.getArgs()[0];
|
||||
final CommandSender sender = e.getSender();
|
||||
if (Bukkit.getPluginManager().getPlugin(pname) == null) {
|
||||
sender.sendMessage(p_n_f);
|
||||
sender.sendMessage(String.format(p_n_f, pname));
|
||||
return;
|
||||
}
|
||||
final PluginManager pluginManager = Bukkit.getPluginManager();
|
||||
@ -80,10 +77,10 @@ public class MonitorCommand implements HandlerCommands {
|
||||
final CommandInjector injected = (CommandInjector) executor;
|
||||
final StringBuffer str = new StringBuffer();
|
||||
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));
|
||||
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());
|
||||
}
|
||||
@ -96,7 +93,7 @@ public class MonitorCommand implements HandlerCommands {
|
||||
final CommandSender sender = e.getSender();
|
||||
final Plugin plugin = Bukkit.getPluginManager().getPlugin(pname);
|
||||
if (plugin == null) {
|
||||
sender.sendMessage(p_n_f);
|
||||
sender.sendMessage(String.format(p_n_f, pname));
|
||||
return;
|
||||
}
|
||||
sender.sendMessage(prefix + "§6插件 §b" + pname + " §6的事件能耗如下!");
|
||||
@ -106,8 +103,8 @@ public class MonitorCommand implements HandlerCommands {
|
||||
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());
|
||||
eventTotalTime.put(trl.getEventClass().getSimpleName(), trl.getTotalTime());
|
||||
eventCount.put(trl.getEventClass().getSimpleName(), trl.getCount());
|
||||
continue;
|
||||
}
|
||||
final EventExecutor executor = Reflect.on(listener).get("executor");
|
||||
@ -127,10 +124,10 @@ public class MonitorCommand implements HandlerCommands {
|
||||
for (final String event : eventTotalTime.keySet()) {
|
||||
final StringBuffer str = new StringBuffer();
|
||||
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)));
|
||||
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());
|
||||
}
|
||||
@ -142,7 +139,7 @@ public class MonitorCommand implements HandlerCommands {
|
||||
final CommandSender sender = e.getSender();
|
||||
final Plugin plugin = Bukkit.getPluginManager().getPlugin(pname);
|
||||
if (plugin == null) {
|
||||
sender.sendMessage(p_n_f);
|
||||
sender.sendMessage(String.format(p_n_f, pname));
|
||||
return;
|
||||
}
|
||||
final List<BukkitTask> pendingTasks = Bukkit.getScheduler().getPendingTasks();
|
||||
@ -155,10 +152,10 @@ public class MonitorCommand implements HandlerCommands {
|
||||
final StringBuffer str = new StringBuffer();
|
||||
final Class<? extends Runnable> taskName = executor.getOriginalTask().getClass();
|
||||
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));
|
||||
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());
|
||||
}
|
||||
|
@ -84,7 +84,7 @@ public class CommandInjector implements TabExecutor {
|
||||
@Override
|
||||
public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) {
|
||||
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 long end = System.nanoTime();
|
||||
totalTime += end - start;
|
||||
|
@ -65,12 +65,16 @@ public class ListenerInjector implements EventExecutor {
|
||||
try {
|
||||
if (!event.isAsynchronous()) {
|
||||
final long start = System.nanoTime();
|
||||
// TODO add a more aggressive 10 ms cpu sample
|
||||
// TODO 当操作大于10ms的时候添加一个Lag提示
|
||||
originalExecutor.execute(listener, event);
|
||||
final long end = System.nanoTime();
|
||||
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)) {
|
||||
eventTotalTime.put(en, eventTotalTime.get(en) + end - start);
|
||||
eventTotalTime.put(en, eventTotalTime.get(en) + lag);
|
||||
eventCount.put(en, eventCount.get(en) + 1);
|
||||
} else {
|
||||
eventTotalTime.put(en, end - start);
|
||||
@ -83,7 +87,7 @@ public class ListenerInjector implements EventExecutor {
|
||||
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(prefix + "§6插件 §b" + plugin.getName() + " §6处理 §d" + event.getEventName() + " §6事件时发生异常 §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++) {
|
||||
|
@ -20,13 +20,13 @@ public class TaskInjector implements Runnable {
|
||||
this.originalTask = originalTask;
|
||||
}
|
||||
|
||||
// sadly it works only with interval tasks
|
||||
// for single runs we would have to register a dynamic proxy
|
||||
// 当前注入只能对TimerTask有效
|
||||
// 对于单次执行的任务 我们需要注册一个动态的代理
|
||||
public static void inject(final Plugin plugin) {
|
||||
final BukkitScheduler scheduler = Bukkit.getScheduler();
|
||||
final List<BukkitTask> pendingTasks = scheduler.getPendingTasks();
|
||||
for (final BukkitTask pendingTask : pendingTasks) {
|
||||
// we could ignore async tasks for now
|
||||
// 忽略异步任务
|
||||
if (pendingTask.isSync() && pendingTask.getOwner().equals(plugin)) {
|
||||
final Runnable originalTask = Reflect.on(pendingTask).get("task");
|
||||
if (originalTask instanceof TaskInjector) {
|
||||
@ -42,7 +42,7 @@ public class TaskInjector implements Runnable {
|
||||
final BukkitScheduler scheduler = Bukkit.getScheduler();
|
||||
final List<BukkitTask> pendingTasks = scheduler.getPendingTasks();
|
||||
for (final BukkitTask pendingTask : pendingTasks) {
|
||||
// we could ignore async tasks for now
|
||||
// 忽略异步任务
|
||||
if (pendingTask.isSync() && pendingTask.getOwner().equals(plugin)) {
|
||||
final Runnable originalTask = Reflect.on(pendingTask).get("task");
|
||||
if (originalTask instanceof TaskInjector) {
|
||||
@ -59,7 +59,7 @@ public class TaskInjector implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
final long start = System.nanoTime();
|
||||
// TODO add a more aggressive 10 ms cpu sample
|
||||
// TODO 当操作大于10ms的时候添加一个Lag提示
|
||||
originalTask.run();
|
||||
final long end = System.nanoTime();
|
||||
totalTime += end - start;
|
||||
|
Loading…
Reference in New Issue
Block a user