From 908da060644ad022f2947f40d3acdc445b66eebf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=99=E2=97=A6=20The=20Tablet=20=E2=9D=80=20GamerGirla?= =?UTF-8?q?ndCo=20=E2=97=A6=E2=9D=A7?= Date: Mon, 22 Jul 2024 23:55:49 -0400 Subject: [PATCH] benchmarks --- package.json | 4 +- src/main.ts | 158 +++++++++++++++++++++++++++++++++++---------------- yarn.lock | 19 +++++++ 3 files changed, 132 insertions(+), 49 deletions(-) diff --git a/package.json b/package.json index 5555fbd..e262d8b 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,8 @@ "esbuild-plugin-inline-worker": "^0.1.1" }, "dependencies": { - "@btfash/sqlite-wasm": "3.46.2" + "@btfash/sqlite-wasm": "3.46.2", + "esbuild-plugin-inline-worker": "^0.1.1", + "localforage": "^1.10.0" } } diff --git a/src/main.ts b/src/main.ts index 026007b..69db44c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,26 +1,33 @@ -import { - Notice, - Plugin, -} from "obsidian"; +import { Notice, Plugin } from "obsidian"; +import * as localforage from "localforage"; import TestWorker from "sqlite3.worker"; import { sqlite3Worker1Promiser } from "@btfash/sqlite-wasm/index.mjs"; // Remember to rename these classes and interfaces! - +interface Benchmark { + time: number; + name: string; +} export default class MyPlugin extends Plugin { + public persister: LocalForage; settings: any; private worker: Worker; private promiser: (...args: any[]) => any; - + public benches: Benchmark[] = []; + private dbId: string; async onload() { await this.loadSettings(); - console.log(import.meta); - - await this.initSqlite(); - await this.start(); - (window as any).sqlite3Promiser = this.promiser; + this.benches.push( + await this.time("sqlite-init", async () => await this.initSqlite()) + ); + await this.sqlite(); + this.benches.push( + await this.time("idb-init", async () => await this.initIDB()) + ); + await this.idb(); + console.table(this.benches) } onunload() { @@ -41,10 +48,28 @@ export default class MyPlugin extends Plugin { } locateFile(path: string, prefix: string): string { return this.app.vault.adapter.getResourcePath( - `${this.manifest.dir}/${path}`, + `${this.manifest.dir}/${path}` ); } + async time(benchName: string, func: () => Promise): Promise { + const start = performance.now(); + await func(); + const end = performance.now(); + return { + name: benchName, + time: end - start, + }; + } + + async initIDB() { + this.persister = localforage.createInstance({ + name: "sqlite-bench", + driver: localforage.INDEXEDDB, + description: "we do a little benchmarking", + }); + } + async initSqlite() { this.worker = new TestWorker(); let w = this.worker; @@ -64,44 +89,81 @@ export default class MyPlugin extends Plugin { worker: () => w, }); }); + (window as any).sqlite3Promiser = this.promiser; } - async start() { - let openRes = await this.promiser("open", { - filename: "/test.sqlite3", - vfs: "opfs", - }); - const { dbId } = openRes; - console.log(openRes); - try { - console.log("Creating a table..."); - let res = await this.promiser("exec", { - dbId, - sql: "DROP TABLE IF EXISTS abcdef; CREATE TABLE IF NOT EXISTS abcdef(a,b,c)", - returnValue: "resultRows", - }); - console.log("Result: ", res); - console.log("Insert some data using exec()..."); - for (let i = 20; i <= 25; ++i) { - let innerRes = await this.promiser("exec", { - dbId, - sql: "INSERT INTO abcdef(a,b,c) VALUES (?,?,69)", - bind: [i, i * 2], - returnValue: "resultRows", - rowMode: "object", - }); - console.log(innerRes); - } - console.log("Query data with exec()..."); - let rowRes = await this.promiser("exec", { - dbId, - sql: "SELECT * FROM abcdef ORDER BY a LIMIT 10", - returnValue: "resultRows", - rowMode: "object", - }); - console.log(rowRes); - } finally { - await this.promiser("close", { dbId }); + async doInsert(each: (s: number) => Promise) { + for (let i = 0; i < 50; i++) { + await each(i); } } + + async sqlite() { + this.benches.push( + await this.time("sqlite-open", async () => { + let openRes = await this.promiser("open", { + filename: "/test.sqlite3", + vfs: "opfs", + }); + const { dbId } = openRes; + this.dbId = dbId; + console.log(openRes); + }) + ); + try { + this.benches.push(await this.time("sqlite-create-table", async () => { + console.log("Creating a table..."); + let res = await this.promiser("exec", { + dbId: this.dbId, + sql: "DROP TABLE IF EXISTS abcdef; CREATE TABLE IF NOT EXISTS abcdef(a,b,c)", + returnValue: "resultRows", + }); + console.log("Result: ", res); + })); + + this.benches.push( + await this.time("sqlite-insert-many", async () => { + console.log("Insert some data using exec()..."); + await this.doInsert(async (i) => { + let innerRes = await this.promiser("exec", { + dbId: this.dbId, + sql: "INSERT INTO abcdef(a,b,c) VALUES (?,?,69)", + bind: [i, i * 2], + returnValue: "resultRows", + rowMode: "object", + }); + // console.log(innerRes); + }); + }) + ); + this.benches.push( + await this.time("sqlite-query-many", async () => { + console.log("Query data with exec()..."); + let rowRes = await this.promiser("exec", { + dbId: this.dbId, + sql: "SELECT * FROM abcdef ORDER BY a LIMIT 50", + returnValue: "resultRows", + rowMode: "object", + }); + console.log(rowRes); + }) + ); + } finally { + await this.promiser("close", { dbId: this.dbId }); + } + } + async idb() { + this.benches.push(await this.time("idb-insert-many", async() => { + await this.doInsert(async (s) => { + await this.persister.setItem("i/" +s, "brian tatler fucked and abused sean harris\n".repeat(Math.floor(s * 1.1))) + }) + })) + this.benches.push(await this.time("idb-query-many", async() => { + let q: any[] = []; + await this.doInsert(async (s) => { + q.push(await this.persister.getItem("i/" + s)) + }) + console.log("qlen", q.length) + })) + } } diff --git a/yarn.lock b/yarn.lock index 2998898..b95f6e0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -605,6 +605,11 @@ ignore@^5.2.0: resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.3.1.tgz#5073e554cd42c5b33b394375f538b8593e34d4ef" integrity sha512-5Fytz/IraMjqpwfd34ke28PTVMjZjJG2MPn5t7OE4eUCUNf8BAa7b5WUS9/Qvr6mwOQS7Mk6vdsMno5he+T8Xw== +immediate@~3.0.5: + version "3.0.6" + resolved "https://registry.yarnpkg.com/immediate/-/immediate-3.0.6.tgz#9db1dbd0faf8de6fbe0f5dd5e56bb606280de69b" + integrity sha512-XXOFtyqDjNDAQxVfYxuF7g9Il/IbWmmlQg2MYKOH8ExIT1qg6xc4zyS3HaEEATgs1btfzxq15ciUiY7gjSXRGQ== + is-extglob@^2.1.1: version "2.1.1" resolved "https://registry.yarnpkg.com/is-extglob/-/is-extglob-2.1.1.tgz#a88c02535791f02ed37c76a1b9ea9773c833f8c2" @@ -622,6 +627,20 @@ is-number@^7.0.0: resolved "https://registry.yarnpkg.com/is-number/-/is-number-7.0.0.tgz#7535345b896734d5f80c4d06c50955527a14f12b" integrity sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng== +lie@3.1.1: + version "3.1.1" + resolved "https://registry.yarnpkg.com/lie/-/lie-3.1.1.tgz#9a436b2cc7746ca59de7a41fa469b3efb76bd87e" + integrity sha512-RiNhHysUjhrDQntfYSfY4MU24coXXdEOgw9WGcKHNeEwffDYbF//u87M1EWaMGzuFoSbqW0C9C6lEEhDOAswfw== + dependencies: + immediate "~3.0.5" + +localforage@^1.10.0: + version "1.10.0" + resolved "https://registry.yarnpkg.com/localforage/-/localforage-1.10.0.tgz#5c465dc5f62b2807c3a84c0c6a1b1b3212781dd4" + integrity sha512-14/H1aX7hzBBmmh7sGPd+AOMkkIrHM3Z1PAyGgZigA1H1p5O5ANnMyWzvpAETtG68/dC4pC0ncy3+PPGzXZHPg== + dependencies: + lie "3.1.1" + locate-path@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-5.0.0.tgz#1afba396afd676a6d42504d0a67a3a7eb9f62aa0"