benchmarks

This commit is contained in:
parent cd399c2937
commit 908da06064
Signed by: tablet
GPG Key ID: 924A5F6AF051E87C
3 changed files with 132 additions and 49 deletions

@ -25,6 +25,8 @@
"esbuild-plugin-inline-worker": "^0.1.1" "esbuild-plugin-inline-worker": "^0.1.1"
}, },
"dependencies": { "dependencies": {
"@btfash/sqlite-wasm": "3.46.2" "@btfash/sqlite-wasm": "3.46.2",
"esbuild-plugin-inline-worker": "^0.1.1",
"localforage": "^1.10.0"
} }
} }

@ -1,26 +1,33 @@
import { import { Notice, Plugin } from "obsidian";
Notice, import * as localforage from "localforage";
Plugin,
} from "obsidian";
import TestWorker from "sqlite3.worker"; import TestWorker from "sqlite3.worker";
import { sqlite3Worker1Promiser } from "@btfash/sqlite-wasm/index.mjs"; import { sqlite3Worker1Promiser } from "@btfash/sqlite-wasm/index.mjs";
// Remember to rename these classes and interfaces! // Remember to rename these classes and interfaces!
interface Benchmark {
time: number;
name: string;
}
export default class MyPlugin extends Plugin { export default class MyPlugin extends Plugin {
public persister: LocalForage;
settings: any; settings: any;
private worker: Worker; private worker: Worker;
private promiser: (...args: any[]) => any; private promiser: (...args: any[]) => any;
public benches: Benchmark[] = [];
private dbId: string;
async onload() { async onload() {
await this.loadSettings(); await this.loadSettings();
console.log(import.meta); this.benches.push(
await this.time("sqlite-init", async () => await this.initSqlite())
await this.initSqlite(); );
await this.start(); await this.sqlite();
(window as any).sqlite3Promiser = this.promiser; this.benches.push(
await this.time("idb-init", async () => await this.initIDB())
);
await this.idb();
console.table(this.benches)
} }
onunload() { onunload() {
@ -41,10 +48,28 @@ export default class MyPlugin extends Plugin {
} }
locateFile(path: string, prefix: string): string { locateFile(path: string, prefix: string): string {
return this.app.vault.adapter.getResourcePath( return this.app.vault.adapter.getResourcePath(
`${this.manifest.dir}/${path}`, `${this.manifest.dir}/${path}`
); );
} }
async time(benchName: string, func: () => Promise<void>): Promise<Benchmark> {
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() { async initSqlite() {
this.worker = new TestWorker(); this.worker = new TestWorker();
let w = this.worker; let w = this.worker;
@ -64,44 +89,81 @@ export default class MyPlugin extends Plugin {
worker: () => w, worker: () => w,
}); });
}); });
(window as any).sqlite3Promiser = this.promiser;
} }
async start() { async doInsert(each: (s: number) => Promise<void>) {
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", { let openRes = await this.promiser("open", {
filename: "/test.sqlite3", filename: "/test.sqlite3",
vfs: "opfs", vfs: "opfs",
}); });
const { dbId } = openRes; const { dbId } = openRes;
this.dbId = dbId;
console.log(openRes); console.log(openRes);
})
);
try { try {
this.benches.push(await this.time("sqlite-create-table", async () => {
console.log("Creating a table..."); console.log("Creating a table...");
let res = await this.promiser("exec", { let res = await this.promiser("exec", {
dbId, dbId: this.dbId,
sql: "DROP TABLE IF EXISTS abcdef; CREATE TABLE IF NOT EXISTS abcdef(a,b,c)", sql: "DROP TABLE IF EXISTS abcdef; CREATE TABLE IF NOT EXISTS abcdef(a,b,c)",
returnValue: "resultRows", returnValue: "resultRows",
}); });
console.log("Result: ", res); console.log("Result: ", res);
}));
this.benches.push(
await this.time("sqlite-insert-many", async () => {
console.log("Insert some data using exec()..."); console.log("Insert some data using exec()...");
for (let i = 20; i <= 25; ++i) { await this.doInsert(async (i) => {
let innerRes = await this.promiser("exec", { let innerRes = await this.promiser("exec", {
dbId, dbId: this.dbId,
sql: "INSERT INTO abcdef(a,b,c) VALUES (?,?,69)", sql: "INSERT INTO abcdef(a,b,c) VALUES (?,?,69)",
bind: [i, i * 2], bind: [i, i * 2],
returnValue: "resultRows", returnValue: "resultRows",
rowMode: "object", rowMode: "object",
}); });
console.log(innerRes); // console.log(innerRes);
} });
})
);
this.benches.push(
await this.time("sqlite-query-many", async () => {
console.log("Query data with exec()..."); console.log("Query data with exec()...");
let rowRes = await this.promiser("exec", { let rowRes = await this.promiser("exec", {
dbId, dbId: this.dbId,
sql: "SELECT * FROM abcdef ORDER BY a LIMIT 10", sql: "SELECT * FROM abcdef ORDER BY a LIMIT 50",
returnValue: "resultRows", returnValue: "resultRows",
rowMode: "object", rowMode: "object",
}); });
console.log(rowRes); console.log(rowRes);
})
);
} finally { } finally {
await this.promiser("close", { dbId }); 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)
}))
}
} }

@ -605,6 +605,11 @@ ignore@^5.2.0:
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.3.1.tgz#5073e554cd42c5b33b394375f538b8593e34d4ef" resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.3.1.tgz#5073e554cd42c5b33b394375f538b8593e34d4ef"
integrity sha512-5Fytz/IraMjqpwfd34ke28PTVMjZjJG2MPn5t7OE4eUCUNf8BAa7b5WUS9/Qvr6mwOQS7Mk6vdsMno5he+T8Xw== 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: is-extglob@^2.1.1:
version "2.1.1" version "2.1.1"
resolved "https://registry.yarnpkg.com/is-extglob/-/is-extglob-2.1.1.tgz#a88c02535791f02ed37c76a1b9ea9773c833f8c2" 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" resolved "https://registry.yarnpkg.com/is-number/-/is-number-7.0.0.tgz#7535345b896734d5f80c4d06c50955527a14f12b"
integrity sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng== 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: locate-path@^5.0.0:
version "5.0.0" version "5.0.0"
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-5.0.0.tgz#1afba396afd676a6d42504d0a67a3a7eb9f62aa0" resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-5.0.0.tgz#1afba396afd676a6d42504d0a67a3a7eb9f62aa0"