Skip to content

Commit

Permalink
Add node:sqlite benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
weary-adventurer committed Feb 28, 2025
1 parent ea9a058 commit 88133ee
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 1 deletion.
9 changes: 9 additions & 0 deletions benchmark/drivers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@ module.exports = new Map([
return db;
}],
]);

const moduleExists = (m) => { try { return require.resolve(m); } catch (e) {} };
if (moduleExists('node:sqlite')) {
module.exports.set('node:sqlite', async (filename, pragma) => {
const db = new (require('node:sqlite').DatabaseSync)(filename, {open: true});
for (const str of pragma) db.exec(`PRAGMA ${str}`);
return db;
});
}
2 changes: 1 addition & 1 deletion benchmark/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ for (const trial of trials) {
const ctx = createContext(trial, driver);
process.stdout.write(`${driver} (running...)\n`);
try {
const result = execFileSync('node', ['./benchmark.js', ctx], { stdio: 'pipe', encoding: 'utf8' });
const result = execFileSync('node', [...process.execArgv, './benchmark.js', ctx], { stdio: 'pipe', encoding: 'utf8' });
console.log(erase() + `${driverName} x ${result}`);
} catch (err) {
console.log(erase() + clc.red(`${driverName} ERROR (probably out of memory)`));
Expand Down
11 changes: 11 additions & 0 deletions benchmark/types/insert.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,14 @@ exports['node-sqlite3'] = async (db, { table, columns }) => {
.map(([k, v]) => ({ ['@' + k]: v })));
return () => db.run(sql, row);
};

exports['node:sqlite'] = (db, { table, columns }) => {
const sql = `INSERT INTO ${table} (${columns.join(', ')}) VALUES (${columns.map(x => '@' + x).join(', ')})`;
const row = Object.assign({}, ...Object.entries(db.prepare(`SELECT * FROM ${table} LIMIT 1`).get())
.filter(([k]) => columns.includes(k))
.map(([k, v]) => ({ ['@' + k]: v })));
return () => {
const stmt = db.prepare(sql);
return stmt.run(row);
}
};
9 changes: 9 additions & 0 deletions benchmark/types/select-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,12 @@ exports['node-sqlite3'] = async (db, { table, columns, count }) => {
let rowid = -100;
return () => db.all(sql, (rowid += 100) % count + 1);
};

exports['node:sqlite'] = (db, { table, columns, count }) => {
const sql = `SELECT ${columns.join(', ')} FROM ${table} WHERE rowid >= ? LIMIT 100`;
let rowid = -100;
return () => {
const stmt = db.prepare(sql);
return stmt.all((rowid += 100) % count + 1);
}
};
16 changes: 16 additions & 0 deletions benchmark/types/select-iterate.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,19 @@ exports['node-sqlite3'] = async (db, { table, columns, count }) => {
})();
};
};

exports['node:sqlite'] = (db, { table, columns, count }) => {
const sql = `SELECT ${columns.join(', ')} FROM ${table} WHERE rowid >= ? LIMIT 100`;
let rowid = -100;

if (!("iterate" in require("node:sqlite").StatementSync.prototype)) {
// Error: StatementSync.iterate is not a function (added in Node v23.4.0+)
return () => {};
}

return () => {
// Error: statement has been finalized
// for (const row of db.prepare(sql).iterate((rowid += 100) % count + 1)) {}
return () => {};
};
};
9 changes: 9 additions & 0 deletions benchmark/types/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,12 @@ exports['node-sqlite3'] = async (db, { table, columns, count }) => {
let rowid = -1;
return () => db.get(sql, ++rowid % count + 1);
};

exports['node:sqlite'] = (db, { table, columns, count }) => {
const sql = `SELECT ${columns.join(', ')} FROM ${table} WHERE rowid = ?`;
let rowid = -1;
return () => {
const stmt = db.prepare(sql);
return stmt.get(++rowid % count + 1);
}
};
13 changes: 13 additions & 0 deletions benchmark/types/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,16 @@ exports['node-sqlite3'] = async (db, { table, columns, driver, pragma }) => {
}
});
};

exports['node:sqlite'] = (db, { table, columns }) => {
const sql = `INSERT INTO ${table} (${columns.join(', ')}) VALUES (${columns.map(x => '@' + x).join(', ')})`;
const row = Object.assign({}, ...Object.entries(db.prepare(`SELECT * FROM ${table} LIMIT 1`).get())
.filter(([k]) => columns.includes(k))
.map(([k, v]) => ({ ['@' + k]: v })));
return () => {
const stmt = db.prepare(sql);
db.exec(`BEGIN`);
for (let i = 0; i < 100; ++i) stmt.run(row);
db.exec(`COMMIT`);
}
};

0 comments on commit 88133ee

Please sign in to comment.