diff --git a/benchmark/sqlite/sqlite-is-transaction.js b/benchmark/sqlite/sqlite-is-transaction.js new file mode 100644 index 00000000000000..e3325ccd3d10bf --- /dev/null +++ b/benchmark/sqlite/sqlite-is-transaction.js @@ -0,0 +1,31 @@ +'use strict'; +const common = require('../common.js'); +const sqlite = require('node:sqlite'); +const assert = require('assert'); + +const bench = common.createBenchmark(main, { + n: [1e7], + transaction: ['true', 'false'], +}); + +function main(conf) { + const db = new sqlite.DatabaseSync(':memory:'); + + if (conf.transaction === 'true') { + db.exec('BEGIN'); + } + + let i; + let deadCodeElimination; + + bench.start(); + for (i = 0; i < conf.n; i += 1) + deadCodeElimination &&= db.isTransaction; + bench.end(conf.n); + + assert.ok(deadCodeElimination === (conf.transaction === 'true')); + + if (conf.transaction === 'true') { + db.exec('ROLLBACK'); + } +} diff --git a/benchmark/sqlite/sqlite-prepare-insert.js b/benchmark/sqlite/sqlite-prepare-insert.js new file mode 100644 index 00000000000000..58e97274c98fc4 --- /dev/null +++ b/benchmark/sqlite/sqlite-prepare-insert.js @@ -0,0 +1,100 @@ +'use strict'; +const common = require('../common.js'); +const sqlite = require('node:sqlite'); +const assert = require('assert'); + +const bench = common.createBenchmark(main, { + n: [1e5], + statement: [ + 'INSERT INTO text_column_type (text_column) VALUES (?)', + 'INSERT INTO integer_column_type (integer_column) VALUES (?)', + 'INSERT INTO real_column_type (real_column) VALUES (?)', + 'INSERT INTO blob_column_type (blob_column) VALUES (?)', + 'INSERT INTO all_column_types (text_column, integer_column, real_column, blob_column) VALUES (?, ?, ?, ?)', + 'INSERT INTO large_text (text_8kb_column) VALUES (?)', + 'INSERT INTO missing_required_value (any_value, required_value) VALUES (?, ?)', + ], +}); + +function main(conf) { + const db = new sqlite.DatabaseSync(':memory:'); + + db.exec('CREATE TABLE text_column_type (text_column TEXT)'); + db.exec('CREATE TABLE integer_column_type (integer_column INTEGER)'); + db.exec('CREATE TABLE real_column_type (real_column REAL)'); + db.exec('CREATE TABLE blob_column_type (blob_column BLOB)'); + db.exec( + 'CREATE TABLE all_column_types (text_column TEXT, integer_column INTEGER, real_column REAL, blob_column BLOB)', + ); + db.exec('CREATE TABLE large_text (text_8kb_column TEXT)'); + db.exec('CREATE TABLE missing_required_value (any_value TEXT, required_value TEXT NOT NULL)'); + + const insertStatement = db.prepare(conf.statement); + + let i; + let deadCodeElimination; + + const stringValue = crypto.randomUUID(); + const integerValue = Math.floor(Math.random() * 100); + const realValue = Math.random(); + const blobValue = Buffer.from('example blob data'); + + const largeText = 'a'.repeat(8 * 1024); + + if (conf.statement.includes('text_column_type')) { + bench.start(); + for (i = 0; i < conf.n; i += 1) { + deadCodeElimination = insertStatement.run(stringValue); + } + bench.end(conf.n); + } else if (conf.statement.includes('integer_column_type')) { + bench.start(); + for (i = 0; i < conf.n; i += 1) { + deadCodeElimination = insertStatement.run(integerValue); + } + bench.end(conf.n); + } else if (conf.statement.includes('real_column_type')) { + bench.start(); + for (i = 0; i < conf.n; i += 1) { + deadCodeElimination = insertStatement.run(realValue); + } + bench.end(conf.n); + } else if (conf.statement.includes('blob_column_type')) { + bench.start(); + for (i = 0; i < conf.n; i += 1) { + deadCodeElimination = insertStatement.run(blobValue); + } + bench.end(conf.n); + } else if (conf.statement.includes('INTO all_column_types')) { + bench.start(); + for (i = 0; i < conf.n; i += 1) { + deadCodeElimination = insertStatement.run( + stringValue, + integerValue, + realValue, + blobValue, + ); + } + bench.end(conf.n); + } else if (conf.statement.includes('INTO large_text')) { + bench.start(); + for (i = 0; i < conf.n; i += 1) { + deadCodeElimination = insertStatement.run(largeText); + } + bench.end(conf.n); + } else if (conf.statement.includes('missing_required_value')) { + bench.start(); + for (i = 0; i < conf.n; i += 1) { + try { + deadCodeElimination = insertStatement.run(stringValue); + } catch (e) { + deadCodeElimination = e; + } + } + bench.end(conf.n); + } else { + throw new Error('Unknown statement'); + } + + assert.ok(deadCodeElimination !== undefined); +} diff --git a/benchmark/sqlite/sqlite-prepare-select-all.js b/benchmark/sqlite/sqlite-prepare-select-all.js new file mode 100644 index 00000000000000..e7ea882f16fd83 --- /dev/null +++ b/benchmark/sqlite/sqlite-prepare-select-all.js @@ -0,0 +1,61 @@ +'use strict'; +const common = require('../common.js'); +const sqlite = require('node:sqlite'); +const assert = require('assert'); + +const bench = common.createBenchmark(main, { + n: [1e5], + tableSeedSize: [1e5], + statement: [ + 'SELECT 1', + 'SELECT * FROM foo LIMIT 1', + 'SELECT * FROM foo LIMIT 100', + 'SELECT text_column FROM foo LIMIT 1', + 'SELECT text_column FROM foo LIMIT 100', + 'SELECT text_column, integer_column FROM foo LIMIT 1', + 'SELECT text_column, integer_column FROM foo LIMIT 100', + 'SELECT text_column, integer_column, real_column FROM foo LIMIT 1', + 'SELECT text_column, integer_column, real_column FROM foo LIMIT 100', + 'SELECT text_column, integer_column, real_column, blob_column FROM foo LIMIT 1', + 'SELECT text_column, integer_column, real_column, blob_column FROM foo LIMIT 100', + 'SELECT text_8kb_column FROM foo_large LIMIT 1', + 'SELECT text_8kb_column FROM foo_large LIMIT 100', + ], +}); + +function main(conf) { + const db = new sqlite.DatabaseSync(':memory:'); + + 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 (?, ?, ?, ?)', + ); + + for (let i = 0; i < conf.tableSeedSize; i++) { + fooInsertStatement.run( + crypto.randomUUID(), + Math.floor(Math.random() * 100), + Math.random(), + Buffer.from('example blob data'), + ); + } + + db.exec('CREATE TABLE foo_large (text_8kb_column TEXT)'); + const fooLargeInsertStatement = db.prepare('INSERT INTO foo_large (text_8kb_column) VALUES (?)'); + const largeText = 'a'.repeat(8 * 1024); + for (let i = 0; i < conf.tableSeedSize; i++) { + fooLargeInsertStatement.run(largeText); + } + + let i; + let deadCodeElimination; + + const stmt = db.prepare(conf.statement); + + bench.start(); + for (i = 0; i < conf.n; i += 1) + deadCodeElimination = stmt.all(); + bench.end(conf.n); + + assert.ok(deadCodeElimination !== undefined); +} diff --git a/benchmark/sqlite/sqlite-prepare-select-get.js b/benchmark/sqlite/sqlite-prepare-select-get.js new file mode 100644 index 00000000000000..2308fe8947654b --- /dev/null +++ b/benchmark/sqlite/sqlite-prepare-select-get.js @@ -0,0 +1,55 @@ +'use strict'; +const common = require('../common.js'); +const sqlite = require('node:sqlite'); +const assert = require('assert'); + +const bench = common.createBenchmark(main, { + n: [1e5], + tableSeedSize: [1e5], + statement: [ + 'SELECT 1', + 'SELECT * FROM foo LIMIT 1', + 'SELECT text_column FROM foo LIMIT 1', + 'SELECT text_column, integer_column FROM foo LIMIT 1', + 'SELECT text_column, integer_column, real_column FROM foo LIMIT 1', + 'SELECT text_column, integer_column, real_column, blob_column FROM foo LIMIT 1', + 'SELECT text_8kb_column FROM foo_large LIMIT 1', + ], +}); + +function main(conf) { + const db = new sqlite.DatabaseSync(':memory:'); + + 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 (?, ?, ?, ?)', + ); + + for (let i = 0; i < conf.tableSeedSize; i++) { + fooInsertStatement.run( + crypto.randomUUID(), + Math.floor(Math.random() * 100), + Math.random(), + Buffer.from('example blob data'), + ); + } + + db.exec('CREATE TABLE foo_large (text_8kb_column TEXT)'); + const fooLargeInsertStatement = db.prepare('INSERT INTO foo_large (text_8kb_column) VALUES (?)'); + const largeText = 'a'.repeat(8 * 1024); + for (let i = 0; i < conf.tableSeedSize; i++) { + fooLargeInsertStatement.run(largeText); + } + + let i; + let deadCodeElimination; + + const stmt = db.prepare(conf.statement); + + bench.start(); + for (i = 0; i < conf.n; i += 1) + deadCodeElimination = stmt.get(); + bench.end(conf.n); + + assert.ok(deadCodeElimination !== undefined); +}