benchmark: sqlite prevent create both tables on prepare selects

PR-URL: https://github.com/nodejs/node/pull/59709
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
This commit is contained in:
Bruno Rodrigues 2025-09-03 16:18:05 -03:00 committed by GitHub
parent 97df3bb204
commit 5b32bb1573
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 52 additions and 38 deletions

View File

@ -26,25 +26,33 @@ const bench = common.createBenchmark(main, {
function main(conf) { function main(conf) {
const db = new sqlite.DatabaseSync(':memory:'); const db = new sqlite.DatabaseSync(':memory:');
db.exec('CREATE TABLE foo (text_column TEXT, integer_column INTEGER, real_column REAL, blob_column BLOB)'); // Create only the necessary table for the benchmark type.
const fooInsertStatement = db.prepare( // If the statement includes 'foo_large', create the foo_large table; otherwise, create the foo table.
'INSERT INTO foo (text_column, integer_column, real_column, blob_column) VALUES (?, ?, ?, ?)', if (conf.statement.includes('foo_large')) {
); db.exec('CREATE TABLE foo_large (text_8kb_column TEXT)');
const fooLargeInsertStatement = db.prepare(
for (let i = 0; i < conf.tableSeedSize; i++) { 'INSERT INTO foo_large (text_8kb_column) VALUES (?)',
fooInsertStatement.run( );
crypto.randomUUID(), const largeText = 'a'.repeat(8 * 1024);
Math.floor(Math.random() * 100), for (let i = 0; i < conf.tableSeedSize; i++) {
Math.random(), fooLargeInsertStatement.run(largeText);
Buffer.from('example blob data'), }
} else {
db.exec(
'CREATE TABLE foo (text_column TEXT, integer_column INTEGER, real_column REAL, blob_column BLOB)',
);
const fooInsertStatement = db.prepare(
'INSERT INTO foo (text_column, integer_column, real_column, blob_column) VALUES (?, ?, ?, ?)',
); );
}
db.exec('CREATE TABLE foo_large (text_8kb_column TEXT)'); for (let i = 0; i < conf.tableSeedSize; i++) {
const fooLargeInsertStatement = db.prepare('INSERT INTO foo_large (text_8kb_column) VALUES (?)'); fooInsertStatement.run(
const largeText = 'a'.repeat(8 * 1024); crypto.randomUUID(),
for (let i = 0; i < conf.tableSeedSize; i++) { Math.floor(Math.random() * 100),
fooLargeInsertStatement.run(largeText); Math.random(),
Buffer.from('example blob data'),
);
}
} }
let i; let i;
@ -53,8 +61,7 @@ function main(conf) {
const stmt = db.prepare(conf.statement); const stmt = db.prepare(conf.statement);
bench.start(); bench.start();
for (i = 0; i < conf.n; i += 1) for (i = 0; i < conf.n; i += 1) deadCodeElimination = stmt.all();
deadCodeElimination = stmt.all();
bench.end(conf.n); bench.end(conf.n);
assert.ok(deadCodeElimination !== undefined); assert.ok(deadCodeElimination !== undefined);

View File

@ -20,25 +20,33 @@ const bench = common.createBenchmark(main, {
function main(conf) { function main(conf) {
const db = new sqlite.DatabaseSync(':memory:'); const db = new sqlite.DatabaseSync(':memory:');
db.exec('CREATE TABLE foo (text_column TEXT, integer_column INTEGER, real_column REAL, blob_column BLOB)'); // Create only the necessary table for the benchmark type.
const fooInsertStatement = db.prepare( // If the statement includes 'foo_large', create the foo_large table; otherwise, create the foo table.
'INSERT INTO foo (text_column, integer_column, real_column, blob_column) VALUES (?, ?, ?, ?)', if (conf.statement.includes('foo_large')) {
); db.exec('CREATE TABLE foo_large (text_8kb_column TEXT)');
const fooLargeInsertStatement = db.prepare(
for (let i = 0; i < conf.tableSeedSize; i++) { 'INSERT INTO foo_large (text_8kb_column) VALUES (?)',
fooInsertStatement.run( );
crypto.randomUUID(), const largeText = 'a'.repeat(8 * 1024);
Math.floor(Math.random() * 100), for (let i = 0; i < conf.tableSeedSize; i++) {
Math.random(), fooLargeInsertStatement.run(largeText);
Buffer.from('example blob data'), }
} else {
db.exec(
'CREATE TABLE foo (text_column TEXT, integer_column INTEGER, real_column REAL, blob_column BLOB)',
);
const fooInsertStatement = db.prepare(
'INSERT INTO foo (text_column, integer_column, real_column, blob_column) VALUES (?, ?, ?, ?)',
); );
}
db.exec('CREATE TABLE foo_large (text_8kb_column TEXT)'); for (let i = 0; i < conf.tableSeedSize; i++) {
const fooLargeInsertStatement = db.prepare('INSERT INTO foo_large (text_8kb_column) VALUES (?)'); fooInsertStatement.run(
const largeText = 'a'.repeat(8 * 1024); crypto.randomUUID(),
for (let i = 0; i < conf.tableSeedSize; i++) { Math.floor(Math.random() * 100),
fooLargeInsertStatement.run(largeText); Math.random(),
Buffer.from('example blob data'),
);
}
} }
let i; let i;
@ -47,8 +55,7 @@ function main(conf) {
const stmt = db.prepare(conf.statement); const stmt = db.prepare(conf.statement);
bench.start(); bench.start();
for (i = 0; i < conf.n; i += 1) for (i = 0; i < conf.n; i += 1) deadCodeElimination = stmt.get();
deadCodeElimination = stmt.get();
bench.end(conf.n); bench.end(conf.n);
assert.ok(deadCodeElimination !== undefined); assert.ok(deadCodeElimination !== undefined);