-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
272 lines (242 loc) · 7.64 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import { Sabr, SabrTable } from "https://deno.land/x/[email protected]/mod.ts";
console.log(`[INFO] Script started.`);
const sabr = new Sabr();
try {
await Deno.readDir("db");
sabr.directoryPath = `db/`;
} catch {
sabr.directoryPath = `${Deno.cwd()}/benchmarks/db/`;
}
// Creates a db object that can be imported in other files.
export const db = {
// This will allow us to access table methods easily as we will see below.
sabr,
// Sets up a table. If this table did not exist, it will create one.
events: new SabrTable(sabr, "events"),
};
// This is important as it prepares all the tables.
await sabr.init();
console.log(`[INFO] Sabr DB has been initialized.`);
export async function memoryBenchmarks(
botCreator: () => any,
options: { times: number; log: boolean; table: boolean } = {
times: 3,
log: false,
table: true,
},
) {
let gcEnable = false;
let garbageCollect = () => {};
try {
//@ts-ignore
gc();
gcEnable = true;
} catch (error) {
if (error.message === "gc is not defined") {
console.error(
`[WARN] add the flag '--v8-flags="--expose-gc"' for higher accuracy, or change options.times to 1`,
);
}
}
//@ts-ignore
if (gcEnable) garbageCollect = gc;
const stages = ["start", "loaded", "end", "cached"] as const;
const typeOfMemUsages = ["rss", "heapUsed", "heapTotal"] as const;
async function runTest(bot: any) {
// Determine memory stats now before touching anything
const results: {
start: Deno.MemoryUsage;
loaded?: Deno.MemoryUsage;
end?: Deno.MemoryUsage;
cached?: Deno.MemoryUsage;
} = {
start: Deno.memoryUsage(),
};
garbageCollect();
results.start = Deno.memoryUsage();
if (options.log) console.log(`[INFO] Loading json files.`);
const events = await db.events.getAll(true);
if (options.log) {
console.log(`[INFO] DB files loaded into memory.`, events.length);
}
// Set the memory stats for when files are loaded in.
results.loaded = Deno.memoryUsage();
let counter = 0;
for (let i = 0; i < events.length; i++) {
const e = events[i];
for (
// @ts-ignore should be fine
const event of Object.values(e) as (string | {
shardId: number;
// the d in DiscordGatewayPayload is {}
payload: any; // DiscordGatewayPayload
})[]
) {
// In db there is some weird id: "1561" event, this filters it
if (typeof event === "string") continue;
counter++;
try {
// Turn all hash into a known working hash, make guild iconHashToBigInt working
if (event.payload.d !== null && event.payload.d) {
if ("icon" in event.payload.d) {
event.payload.d.icon = "eae5905ad2d18d7c8deca20478b088b5";
}
if ("discovery_splash" in event.payload.d) {
event.payload.d.discovery_splash =
"eae5905ad2d18d7c8deca20478b088b5";
}
if ("banner" in event.payload.d) {
event.payload.d.banner = "eae5905ad2d18d7c8deca20478b088b5";
}
if ("splash" in event.payload.d) {
event.payload.d.splash = "eae5905ad2d18d7c8deca20478b088b5";
}
}
if (event.payload.t) {
bot.handlers[event.payload.t as any]?.(
bot,
event.payload,
event.shardId,
);
}
} catch (error) {
console.log(event);
console.log("erroring in benchmark", error);
}
}
}
if (options.log) {
console.log(`[INFO] Processed ${counter.toLocaleString()} events.`);
}
// Set results for data once all events are processed
results.end = Deno.memoryUsage();
//@ts-ignore
results.cached = {};
for (const typeOfMemUsage of typeOfMemUsages) {
results.cached![typeOfMemUsage] = results.end![typeOfMemUsage] -
results.loaded![typeOfMemUsage];
}
if (options.log) {
console.log(
"channels",
bot.channels.size.toLocaleString(),
"guilds",
bot.guilds.size.toLocaleString(),
"members",
bot.members.size.toLocaleString(),
"users",
bot.users.size.toLocaleString(),
"messages",
bot.messages.size.toLocaleString(),
"presences",
bot.presences.size.toLocaleString(),
);
}
return results;
}
const allResults = {
start: {
rss: [] as number[],
heapUsed: [] as number[],
heapTotal: [] as number[],
},
loaded: {
rss: [] as number[],
heapUsed: [] as number[],
heapTotal: [] as number[],
},
end: {
rss: [] as number[],
heapUsed: [] as number[],
heapTotal: [] as number[],
},
cached: {
rss: [] as number[],
heapUsed: [] as number[],
heapTotal: [] as number[],
},
};
const BYTES = 1000000;
for (let index = 0; index < options.times; index++) {
if (options.log) console.log("running the", index + 1, "time");
const currentResult = await runTest(botCreator());
for (const typeOfMemUsage of typeOfMemUsages) {
for (const stage of stages) {
allResults[stage][typeOfMemUsage].push(
currentResult[stage]![typeOfMemUsage],
);
}
}
}
type ArrayElement<ArrayType extends readonly unknown[]> = ArrayType extends
readonly (infer ElementType)[] ? ElementType : never;
const tableRows = ["Starting", "Loaded", "End", "Cached"] as const;
const tableFields = ["RSS", "Heap Used", "Heap Total"] as const;
const preprocessedResults: {
[K in ArrayElement<typeof tableRows>]?: {
[K in ArrayElement<typeof tableFields>]?: {
value: number;
min: number;
max: number;
};
};
} = {};
for (const [index, tableRow] of tableRows.entries()) {
for (const [index2, tableField] of tableFields.entries()) {
if (index2 === 0) preprocessedResults[tableRow] = {};
preprocessedResults[tableRow]![tableField] = {
value: Math.round(
allResults[stages[index]][typeOfMemUsages[index2]].reduce(
(acc, c) => acc + c,
0,
) / allResults.start.rss.length / BYTES * 100,
) / 100,
min: Math.round(
Math.min(...allResults[stages[index]][typeOfMemUsages[index2]]) /
BYTES * 100,
) / 100,
max: Math.round(
Math.max(...allResults[stages[index]][typeOfMemUsages[index2]]) /
BYTES * 100,
) / 100,
};
}
}
const processedResults = preprocessedResults as {
[K in ArrayElement<typeof tableRows>]: {
[K in ArrayElement<typeof tableFields>]: {
value: number;
min: number;
max: number;
};
};
};
const humanReadable: {
[K in ArrayElement<typeof tableRows>]?: {
[K in ArrayElement<typeof tableFields>]?: string;
};
} = {};
for (const tableRow of tableRows) {
for (const [index, tableField] of tableFields.entries()) {
if (index === 0) humanReadable[tableRow] = {};
humanReadable[tableRow]![tableField] = `${
processedResults[tableRow][tableField].value
} MB (${processedResults[tableRow][tableField].min} MB … ${
processedResults[tableRow][tableField].max
} MB)`;
}
}
if (options.table) console.table(humanReadable);
return processedResults;
}
/* Example Usage
deno run --v8-flags="--expose-gc" -A .\index.ts
*/
/*
import { createBot } from "https://deno.land/x/[email protected]/mod.ts";
import { enableCachePlugin } from "https://deno.land/x/[email protected]/plugins/mod.ts";
memoryBenchmarks(() => enableCachePlugin(createBot({
token: " ",
botId: 0n,
})))
*/