Created
September 27, 2023 00:38
-
-
Save johnholdun/2c98a4090ec2a10f7653c824c8b96dee to your computer and use it in GitHub Desktop.
Benchmarking idempotent `CREATE IF NOT EXISTS` sqlite statements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs from 'fs' | |
import sqlite3 from 'sqlite3' | |
import { open } from 'sqlite' | |
const filename = './database.db' | |
if (fs.existsSync(filename)) { | |
fs.unlinkSync(filename) | |
} | |
const TABLE_COUNT = 100 | |
const RECORD_COUNT_PER_TABLE = 100000 | |
const now = () => new Date().valueOf() | |
const runMigration = (db) => { | |
const statements = [] | |
for (let i = 0; i < TABLE_COUNT; i += 1) { | |
statements.push(` | |
create table if not exists test_${i} ( | |
id integer primary key autoincrement, | |
name text | |
); | |
`) | |
} | |
return db.exec(statements.join('\n')) | |
} | |
const addData = async (db) => { | |
for (let i = 0; i < TABLE_COUNT; i += 1) { | |
const records = new Array(RECORD_COUNT_PER_TABLE).fill(null).map((_, i) => `('foobar ${i}')`) | |
await db.run(`insert into test_${i} (name) values ${records.join(', ')}`) | |
} | |
} | |
const getDuration = async (promise) => { | |
const startedAt = now() | |
await promise | |
return now() - startedAt | |
} | |
;(async () => { | |
const db = await open({ filename, driver: sqlite3.Database }) | |
console.log(`Running first migration to create ${TABLE_COUNT} ${TABLE_COUNT === 1 ? 'table' : 'tables'}…`) | |
const firstMigrationDuration = await getDuration(runMigration(db)) | |
console.log(`Finished in ${firstMigrationDuration}ms`) | |
console.log(`Adding ${RECORD_COUNT_PER_TABLE} ${RECORD_COUNT_PER_TABLE === 1 ? 'record' : 'records'} to each table…`) | |
const addDataDuration = await getDuration(addData(db)) | |
console.log(`Finished in ${addDataDuration}ms`) | |
console.log('Running second migration, no change to schema…') | |
const secondMigrationDuration = await getDuration(runMigration(db)) | |
console.log(`Finished in ${secondMigrationDuration}ms`) | |
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Running first migration to create 100 tables… | |
Finished in 88ms | |
Adding 100000 records to each table… | |
Finished in 17144ms | |
Running second migration, no change to schema… | |
Finished in 1ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment