2024-12-12 16:34:09 +05:30
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
|
|
// deno-lint-ignore-file no-console
|
|
|
|
|
|
|
|
import { DatabaseSync } from "node:sqlite";
|
|
|
|
import fs from "node:fs";
|
|
|
|
|
|
|
|
function bench(name, fun, count = 10000) {
|
|
|
|
const start = Date.now();
|
|
|
|
for (let i = 0; i < count; i++) fun();
|
|
|
|
const elapsed = Date.now() - start;
|
|
|
|
const rate = Math.floor(count / (elapsed / 1000));
|
|
|
|
console.log(` ${name}: time ${elapsed} ms rate ${rate}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const name of [":memory:", "test.db"]) {
|
|
|
|
console.log(`Benchmarking ${name}`);
|
|
|
|
try {
|
|
|
|
fs.unlinkSync(name);
|
2024-12-15 08:58:30 +05:30
|
|
|
} catch {
|
2024-12-12 16:34:09 +05:30
|
|
|
// Ignore
|
|
|
|
}
|
|
|
|
|
|
|
|
const db = new DatabaseSync(name);
|
|
|
|
db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)");
|
|
|
|
|
|
|
|
bench("prepare", () => db.prepare("SELECT * FROM test"));
|
|
|
|
bench("exec", () => db.exec("INSERT INTO test (name) VALUES ('foo')"));
|
|
|
|
|
|
|
|
const stmt = db.prepare("SELECT * FROM test");
|
|
|
|
bench("get", () => stmt.get());
|
|
|
|
|
|
|
|
const stmt2 = db.prepare("SELECT * FROM test WHERE id = ?");
|
|
|
|
bench("get (integer bind)", () => stmt2.get(1));
|
|
|
|
|
|
|
|
bench("all", () => stmt.all(), 1000);
|
|
|
|
}
|