Skip to content

Commit

Permalink
Add optimized version of droff benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Smart committed Mar 8, 2022
1 parent 44d2196 commit f28af9f
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 10 deletions.
5 changes: 3 additions & 2 deletions memory/droff.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ const droff = createClient({
// Load caches
const [guildsCache, guildsCache$] = droff.guildsCache();
const [membersCache, membersCache$] = droff.membersCache();
const [dmsCache, dmsCache$] = droff.directMessagesCache();
const [messagesCache, messagesCache$] = droff.messagesCache();
const [channelsCache, channelsCache$] = droff.channelsCache();

// Start client and caches
Rx.merge(droff.effects$, guildsCache$, messagesCache$, membersCache$, channelsCache$).subscribe();
Rx.merge(droff.effects$, guildsCache$, dmsCache$, messagesCache$, membersCache$, channelsCache$).subscribe();

// == Handlers

Expand All @@ -58,7 +59,7 @@ Rx.firstValueFrom(starttests$).then(() => {
"droff",
await guildsCache.size(),
await membersCache.size(),
await messagesCache.size(),
(await messagesCache.size()) + (await dmsCache.size()),
await channelsCache.size()
);
};
Expand Down
85 changes: 85 additions & 0 deletions memory/optimized/droff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const { createClient, Intents, CacheTTLStore } = require("droff");
const Rx = require("rxjs");
const RxO = require("rxjs/operators");
const { TOKEN, OWNER_ID } = require("../../configs-node");
const { READY, SHARD_READY, logMemory } = require("../../utils/events-node");

const droffStarted = Date.now();
let droffTime = Date.now();
let droffCounter = 0;

const droff = createClient({
token: TOKEN,
gateway: {
intents: Intents.DIRECT_MESSAGES | Intents.GUILD_MEMBERS | Intents.GUILD_MESSAGES | Intents.GUILDS,
},
});

// Load caches
const second = 1000;
const minute = 60 * second;

const [guildsCache, guildsCache$] = droff.guildsCache(
CacheTTLStore.createNonParent({
ttl: 60 * minute,
})
);
const [membersCache, membersCache$] = droff.membersCache(
CacheTTLStore.create({
ttl: 30 * minute,
strategy: "activity",
})
);
const [dmsCache, dmsCache$] = droff.directMessagesCache(
CacheTTLStore.createNonParent({
ttl: 10 * minute,
strategy: "activity",
})
);
const [messagesCache, messagesCache$] = droff.messagesCache(
CacheTTLStore.create({
ttl: 10 * minute,
strategy: "activity",
})
);
const [channelsCache, channelsCache$] = droff.channelsCache(
CacheTTLStore.create({
ttl: 61 * minute,
})
);

// Start client and caches
Rx.merge(droff.effects$, guildsCache$, dmsCache$, messagesCache$, membersCache$, channelsCache$).subscribe();

// == Handlers

droff.fromDispatchWithShard("READY").subscribe(([, shard]) => {
droffTime = SHARD_READY(shard.id[0], droffTime);
});

droff.gateway.shardsReady$.subscribe(() => {
READY(droffStarted);
});

const starttests$ = droff.fromDispatch("MESSAGE_CREATE").pipe(
RxO.filter((message) => message.content === "!starttests"),
RxO.filter((message) => message.author.id === OWNER_ID)
);

// We only care about the command once
Rx.firstValueFrom(starttests$).then(() => {
const tick = async () => {
droffCounter = logMemory(
process.memoryUsage(),
droffCounter,
"droff",
await guildsCache.size(),
await membersCache.size(),
(await dmsCache.size()) + (await messagesCache.size()),
await channelsCache.size()
);
};

setInterval(tick, 60000);
tick();
});
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@jay3332/js-cord": "^1.1.3",
"detritus-client": "^0.15.0",
"discord.js": "^12.5.3",
"droff": "^0.24.1",
"droff": "^0.27.0",
"eris": "^0.15.1",
"rxjs": "^7.5.4"
}
Expand Down

0 comments on commit f28af9f

Please sign in to comment.