From 05c2434efcaa1772bbfa677f8ce3f5d6a2c807a6 Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Sat, 4 Sep 2021 22:11:38 +0300 Subject: [PATCH] Add command to monitor plugin load performance --- .../plugins/BotControl/BotControlPlugin.ts | 3 +++ .../BotControl/commands/PerformanceCmd.ts | 23 +++++++++++++++++++ backend/src/plugins/BotControl/types.ts | 1 + 3 files changed, 27 insertions(+) create mode 100644 backend/src/plugins/BotControl/commands/PerformanceCmd.ts diff --git a/backend/src/plugins/BotControl/BotControlPlugin.ts b/backend/src/plugins/BotControl/BotControlPlugin.ts index dd53d72c..6770b8ef 100644 --- a/backend/src/plugins/BotControl/BotControlPlugin.ts +++ b/backend/src/plugins/BotControl/BotControlPlugin.ts @@ -18,11 +18,13 @@ import { ReloadServerCmd } from "./commands/ReloadServerCmd"; import { RemoveDashboardUserCmd } from "./commands/RemoveDashboardUserCmd"; import { ServersCmd } from "./commands/ServersCmd"; import { BotControlPluginType, ConfigSchema } from "./types"; +import { PerformanceCmd } from "./commands/PerformanceCmd"; const defaultOptions = { config: { can_use: false, can_eligible: false, + can_performance: false, update_cmd: null, }, }; @@ -45,6 +47,7 @@ export const BotControlPlugin = zeppelinGlobalPlugin()({ ListDashboardUsersCmd, ListDashboardPermsCmd, EligibleCmd, + PerformanceCmd, ], async afterLoad(pluginData) { diff --git a/backend/src/plugins/BotControl/commands/PerformanceCmd.ts b/backend/src/plugins/BotControl/commands/PerformanceCmd.ts new file mode 100644 index 00000000..6e4c857a --- /dev/null +++ b/backend/src/plugins/BotControl/commands/PerformanceCmd.ts @@ -0,0 +1,23 @@ +import { TextChannel } from "discord.js"; +import { commandTypeHelpers as ct } from "../../../commandTypes"; +import { sendErrorMessage, sendSuccessMessage } from "../../../pluginUtils"; +import { createChunkedMessage, formatNumber, resolveInvite, sorter, verboseUserMention } from "../../../utils"; +import { botControlCmd } from "../types"; + +export const PerformanceCmd = botControlCmd({ + trigger: ["performance"], + permission: "can_performance", + + signature: {}, + + async run({ pluginData, message: msg, args }) { + const stats = pluginData.getKnubInstance().getPluginPerformanceStats(); + const averageLoadTimeEntries = Object.entries(stats.averageLoadTimes); + averageLoadTimeEntries.sort(sorter(v => v[1].time, "DESC")); + const lines = averageLoadTimeEntries.map( + ([pluginName, { time }]) => `${pluginName}: **${formatNumber(Math.round(time))}ms**`, + ); + const fullStats = `Average plugin load times:\n\n${lines.join("\n")}`; + createChunkedMessage(msg.channel as TextChannel, fullStats); + }, +}); diff --git a/backend/src/plugins/BotControl/types.ts b/backend/src/plugins/BotControl/types.ts index 1acac3a4..44e57cf8 100644 --- a/backend/src/plugins/BotControl/types.ts +++ b/backend/src/plugins/BotControl/types.ts @@ -9,6 +9,7 @@ import { tNullable } from "../../utils"; export const ConfigSchema = t.type({ can_use: t.boolean, can_eligible: t.boolean, + can_performance: t.boolean, update_cmd: tNullable(t.string), }); export type TConfigSchema = t.TypeOf;