Skip to content

Commit e297e24

Browse files
committed
benchmark: add sqlite prepare insert
1 parent e6446d2 commit e297e24

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const sqlite = require('node:sqlite');
4+
const assert = require('assert');
5+
6+
const bench = common.createBenchmark(main, {
7+
n: [1e5],
8+
statement: [
9+
'INSERT INTO text_column_type (text_column) VALUES (?)',
10+
'INSERT INTO integer_column_type (integer_column) VALUES (?)',
11+
'INSERT INTO real_column_type (real_column) VALUES (?)',
12+
'INSERT INTO blob_column_type (blob_column) VALUES (?)',
13+
'INSERT INTO all_column_types (text_column, integer_column, real_column, blob_column) VALUES (?, ?, ?, ?)',
14+
'INSERT INTO large_text (text_8kb_column) VALUES (?)',
15+
'INSERT INTO missing_required_value (any_value, required_value) VALUES (?, ?)',
16+
],
17+
});
18+
19+
function main(conf) {
20+
const n = conf.n | 0;
21+
22+
const db = new sqlite.DatabaseSync(':memory:');
23+
24+
db.exec('CREATE TABLE text_column_type (text_column TEXT)');
25+
db.exec('CREATE TABLE integer_column_type (integer_column INTEGER)');
26+
db.exec('CREATE TABLE real_column_type (real_column REAL)');
27+
db.exec('CREATE TABLE blob_column_type (blob_column BLOB)');
28+
db.exec(
29+
'CREATE TABLE all_column_types (text_column TEXT, integer_column INTEGER, real_column REAL, blob_column BLOB)',
30+
);
31+
db.exec('CREATE TABLE large_text (text_8kb_column TEXT)');
32+
db.exec('CREATE TABLE missing_required_value (any_value TEXT, required_value TEXT NOT NULL)');
33+
34+
const insertStatement = db.prepare(conf.statement);
35+
36+
let i;
37+
let deadCodeElimination;
38+
39+
const stringValue = crypto.randomUUID();
40+
const integerValue = Math.floor(Math.random() * 100);
41+
const realValue = Math.random();
42+
const blobValue = Buffer.from('example blob data');
43+
44+
const largeText = 'a'.repeat(8 * 1024);
45+
46+
if (conf.statement.includes('text_column_type')) {
47+
bench.start();
48+
for (i = 0; i < n; i += 1) {
49+
deadCodeElimination = insertStatement.run(stringValue);
50+
}
51+
bench.end(n);
52+
} else if (conf.statement.includes('integer_column_type')) {
53+
bench.start();
54+
for (i = 0; i < n; i += 1) {
55+
deadCodeElimination = insertStatement.run(integerValue);
56+
}
57+
bench.end(n);
58+
} else if (conf.statement.includes('real_column_type')) {
59+
bench.start();
60+
for (i = 0; i < n; i += 1) {
61+
deadCodeElimination = insertStatement.run(realValue);
62+
}
63+
bench.end(n);
64+
} else if (conf.statement.includes('blob_column_type')) {
65+
bench.start();
66+
for (i = 0; i < n; i += 1) {
67+
deadCodeElimination = insertStatement.run(blobValue);
68+
}
69+
bench.end(n);
70+
} else if (conf.statement.includes('INTO all_column_types')) {
71+
bench.start();
72+
for (i = 0; i < n; i += 1) {
73+
deadCodeElimination = insertStatement.run(
74+
stringValue,
75+
integerValue,
76+
realValue,
77+
blobValue,
78+
);
79+
}
80+
bench.end(n);
81+
} else if (conf.statement.includes('INTO large_text')) {
82+
bench.start();
83+
for (i = 0; i < n; i += 1) {
84+
deadCodeElimination = insertStatement.run(largeText);
85+
}
86+
bench.end(n);
87+
} else if (conf.statement.includes('missing_required_value')) {
88+
bench.start();
89+
for (i = 0; i < n; i += 1) {
90+
try {
91+
deadCodeElimination = insertStatement.run(stringValue);
92+
} catch (e) {
93+
deadCodeElimination = e;
94+
}
95+
}
96+
bench.end(n);
97+
} else {
98+
throw new Error('Unknown statement');
99+
}
100+
101+
assert.ok(deadCodeElimination !== undefined);
102+
}

0 commit comments

Comments
 (0)