mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-11-05 20:32:27 +00:00
refactor(dm-db): use singleton IndexedDB connection to avoid N+1 overhead
Co-authored-by: PR0M3TH3AN <[email protected]>
This commit is contained in:
1 parent
1fbee4219b
commit
03f4efbfeb
3 files changed
+108
-11
No files matched your search
@@ -0,0 +1,55 @@
|
||||
|
||||
import { performance } from "perf_hooks";
|
||||
|
||||
// Mock IndexedDB if running in Node
|
||||
if (!globalThis.indexedDB) {
|
||||
const { indexedDB, IDBKeyRange } = await import("fake-indexeddb");
|
||||
globalThis.indexedDB = indexedDB;
|
||||
globalThis.IDBKeyRange = IDBKeyRange;
|
||||
}
|
||||
|
||||
// Mock console to suppress logs
|
||||
const originalConsoleWarn = console.warn;
|
||||
console.warn = () => {};
|
||||
|
||||
// Import after mocking console
|
||||
const { writeMessages, clearDmDb } = await import("../js/storage/dmDb.js");
|
||||
|
||||
async function runBenchmark() {
|
||||
await clearDmDb();
|
||||
|
||||
const iterations = 1000;
|
||||
const messages = [];
|
||||
|
||||
for (let i = 0; i < iterations; i++) {
|
||||
messages.push({
|
||||
id: `msg-${i}`,
|
||||
conversation_id: "conv-1",
|
||||
sender_pubkey: "sender",
|
||||
receiver_pubkey: "receiver",
|
||||
created_at: Date.now(),
|
||||
kind: 4,
|
||||
content: `Message ${i}`,
|
||||
});
|
||||
}
|
||||
|
||||
console.log(`Starting benchmark with ${iterations} writes...`);
|
||||
|
||||
const start = performance.now();
|
||||
|
||||
// Simulate N+1 writes
|
||||
for (const msg of messages) {
|
||||
await writeMessages(msg);
|
||||
}
|
||||
|
||||
const end = performance.now();
|
||||
const duration = end - start;
|
||||
|
||||
console.log(`Total time: ${duration.toFixed(2)}ms`);
|
||||
console.log(`Average time per write: ${(duration / iterations).toFixed(2)}ms`);
|
||||
|
||||
// Restore console
|
||||
console.warn = originalConsoleWarn;
|
||||
}
|
||||
|
||||
runBenchmark().catch(console.error);
|
||||
Reference in new issue
Block a user