mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-15 05:41:51 +00:00
feat: add env var to enable profiling
This commit is contained in:
parent
831ff1893a
commit
bb94d77143
5 changed files with 72 additions and 26 deletions
|
@ -3,3 +3,4 @@ DB_HOST=
|
|||
DB_USER=
|
||||
DB_PASSWORD=
|
||||
DB_DATABASE=
|
||||
PROFILING=false
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -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<AutomodPluginType>,
|
||||
|
@ -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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -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<AutomodPluginType>, context: AutomodContext) {
|
||||
const userId = context.user?.id || context.member?.id || context.message?.user_id;
|
||||
|
@ -50,20 +50,28 @@ export async function runAutomod(pluginData: GuildPluginData<AutomodPluginType>,
|
|||
const triggerStartTime = performance.now();
|
||||
|
||||
const trigger = availableTriggers[triggerName];
|
||||
const getBlockingTime = calculateBlocking();
|
||||
|
||||
let getBlockingTime: ReturnType<typeof calculateBlocking> | 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<AutomodPluginType>,
|
|||
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<AutomodPluginType>,
|
|||
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;
|
||||
|
|
|
@ -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(() => {
|
||||
|
|
Loading…
Add table
Reference in a new issue