diff --git a/backend/bot.env.example b/backend/bot.env.example index 8a2956a3..7fdd7e5c 100644 --- a/backend/bot.env.example +++ b/backend/bot.env.example @@ -3,3 +3,4 @@ DB_HOST= DB_USER= DB_PASSWORD= DB_DATABASE= +PROFILING=false diff --git a/backend/src/index.ts b/backend/src/index.ts index a8d4236b..2d8bfc7a 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -32,6 +32,7 @@ import { runExpiredArchiveDeletionLoop } from "./data/loops/expiredArchiveDeleti import { runSavedMessageCleanupLoop } from "./data/loops/savedMessageCleanupLoop"; import { performance } from "perf_hooks"; import { setProfiler } from "./profiler"; +import { enableProfiling } from "./utils/easyProfiler"; if (!process.env.KEY) { // tslint:disable-next-line:no-console @@ -365,6 +366,9 @@ connect().then(async () => { }); setProfiler(bot.profiler); + if (process.env.PROFILING === "true") { + enableProfiling(); + } bot.initialize(); logger.info("Bot Initialized"); diff --git a/backend/src/plugins/Automod/events/runAutomodOnMessage.ts b/backend/src/plugins/Automod/events/runAutomodOnMessage.ts index a401de65..4b595036 100644 --- a/backend/src/plugins/Automod/events/runAutomodOnMessage.ts +++ b/backend/src/plugins/Automod/events/runAutomodOnMessage.ts @@ -7,6 +7,7 @@ import { clearRecentActionsForMessage } from "../functions/clearRecentActionsFor import { runAutomod } from "../functions/runAutomod"; import { AutomodContext, AutomodPluginType } from "../types"; import { performance } from "perf_hooks"; +import { profilingEnabled } from "../../../utils/easyProfiler"; export async function runAutomodOnMessage( pluginData: GuildPluginData, @@ -34,6 +35,10 @@ export async function runAutomodOnMessage( await runAutomod(pluginData, context); - pluginData.getKnubInstance().profiler.addDataPoint(`automod:${pluginData.guild.id}`, performance.now() - startTime); + if (profilingEnabled()) { + pluginData + .getKnubInstance() + .profiler.addDataPoint(`automod:${pluginData.guild.id}`, performance.now() - startTime); + } }); } diff --git a/backend/src/plugins/Automod/functions/runAutomod.ts b/backend/src/plugins/Automod/functions/runAutomod.ts index 4f323e07..7f298eb7 100644 --- a/backend/src/plugins/Automod/functions/runAutomod.ts +++ b/backend/src/plugins/Automod/functions/runAutomod.ts @@ -7,7 +7,7 @@ import { availableTriggers } from "../triggers/availableTriggers"; import { AutomodContext, AutomodPluginType } from "../types"; import { checkAndUpdateCooldown } from "./checkAndUpdateCooldown"; import { performance } from "perf_hooks"; -import { calculateBlocking } from "../../../utils/easyProfiler"; +import { calculateBlocking, profilingEnabled } from "../../../utils/easyProfiler"; export async function runAutomod(pluginData: GuildPluginData, context: AutomodContext) { const userId = context.user?.id || context.member?.id || context.message?.user_id; @@ -50,20 +50,28 @@ export async function runAutomod(pluginData: GuildPluginData, const triggerStartTime = performance.now(); const trigger = availableTriggers[triggerName]; - const getBlockingTime = calculateBlocking(); + + let getBlockingTime: ReturnType | null = null; + if (profilingEnabled()) { + getBlockingTime = calculateBlocking(); + } + matchResult = await trigger.match({ ruleName, pluginData, context, triggerConfig, }); - const blockingTime = getBlockingTime(); - pluginData - .getKnubInstance() - .profiler.addDataPoint( - `automod:${pluginData.guild.id}:${ruleName}:triggers:${triggerName}:blocking`, - blockingTime, - ); + + if (profilingEnabled()) { + const blockingTime = getBlockingTime?.() || 0; + pluginData + .getKnubInstance() + .profiler.addDataPoint( + `automod:${pluginData.guild.id}:${ruleName}:triggers:${triggerName}:blocking`, + blockingTime, + ); + } if (matchResult) { contexts = [context, ...(matchResult.extraContexts || [])]; @@ -95,12 +103,14 @@ export async function runAutomod(pluginData: GuildPluginData, matchResult.fullSummary = `Triggered automod rule **${ruleName}**\n${matchResult.summary}`.trim(); } - pluginData - .getKnubInstance() - .profiler.addDataPoint( - `automod:${pluginData.guild.id}:${ruleName}:triggers:${triggerName}`, - performance.now() - triggerStartTime, - ); + if (profilingEnabled()) { + pluginData + .getKnubInstance() + .profiler.addDataPoint( + `automod:${pluginData.guild.id}:${ruleName}:triggers:${triggerName}`, + performance.now() - triggerStartTime, + ); + } if (matchResult) { break triggerLoop; @@ -126,18 +136,22 @@ export async function runAutomod(pluginData: GuildPluginData, matchResult, }); - pluginData - .getKnubInstance() - .profiler.addDataPoint( - `automod:${pluginData.guild.id}:${ruleName}:actions:${actionName}`, - performance.now() - actionStartTime, - ); + if (profilingEnabled()) { + pluginData + .getKnubInstance() + .profiler.addDataPoint( + `automod:${pluginData.guild.id}:${ruleName}:actions:${actionName}`, + performance.now() - actionStartTime, + ); + } } } - pluginData - .getKnubInstance() - .profiler.addDataPoint(`automod:${pluginData.guild.id}:${ruleName}`, performance.now() - ruleStartTime); + if (profilingEnabled()) { + pluginData + .getKnubInstance() + .profiler.addDataPoint(`automod:${pluginData.guild.id}:${ruleName}`, performance.now() - ruleStartTime); + } if (matchResult && !rule.allow_further_rules) { break; diff --git a/backend/src/utils/easyProfiler.ts b/backend/src/utils/easyProfiler.ts index 18f74c45..b26c68b4 100644 --- a/backend/src/utils/easyProfiler.ts +++ b/backend/src/utils/easyProfiler.ts @@ -1,8 +1,26 @@ import { Profiler } from "knub/dist/Profiler"; import { performance } from "perf_hooks"; -import { SECONDS } from "../utils"; +import { SECONDS, noop } from "../utils"; + +let _profilingEnabled = false; + +export const profilingEnabled = () => { + return _profilingEnabled; +}; + +export const enableProfiling = () => { + _profilingEnabled = true; +}; + +export const disableProfiling = () => { + _profilingEnabled = false; +}; export const startProfiling = (profiler: Profiler, key: string) => { + if (!profilingEnabled()) { + return noop; + } + const startTime = performance.now(); return () => { profiler.addDataPoint(key, performance.now() - startTime); @@ -10,6 +28,10 @@ export const startProfiling = (profiler: Profiler, key: string) => { }; export const calculateBlocking = (coarseness = 10) => { + if (!profilingEnabled()) { + return () => 0; + } + let last = performance.now(); let result = 0; const interval = setInterval(() => {