Skip to content

Commit bfc561a

Browse files
H4adaduh95
authored andcommitted
benchmark: add sqlite prepare insert
PR-URL: #58040 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Edy Silva <edigleyssonsilva@gmail.com>
1 parent 6ceac5f commit bfc561a

File tree

1 file changed

+100
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)