-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode-sqlite.ts
More file actions
282 lines (254 loc) · 7.57 KB
/
node-sqlite.ts
File metadata and controls
282 lines (254 loc) · 7.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import { promises as fs } from 'fs';
import assert from 'node:assert';
import { join } from 'path';
import { Benchmark } from '../Benchmark.js';
import { numberName, createRandom } from '../util.js';
//@ts-ignore
import * as sqlite from 'node:sqlite';
export class NodeSqliteImpl extends Benchmark {
private db!: sqlite.DatabaseSync;
private dir: string;
private random = createRandom(0);
constructor(
public name: string,
dir: string
) {
super();
this.dir = dir;
}
async setUp(): Promise<void> {
const dbPath = join(this.dir, this.name + '.db');
try {
await fs.unlink(dbPath);
await fs.unlink(dbPath + '-wal');
} catch (e) {
// Ignore
}
const db = new sqlite.DatabaseSync(dbPath);
this.db = db;
db.exec('PRAGMA journal_mode = wal');
db.exec('PRAGMA synchronous = normal');
db.exec(
'CREATE TABLE t1(id INTEGER PRIMARY KEY, a INTEGER, b INTEGER, c TEXT)'
);
db.exec(
'CREATE TABLE t2(id INTEGER PRIMARY KEY, a INTEGER, b INTEGER, c TEXT)'
);
db.exec(
'CREATE TABLE t3(id INTEGER PRIMARY KEY, a INTEGER, b INTEGER, c TEXT)'
);
db.exec('CREATE INDEX i3a ON t3(a)');
db.exec('CREATE INDEX i3b ON t3(b)');
}
async tearDown(): Promise<void> {
this.db.close();
}
// Test 1: 1000 INSERTs
async test1(): Promise<void> {
const db = this.db;
const stmt = db.prepare('INSERT INTO t1(a, b, c) VALUES(?, ?, ?)');
for (let i = 0; i < 1000; i++) {
const n = this.random.nextInt(0, 100000);
stmt.run(i + 1, n, numberName(n));
}
db.exec('PRAGMA wal_checkpoint(RESTART)');
const total = db.prepare('select count() as count from t1').get() as {
count: number;
};
assert(total.count == 1000);
}
// Test 2: 25000 INSERTs in a transaction
async test2(): Promise<void> {
const db = this.db;
const stmt = db.prepare('INSERT INTO t2(a, b, c) VALUES(?, ?, ?)');
this.transaction(() => {
for (let i = 0; i < 25000; i++) {
const n = this.random.nextInt(0, 100000);
stmt.run(i + 1, n, numberName(n));
}
});
db.exec('PRAGMA wal_checkpoint(RESTART)');
const total = db.prepare('select count() as count from t2').get() as {
count: number;
};
assert(total.count == 25000);
}
// Test 3: 25000 INSERTs into an indexed table
async test3(): Promise<void> {
const db = this.db;
const stmt = db.prepare('INSERT INTO t3(a, b, c) VALUES(?, ?, ?)');
this.transaction(() => {
for (let i = 0; i < 25000; i++) {
const n = this.random.nextInt(0, 100000);
stmt.run(i + 1, n, numberName(n));
}
});
db.exec('PRAGMA wal_checkpoint(RESTART)');
}
// Test 4: 100 SELECTs without an index
async test4(): Promise<void> {
const db = this.db;
const stmt = db.prepare(
'SELECT count(*) count, avg(b) avg FROM t2 WHERE b>=? AND b<?'
);
this.transaction(() => {
for (let i = 0; i < 100; i++) {
const row = stmt.get(i * 100, i * 100 + 1000) as {
count: number;
avg: number;
};
assert(row.count > 200);
assert(row.count < 300);
assert(row.avg > i * 100);
assert(row.avg < i * 100 + 1000);
}
});
}
// Test 5: 100 SELECTs on a string comparison
async test5(): Promise<void> {
const db = this.db;
const stmt = db.prepare(
'SELECT count(*) count, avg(b) avg FROM t2 WHERE c LIKE ?'
);
this.transaction(() => {
for (let i = 0; i < 100; i++) {
const row = stmt.get(`%${numberName(i + 1)}%`) as {
count: number;
avg: number;
};
assert(row.count > 400);
assert(row.count < 12000);
assert(row.avg > 25000);
}
});
}
// Test 7: 5000 SELECTs with an index
async test7(): Promise<void> {
const db = this.db;
const stmt = db.prepare(
'SELECT count(*) count, avg(b) avg FROM t3 WHERE b>=? AND b<?'
);
this.transaction(() => {
for (let i = 0; i < 5000; i++) {
const row = stmt.get(i * 100, i * 100 + 100) as {
count: number;
avg: number;
};
if (i < 1000) {
assert(row.count > 8);
assert(row.count < 100);
} else {
assert(row.count === 0);
}
}
});
}
// Test 8: 1000 UPDATEs without an index
async test8(): Promise<void> {
const db = this.db;
const stmt = db.prepare('UPDATE t1 SET b=b*2 WHERE a>=? AND a<?');
this.transaction(() => {
for (let i = 0; i < 1000; i++) {
stmt.run(i * 10, i * 10 + 10);
}
});
db.exec('PRAGMA wal_checkpoint(RESTART)');
}
// Test 9: 25000 UPDATEs with an index
async test9(): Promise<void> {
const db = this.db;
const stmt = db.prepare('UPDATE t3 SET b=? WHERE a=?');
this.transaction(() => {
for (let i = 0; i < 25000; i++) {
const n = this.random.nextInt(0, 100000);
stmt.run(n, i + 1);
}
});
db.exec('PRAGMA wal_checkpoint(RESTART)');
}
// Test 10: 25000 text UPDATEs with an index
async test10(): Promise<void> {
const db = this.db;
const stmt = db.prepare('UPDATE t3 SET c=? WHERE a=?');
this.transaction(() => {
for (let i = 0; i < 25000; i++) {
const n = this.random.nextInt(0, 100000);
stmt.run(numberName(n), i + 1);
}
});
db.exec('PRAGMA wal_checkpoint(RESTART)');
}
// Test 11: INSERTs from a SELECT
async test11(): Promise<void> {
const db = this.db;
this.transaction(() => {
db.exec('INSERT INTO t1(a, b, c) SELECT b,a,c FROM t3');
db.exec('INSERT INTO t3(a, b, c) SELECT b,a,c FROM t1');
});
db.exec('PRAGMA wal_checkpoint(RESTART)');
}
// Test 12: DELETE without an index
async test12(): Promise<void> {
const db = this.db;
db.exec("DELETE FROM t3 WHERE c LIKE '%fifty%'");
db.exec('PRAGMA wal_checkpoint(RESTART)');
}
// Test 13: DELETE with an index
async test13(): Promise<void> {
const db = this.db;
db.exec('DELETE FROM t3 WHERE a>10 AND a<20000');
db.exec('PRAGMA wal_checkpoint(RESTART)');
}
// Test 14: A big INSERT after a big DELETE
async test14(): Promise<void> {
const db = this.db;
db.exec('INSERT INTO t3(a, b, c) SELECT a, b, c FROM t1');
db.exec('PRAGMA wal_checkpoint(RESTART)');
}
// Test 15: A big DELETE followed by many small INSERTs
async test15(): Promise<void> {
const db = this.db;
const stmt = db.prepare('INSERT INTO t1(a, b, c) VALUES(?, ?, ?)');
this.transaction(() => {
db.exec('DELETE FROM t1');
for (let i = 0; i < 12000; i++) {
const n = this.random.nextInt(0, 100000);
stmt.run(i + 1, n, numberName(n));
}
});
db.exec('PRAGMA wal_checkpoint(RESTART)');
}
// Test 16: Clear table
async test16(): Promise<void> {
const db = this.db;
const row1 = db.prepare('SELECT count() count FROM t1').get() as {
count: number;
};
const row2 = db.prepare('SELECT count() count FROM t2').get() as {
count: number;
};
const row3 = db.prepare('SELECT count() count FROM t3').get() as {
count: number;
};
assert(row1.count === 12000);
assert(row2.count === 25000);
assert(row3.count > 34000);
assert(row3.count < 36000);
db.exec('DELETE FROM t1');
db.exec('DELETE FROM t2');
db.exec('DELETE FROM t3');
db.exec('PRAGMA wal_checkpoint(RESTART)');
}
transaction(callback: () => void) {
this.db.exec('begin');
try {
callback();
this.db.exec('commit');
} catch (e) {
try {
this.db.exec('rollback');
} catch (e2) {}
throw e;
}
}
}