diff options
author | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2022-09-15 22:43:37 +1000 |
---|---|---|
committer | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2022-09-15 23:14:50 +1000 |
commit | 15940f4aa724be1aae3f03f7aafc1bc286de296a (patch) | |
tree | 6c454c06afe70ca5b666a9a61ff08d34a305c929 | |
parent | Allow empty content through block list (diff) | |
download | server-15940f4aa724be1aae3f03f7aafc1bc286de296a.tar.xz |
Perhaps a message create perf test?
-rw-r--r-- | slowcord/status/package.json | 3 | ||||
-rw-r--r-- | slowcord/status/src/gateway.ts | 82 | ||||
-rw-r--r-- | slowcord/status/src/index.ts | 27 |
3 files changed, 84 insertions, 28 deletions
diff --git a/slowcord/status/package.json b/slowcord/status/package.json index c6af6f65..ccec4f55 100644 --- a/slowcord/status/package.json +++ b/slowcord/status/package.json @@ -5,7 +5,8 @@ "main": "build/index.js", "scripts": { "build": "tsc -b", - "start": "node build/index.js" + "start": "node build/index.js", + "start:gateway": "node build/gateway.js" }, "repository": { "type": "git", diff --git a/slowcord/status/src/gateway.ts b/slowcord/status/src/gateway.ts new file mode 100644 index 00000000..14944d09 --- /dev/null +++ b/slowcord/status/src/gateway.ts @@ -0,0 +1,82 @@ +import "dotenv/config"; +import Fosscord from "fosscord-gopnik"; +import Discord from "discord.js"; +import mysql from "mysql2"; +import fetch from "node-fetch"; + +const dbConn = mysql.createConnection(process.env.DATABASE as string); +const executePromise = (sql: string, args: any[]) => new Promise((resolve, reject) => dbConn.execute(sql, args, (err, res) => { if (err) reject(err); else resolve(res); })); +const savePerf = async (time: number, name: string, error?: string | Error) => { + if (error && typeof error != "string") error = error.message; + try { + await executePromise("INSERT INTO performance (value, endpoint, timestamp, error) VALUES (?, ?, ?, ?)", [time ?? 0, name, new Date(), error ?? null]); + // await executePromise("DELETE FROM performance WHERE DATE(timestamp) < now() - interval ? DAY", [process.env.RETENTION_DAYS]); + } + catch (e) { + console.error(e); + } +}; + +var timestamp: number | undefined; + +const doMeasurements = async (channel: Discord.TextChannel) => { + timestamp = Date.now(); + await channel.send("hello this is a special message kthxbye"); + + setTimeout(doMeasurements, parseInt(process.env.MEASURE_INTERVAL as string), channel); +}; + +const instance = { + app: process.env.INSTANCE_WEB_APP as string, + api: process.env.INSTANCE_API as string, + cdn: process.env.INSTANCE_CDN as string, + token: process.env.INSTANCE_TOKEN as string, +}; + +const client = new Fosscord.Client({ + intents: [], + http: { + api: instance.api, + cdn: instance.cdn + } +}); + +client.on("ready", async () => { + console.log(`Ready on gateway as ${client.user!.tag}`); + + const channel = await client.channels.fetch("1019955729054267764"); + if (!channel) return; + + doMeasurements(channel as Discord.TextChannel); +}); + +client.on("messageCreate", async (msg: Discord.Message) => { + if (!timestamp) return; + if (msg.author.id != "992745947417141682" + || msg.channel.id != "1019955729054267764" + || msg.content != "hello this is a special message kthxbye") + return; + await savePerf(Date.now() - timestamp, "messageCreate", undefined); + timestamp = undefined; + + await fetch(`${instance.api}/channels/1019955729054267764/messages/${msg.id}`, { + method: "DELETE", + headers: { + authorization: instance.token + } + }) +}); + +client.on("error", (error: any) => { + console.log(`Gateway error`, error); +}); + +client.on("warn", (msg: any) => { + console.log(`Gateway warning:`, msg); +}); + +(async () => { + await new Promise((resolve) => dbConn.connect(resolve)); + console.log("Connected to db"); + await client.login(instance.token); +})(); \ No newline at end of file diff --git a/slowcord/status/src/index.ts b/slowcord/status/src/index.ts index b80893fa..735b8a9b 100644 --- a/slowcord/status/src/index.ts +++ b/slowcord/status/src/index.ts @@ -1,6 +1,5 @@ import "dotenv/config"; import https from "https"; -import Fosscord from "fosscord-gopnik"; import mysql from "mysql2"; import fetch from "node-fetch"; @@ -14,32 +13,6 @@ const instance = { token: process.env.INSTANCE_TOKEN as string, }; -const client = new Fosscord.Client({ - intents: [], - http: { - api: instance.api, - cdn: instance.cdn - } -}); - -const gatewayMeasure = async (name: string) => { - const time = Math.max(client.ws.ping, 0); - await savePerf(time, name, ''); - console.log(`${name} took ${time}ms`); -}; - -client.on("ready", () => { - console.log(`Ready on gateway as ${client.user!.tag}`); -}); - -client.on("error", (error: any) => { - console.log(`Gateway error`, error); -}); - -client.on("warn", (msg: any) => { - console.log(`Gateway warning:`, msg); -}); - const savePerf = async (time: number, name: string, error?: string | Error) => { if (error && typeof error != "string") error = error.message; try { |