From 310cd9d7d95a6ad465747b4e02f0f41e67436d2e Mon Sep 17 00:00:00 2001 From: kamecha Date: Fri, 9 Aug 2024 13:38:12 +0900 Subject: [PATCH] =?UTF-8?q?bench=E7=94=A8=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?=E3=82=82=E4=B8=80=E5=BF=9C=E3=81=82=E3=81=92=E3=81=A8=E3=81=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- denops/traqvim/message_bench.ts | 98 +++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 denops/traqvim/message_bench.ts diff --git a/denops/traqvim/message_bench.ts b/denops/traqvim/message_bench.ts new file mode 100644 index 0000000..47409b8 --- /dev/null +++ b/denops/traqvim/message_bench.ts @@ -0,0 +1,98 @@ +import { traq } from "./deps.ts"; +import { + channelMessageOptions, + channelTimeline, + channelUUID, + getStamp, + getUser, + homeChannelId, +} from "./model.ts"; +import { Message } from "./type.d.ts"; +import { api } from "./api.ts"; + +api.tokenFilePath = ""; + +let channelId: string; + +// $ deno bench --allow-read --allow-net -- --#gps/times/kamecha +if (Deno.args.length > 0) { + // --#gps/times/kamecha + const channelPath = Deno.args[0]; + // --#gps/times/kamecha -> ["gps", "times", "kamecha"] + const channelPathArray = channelPath.slice(3).split("/"); + channelId = await channelUUID(channelPathArray) ?? ""; +} else { + channelId = await homeChannelId(); +} + +const timelineOption: channelMessageOptions = { + id: channelId, + limit: 10, + until: new Date().toISOString(), + inclusive: true, + order: "desc", +}; + +const messages: Message[] = await channelTimeline(timelineOption); + +console.log("messages length:", messages.length); + +for (const message of messages) { + console.log("stamps length:", message.stamps.length); +} + +Deno.bench("user & stamp chache", async (b: Deno.BenchContext) => { + const userCache: Record = {}; + const stampCache: Record = {}; + b.start(); + for (const message of messages) { + for (const stamp of message.stamps) { + if (!stampCache[stamp.stampId]) { + stampCache[stamp.stampId] = await getStamp(stamp.stampId); + } + if (!userCache[stamp.userId]) { + userCache[stamp.userId] = await getUser(stamp.userId); + } + } + } + b.end(); +}); + +Deno.bench("stamp cache", async (b: Deno.BenchContext) => { + const stampCache: Record = {}; + b.start(); + for (const message of messages) { + for (const stamp of message.stamps) { + if (!stampCache[stamp.stampId]) { + stampCache[stamp.stampId] = await getStamp(stamp.stampId); + } + await getUser(stamp.userId); + } + } + b.end(); +}); + +Deno.bench("user chache", async (b: Deno.BenchContext) => { + const userCache: Record = {}; + b.start(); + for (const message of messages) { + for (const stamp of message.stamps) { + await getStamp(stamp.stampId); + if (!userCache[stamp.userId]) { + userCache[stamp.userId] = await getUser(stamp.userId); + } + } + } + b.end(); +}); + +Deno.bench("no chache", async (b: Deno.BenchContext) => { + b.start(); + for (const message of messages) { + for (const stamp of message.stamps) { + await getStamp(stamp.stampId); + await getUser(stamp.userId); + } + } + b.end(); +});