Skip to content

Commit 6055db6

Browse files
authored
Merge pull request #106 from haaawk/perf-test
Add performance benchmarks and improve iterate
2 parents fb793fe + c45c77e commit 6055db6

File tree

8 files changed

+477
-19
lines changed

8 files changed

+477
-19
lines changed

index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,19 @@ class Statement {
316316
rows = statementRowsSync.call(this.stmt, bindParameters.flat());
317317
}
318318
const iter = {
319+
nextRows: Array(100),
320+
nextRowIndex: 100,
319321
next() {
320-
const row = rowsNext.call(rows);
322+
if (this.nextRowIndex === 100) {
323+
rowsNext.call(rows, this.nextRows);
324+
this.nextRowIndex = 0;
325+
}
326+
const row = this.nextRows[this.nextRowIndex];
327+
this.nextRows[this.nextRowIndex] = undefined;
321328
if (!row) {
322329
return { done: true };
323330
}
331+
this.nextRowIndex++;
324332
return { value: row, done: false };
325333
},
326334
[Symbol.iterator]() {

perf/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "libsql-perf",
3+
"type": "module",
4+
"private": true,
5+
"dependencies": {
6+
"better-sqlite3": "^9.5.0",
7+
"libsql": "..",
8+
"mitata": "^0.1.11"
9+
}
10+
}

perf/perf-better-sqlite3.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { run, bench, group, baseline } from 'mitata';
2+
3+
import Database from 'better-sqlite3';
4+
5+
const db = new Database(':memory:');
6+
7+
db.exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)");
8+
db.exec("INSERT INTO users (id, name, email) VALUES (1, 'Alice', 'alice@example.org')");
9+
10+
const stmt = db.prepare("SELECT * FROM users WHERE id = ?");
11+
12+
group('Statement', () => {
13+
bench('get(1)', () => {
14+
stmt.get(1);
15+
});
16+
});
17+
18+
await run({
19+
units: false, // print small units cheatsheet
20+
silent: false, // enable/disable stdout output
21+
avg: true, // enable/disable avg column (default: true)
22+
json: false, // enable/disable json output (default: false)
23+
colors: true, // enable/disable colors (default: true)
24+
min_max: true, // enable/disable min/max column (default: true)
25+
percentiles: true, // enable/disable percentiles column (default: true)
26+
});

perf/perf-iterate-better-sqlite3.js

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
import { run, bench, group, baseline } from 'mitata';
2+
3+
import Database from 'better-sqlite3';
4+
5+
const db = new Database(':memory:');
6+
7+
db.exec(`CREATE TABLE users (
8+
field1 TEXT,
9+
field2 TEXT,
10+
field3 TEXT,
11+
field4 TEXT,
12+
field5 TEXT,
13+
field6 TEXT,
14+
field7 TEXT,
15+
field8 TEXT,
16+
field9 TEXT,
17+
field10 TEXT,
18+
field11 TEXT,
19+
field12 TEXT,
20+
field13 TEXT,
21+
field14 TEXT,
22+
field15 TEXT,
23+
field16 TEXT,
24+
field17 TEXT,
25+
field18 TEXT,
26+
field19 TEXT,
27+
field20 TEXT,
28+
field21 TEXT,
29+
field22 TEXT,
30+
field23 TEXT,
31+
field24 TEXT,
32+
field25 TEXT,
33+
field26 TEXT,
34+
field27 TEXT,
35+
field28 TEXT,
36+
field29 TEXT,
37+
field30 TEXT,
38+
field31 TEXT,
39+
field32 TEXT,
40+
field33 TEXT,
41+
field34 TEXT,
42+
field35 TEXT,
43+
field36 TEXT,
44+
field37 TEXT,
45+
field38 TEXT,
46+
field39 TEXT,
47+
field40 TEXT,
48+
field41 TEXT,
49+
field42 TEXT,
50+
field43 TEXT,
51+
field44 TEXT,
52+
field45 TEXT,
53+
field46 TEXT,
54+
field47 TEXT,
55+
field48 TEXT,
56+
field49 TEXT,
57+
field50 TEXT,
58+
field51 INTEGER,
59+
field52 INTEGER,
60+
field53 INTEGER,
61+
field54 INTEGER,
62+
field55 INTEGER,
63+
field56 INTEGER,
64+
field57 INTEGER,
65+
field58 INTEGER,
66+
field59 INTEGER,
67+
field60 INTEGER,
68+
field61 INTEGER,
69+
field62 INTEGER,
70+
field63 INTEGER,
71+
field64 INTEGER,
72+
field65 INTEGER,
73+
field66 INTEGER,
74+
field67 INTEGER,
75+
field68 INTEGER,
76+
field69 INTEGER,
77+
field70 INTEGER
78+
)`);
79+
for (let id = 0; id < 500; id++) {
80+
db.exec(`INSERT INTO users VALUES (
81+
'some string here',
82+
'some string here',
83+
'some string here',
84+
'some string here',
85+
'some string here',
86+
'some string here',
87+
'some string here',
88+
'some string here',
89+
'some string here',
90+
'some string here',
91+
'some string here',
92+
'some string here',
93+
'some string here',
94+
'some string here',
95+
'some string here',
96+
'some string here',
97+
'some string here',
98+
'some string here',
99+
'some string here',
100+
'some string here',
101+
'some string here',
102+
'some string here',
103+
'some string here',
104+
'some string here',
105+
'some string here',
106+
'some string here',
107+
'some string here',
108+
'some string here',
109+
'some string here',
110+
'some string here',
111+
'some string here',
112+
'some string here',
113+
'some string here',
114+
'some string here',
115+
'some string here',
116+
'some string here',
117+
'some string here',
118+
'some string here',
119+
'some string here',
120+
'some string here',
121+
'some string here',
122+
'some string here',
123+
'some string here',
124+
'some string here',
125+
'some string here',
126+
'some string here',
127+
'some string here',
128+
'some string here',
129+
'some string here',
130+
'some string here',
131+
${id},
132+
${id},
133+
${id},
134+
${id},
135+
${id},
136+
${id},
137+
${id},
138+
${id},
139+
${id},
140+
${id},
141+
${id},
142+
${id},
143+
${id},
144+
${id},
145+
${id},
146+
${id},
147+
${id},
148+
${id},
149+
${id},
150+
${id}
151+
)`);
152+
}
153+
154+
const stmt = db.prepare("SELECT * FROM users WHERE field70 > ?");
155+
156+
group('Statement', () => {
157+
bench('iterate', () => {
158+
for (const row of stmt.iterate(10)) {
159+
if (row.field1 === 'Never appears') {
160+
break;
161+
}
162+
}
163+
});
164+
});
165+
166+
await run({
167+
units: false, // print small units cheatsheet
168+
silent: false, // enable/disable stdout output
169+
avg: true, // enable/disable avg column (default: true)
170+
json: false, // enable/disable json output (default: false)
171+
colors: true, // enable/disable colors (default: true)
172+
min_max: true, // enable/disable min/max column (default: true)
173+
percentiles: true, // enable/disable percentiles column (default: true)
174+
});

0 commit comments

Comments
 (0)