-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode-sqlite3.ts
More file actions
297 lines (272 loc) · 8.44 KB
/
node-sqlite3.ts
File metadata and controls
297 lines (272 loc) · 8.44 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import { Benchmark } from '../Benchmark.js';
import { join } from 'path';
import { promises as fs } from 'fs';
import assert from 'node:assert';
import { numberName, createRandom } from '../util.js';
import sqlite3 from 'sqlite3';
import { open, Database } from 'sqlite';
export class NodeSqlite3Impl extends Benchmark {
private db!: Database;
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 = await open({ driver: sqlite3.Database, filename: dbPath });
this.db = db;
await db.exec('PRAGMA journal_mode = WAL');
await db.exec('PRAGMA synchronous = normal');
await db.exec(
'CREATE TABLE t1(id INTEGER PRIMARY KEY, a INTEGER, b INTEGER, c TEXT)'
);
await db.exec(
'CREATE TABLE t2(id INTEGER PRIMARY KEY, a INTEGER, b INTEGER, c TEXT)'
);
await db.exec(
'CREATE TABLE t3(id INTEGER PRIMARY KEY, a INTEGER, b INTEGER, c TEXT)'
);
await db.exec('CREATE INDEX i3a ON t3(a)');
await db.exec('CREATE INDEX i3b ON t3(b)');
}
async tearDown(): Promise<void> {
await this.db.close();
}
// Test 1: 1000 INSERTs
async test1(): Promise<void> {
const db = this.db;
const stmt = await db.prepare('INSERT INTO t1(a, b, c) VALUES(?, ?, ?)');
try {
for (let i = 0; i < 1000; i++) {
const n = this.random.nextInt(0, 100000);
await stmt.run(i + 1, n, numberName(n));
}
} finally {
await stmt.finalize();
}
// await db.run('PRAGMA wal_checkpoint(RESTART)');
const total = await db.get<{ count: number }>(
'select count() as count from t1'
);
assert(total!.count == 1000);
}
// Test 2: 25000 INSERTs in a transaction
async test2(): Promise<void> {
const db = this.db;
const stmt = await db.prepare('INSERT INTO t2(a, b, c) VALUES(?, ?, ?)');
try {
await this.transaction(async () => {
for (let i = 0; i < 25000; i++) {
const n = this.random.nextInt(0, 100000);
await stmt.run(i + 1, n, numberName(n));
}
});
await db.exec('PRAGMA wal_checkpoint(RESTART)');
const total = await db.get<{ count: number }>(
'select count() as count from t2'
);
assert(total!.count == 25000);
} finally {
await stmt.finalize();
}
}
// Test 3: 25000 INSERTs into an indexed table
async test3(): Promise<void> {
const db = this.db;
const stmt = await db.prepare('INSERT INTO t3(a, b, c) VALUES(?, ?, ?)');
try {
await this.transaction(async () => {
for (let i = 0; i < 25000; i++) {
const n = this.random.nextInt(0, 100000);
await stmt.run([i + 1, n, numberName(n)]);
}
});
} finally {
await stmt.finalize();
}
await db.exec('PRAGMA wal_checkpoint(RESTART)');
}
// Test 4: 100 SELECTs without an index
async test4(): Promise<void> {
const db = this.db;
const stmt = await db.prepare(
'SELECT count(*) count, avg(b) avg FROM t2 WHERE b>=? AND b<?'
);
await this.transaction(async () => {
for (let i = 0; i < 100; i++) {
const row = await stmt.get<{ count: number; avg: number }>([
i * 100,
i * 100 + 1000
]);
assert(row!.count > 200);
assert(row!.count < 300);
assert(row!.avg > i * 100);
assert(row!.avg < i * 100 + 1000);
}
});
await stmt.finalize();
}
// Test 5: 100 SELECTs on a string comparison
async test5(): Promise<void> {
const db = this.db;
const stmt = await db.prepare(
'SELECT count(*) count, avg(b) avg FROM t2 WHERE c LIKE ?'
);
await this.transaction(async () => {
for (let i = 0; i < 100; i++) {
const row = await stmt.get<{ count: number; avg: number }>([
`%${numberName(i + 1)}%`
]);
assert(row!.count > 400);
assert(row!.count < 12000);
assert(row!.avg > 25000);
}
});
await stmt.finalize();
}
// Test 7: 5000 SELECTs with an index
async test7(): Promise<void> {
const db = this.db;
const stmt = await db.prepare(
'SELECT count(*) count, avg(b) avg FROM t3 WHERE b>=? AND b<?'
);
await this.transaction(async () => {
for (let i = 0; i < 5000; i++) {
const row = await stmt.get<{ count: number; avg: number }>([
i * 100,
i * 100 + 100
]);
if (i < 1000) {
assert(row!.count > 8);
assert(row!.count < 100);
} else {
assert(row!.count === 0);
}
}
});
await stmt.finalize();
}
// Test 8: 1000 UPDATEs without an index
async test8(): Promise<void> {
const db = this.db;
const stmt = await db.prepare('UPDATE t1 SET b=b*2 WHERE a>=? AND a<?');
await this.transaction(async () => {
for (let i = 0; i < 1000; i++) {
await stmt.run([i * 10, i * 10 + 10]);
}
});
await stmt.finalize();
await db.exec('PRAGMA wal_checkpoint(RESTART)');
}
// Test 9: 25000 UPDATEs with an index
async test9(): Promise<void> {
const db = this.db;
const stmt = await db.prepare('UPDATE t3 SET b=? WHERE a=?');
await this.transaction(async () => {
for (let i = 0; i < 25000; i++) {
const n = this.random.nextInt(0, 100000);
await stmt.run([n, i + 1]);
}
});
await stmt.finalize();
await db.exec('PRAGMA wal_checkpoint(RESTART)');
}
// Test 10: 25000 text UPDATEs with an index
async test10(): Promise<void> {
const db = this.db;
const stmt = await db.prepare('UPDATE t3 SET c=? WHERE a=?');
await this.transaction(async () => {
for (let i = 0; i < 25000; i++) {
const n = this.random.nextInt(0, 100000);
await stmt.run([numberName(n), i + 1]);
}
});
await stmt.finalize();
await db.exec('PRAGMA wal_checkpoint(RESTART)');
}
// Test 11: INSERTs from a SELECT
async test11(): Promise<void> {
const db = this.db;
await this.transaction(async () => {
await db.exec('INSERT INTO t1(a, b, c) SELECT b,a,c FROM t3');
await db.exec('INSERT INTO t3(a, b, c) SELECT b,a,c FROM t1');
});
await db.exec('PRAGMA wal_checkpoint(RESTART)');
}
// Test 12: DELETE without an index
async test12(): Promise<void> {
const db = this.db;
await db.exec("DELETE FROM t3 WHERE c LIKE '%fifty%'");
await db.exec('PRAGMA wal_checkpoint(RESTART)');
}
// Test 13: DELETE with an index
async test13(): Promise<void> {
const db = this.db;
await db.exec('DELETE FROM t3 WHERE a>10 AND a<20000');
await db.exec('PRAGMA wal_checkpoint(RESTART)');
}
// Test 14: A big INSERT after a big DELETE
async test14(): Promise<void> {
const db = this.db;
await db.exec('INSERT INTO t3(a, b, c) SELECT a, b, c FROM t1');
await 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 = await db.prepare('INSERT INTO t1(a, b, c) VALUES(?, ?, ?)');
await this.transaction(async () => {
await db.run('DELETE FROM t1');
for (let i = 0; i < 12000; i++) {
const n = this.random.nextInt(0, 100000);
await stmt.run([i + 1, n, numberName(n)]);
}
});
await stmt.finalize();
await db.exec('PRAGMA wal_checkpoint(RESTART)');
}
// Test 16: Clear table
async test16(): Promise<void> {
const db = this.db;
const row1 = await db.get<{ count: number }>(
'SELECT count() count FROM t1'
);
const row2 = await db.get<{ count: number }>(
'SELECT count() count FROM t2'
);
const row3 = await db.get<{ count: number }>(
'SELECT count() count FROM t3'
);
assert(row1!.count === 12000);
assert(row2!.count === 25000);
assert(row3!.count > 34000);
assert(row3!.count < 36000);
await db.exec('DELETE FROM t1');
await db.exec('DELETE FROM t2');
await db.exec('DELETE FROM t3');
await db.exec('PRAGMA wal_checkpoint(RESTART)');
}
async transaction(callback: () => Promise<void>) {
await this.db.exec('begin');
try {
await callback();
await this.db.exec('commit');
} catch (e) {
try {
await this.db.exec('rollback');
} catch (e2) {}
throw e;
}
}
}