-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsjp-json.ts
More file actions
316 lines (284 loc) · 9.43 KB
/
sjp-json.ts
File metadata and controls
316 lines (284 loc) · 9.43 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import { promises as fs } from 'fs';
import assert from 'node:assert';
import { join } from 'path';
import { SqliteConnectionPool } from '@sqlite-js/api';
import { Benchmark } from '../Benchmark.js';
import { numberName, createRandom } from '../util.js';
export class JSPJsonImpl extends Benchmark {
private db!: SqliteConnectionPool;
private dir: string;
private random = createRandom(0);
constructor(
public name: string,
dir: string,
private driver: (path: string) => SqliteConnectionPool
) {
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 = this.driver(dbPath);
this.db = db;
{
await using c = await db.reserveConnection();
await c.run(
'CREATE TABLE t1(id INTEGER PRIMARY KEY, a INTEGER, b INTEGER, c TEXT)'
);
await c.run(
'CREATE TABLE t2(id INTEGER PRIMARY KEY, a INTEGER, b INTEGER, c TEXT)'
);
await c.run(
'CREATE TABLE t3(id INTEGER PRIMARY KEY, a INTEGER, b INTEGER, c TEXT)'
);
await c.run('CREATE INDEX i3a ON t3(a)');
await c.run('CREATE INDEX i3b ON t3(b)');
}
// Pre-create a bunch of read connections
let promises = [];
for (let i = 0; i < 10; i++) {
promises.push(
(async () => {
await using c = await db.reserveConnection({ readonly: true });
await new Promise((resolve) => setTimeout(resolve, 1));
})()
);
}
await Promise.all(promises);
}
async tearDown(): Promise<void> {
await this.db.close();
}
// Test 1: 1000 INSERTs
async test1(): Promise<void> {
await using db = await this.db.reserveConnection();
using s = db.prepare('INSERT INTO t1(a, b, c) VALUES(?, ?, ?)');
for (let i = 0; i < 1000; i++) {
const n = this.random.nextInt(0, 100000);
await s.run([i + 1, n, numberName(n)]);
}
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> {
await using tx = await this.db.begin();
using s = tx.prepare(
'INSERT INTO t2(a, b, c) SELECT e.value ->> 0, e.value ->> 1, e.value ->> 2 from json_each(?) e'
);
let buffer: any[][] = [];
for (let i = 0; i < 25000; i++) {
const n = this.random.nextInt(0, 100000);
buffer.push([i + 1, n, numberName(n)]);
if (buffer.length >= 100) {
await s.run([JSON.stringify(buffer)]);
buffer = [];
}
}
await s.run([JSON.stringify(buffer)]);
await tx.commit();
await this.db.run('PRAGMA wal_checkpoint(RESTART)');
const total = await this.db.get<{ count: number }>(
'select count() as count from t2'
);
assert(total.count == 25000);
}
// Test 3: 25000 INSERTs into an indexed table
async test3(): Promise<void> {
await using tx = await this.db.begin();
using s = tx.prepare(
'INSERT INTO t3(a, b, c) SELECT e.value ->> 0, e.value ->> 1, e.value ->> 2 from json_each(?) e'
);
let buffer: any[][] = [];
for (let i = 0; i < 25000; i++) {
const n = this.random.nextInt(0, 100000);
buffer.push([i + 1, n, numberName(n)]);
if (buffer.length >= 100) {
await s.run([JSON.stringify(buffer)]);
buffer = [];
}
}
await s.run([JSON.stringify(buffer)]);
await tx.commit();
await this.db.run('PRAGMA wal_checkpoint(RESTART)');
}
// Test 4: 100 SELECTs without an index
async test4(): Promise<void> {
await using tx = await this.db.begin({ readonly: true });
using s = tx.prepare<{ count: number; avg: number }>(
'SELECT count(*) count, avg(b) avg FROM t2 WHERE b>=? AND b<?'
);
for (let i = 0; i < 100; i++) {
const row = (await s.select([i * 100, i * 100 + 1000]))[0];
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> {
await using tx = await this.db.begin({ readonly: true });
using s = tx.prepare<{ count: number; avg: number }>(
'SELECT count(*) count, avg(b) avg FROM t2 WHERE c LIKE ?'
);
for (let i = 0; i < 100; i++) {
const row = (await s.select([`%${numberName(i + 1)}%`]))[0];
assert(row.count > 400);
assert(row.count < 12000);
assert(row.avg > 25000);
}
}
// Test 7: 5000 SELECTs with an index
async test7(): Promise<void> {
let promises: Promise<void>[] = [];
const batchSize = 500;
const batches = Math.ceil(5000 / batchSize);
for (let batch = 0; batch < batches; batch++) {
const promise = this.db.transaction(
async (tx) => {
using s = tx.prepare<{ count: number; avg: number }>(
'SELECT count(*) count, avg(b) avg FROM t3 WHERE b>=? AND b<?'
);
for (
let i = batch * batchSize;
i < batch * batchSize + batchSize && i < 5000;
i++
) {
const row = (await s.select([i * 100, i * 100 + 100]))[0];
if (i < 1000) {
assert(row.count > 8);
assert(row.count < 100);
} else {
assert(row.count === 0);
}
}
},
{ readonly: true }
);
promises.push(promise);
}
await Promise.all(promises);
}
// Test 8: 1000 UPDATEs without an index
async test8(): Promise<void> {
await using tx = await this.db.begin();
using s = tx.prepare('UPDATE t1 SET b=b*2 WHERE a>=? AND a<?');
for (let i = 0; i < 1000; i++) {
await s.run([i * 10, i * 10 + 10]);
}
await tx.commit();
await this.db.run('PRAGMA wal_checkpoint(RESTART)');
}
// Test 9: 25000 UPDATEs with an index
async test9(): Promise<void> {
await using tx = await this.db.begin();
using s = tx.prepare(
'UPDATE t3 SET b = e.value ->> 1 FROM json_each(?) e WHERE a = e.value ->> 0'
);
let batch: any[][] = [];
for (let i = 0; i < 25000; i++) {
const n = this.random.nextInt(0, 100000);
batch.push([i + 1, n]);
if (batch.length >= 100) {
await s.run([JSON.stringify(batch)]);
batch = [];
}
}
await s.run([JSON.stringify(batch)]);
await tx.commit();
await this.db.run('PRAGMA wal_checkpoint(RESTART)');
}
// Test 10: 25000 text UPDATEs with an index
async test10(): Promise<void> {
await using tx = await this.db.begin();
using s = tx.prepare(
'UPDATE t3 SET c = e.value ->> 1 FROM json_each(?) e WHERE a = e.value ->> 0'
);
let batch: any[][] = [];
for (let i = 0; i < 25000; i++) {
const n = this.random.nextInt(0, 100000);
batch.push([i + 1, numberName(n)]);
if (batch.length >= 100) {
await s.run([JSON.stringify(batch)]);
batch = [];
}
}
await s.run([JSON.stringify(batch)]);
await tx.commit();
await this.db.run('PRAGMA wal_checkpoint(RESTART)');
}
// Test 11: INSERTs from a SELECT
async test11(): Promise<void> {
await using tx = await this.db.begin();
await tx.run('INSERT INTO t1(a, b, c) SELECT b,a,c FROM t3');
await tx.run('INSERT INTO t3(a, b, c) SELECT b,a,c FROM t1');
await tx.commit();
await this.db.run('PRAGMA wal_checkpoint(RESTART)');
}
// Test 12: DELETE without an index
async test12(): Promise<void> {
await this.db.run("DELETE FROM t3 WHERE c LIKE '%fifty%'");
await this.db.run('PRAGMA wal_checkpoint(RESTART)');
}
// Test 13: DELETE with an index
async test13(): Promise<void> {
await this.db.run('DELETE FROM t3 WHERE a>10 AND a<20000');
await this.db.run('PRAGMA wal_checkpoint(RESTART)');
}
// Test 14: A big INSERT after a big DELETE
async test14(): Promise<void> {
await this.db.run('INSERT INTO t3(a, b, c) SELECT a, b, c FROM t1');
await this.db.run('PRAGMA wal_checkpoint(RESTART)');
}
// Test 15: A big DELETE followed by many small INSERTs
async test15(): Promise<void> {
await using tx = await this.db.begin();
using s = tx.prepare(
'INSERT INTO t1(a, b, c) SELECT value ->> 0, value ->> 1, value ->> 2 FROM json_each(?)'
);
await tx.run('DELETE FROM t1');
let batch: any[][] = [];
for (let i = 0; i < 12000; i++) {
const n = this.random.nextInt(0, 100000);
batch.push([i + 1, n, numberName(n)]);
if (batch.length >= 100) {
await s.run([JSON.stringify(batch)]);
batch = [];
}
}
await s.run([JSON.stringify(batch)]);
await tx.commit();
await this.db.run('PRAGMA wal_checkpoint(RESTART)');
}
// Test 16: Clear table
async test16(): Promise<void> {
await using db = await this.db.reserveConnection();
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.run('DELETE FROM t1');
await db.run('DELETE FROM t2');
await db.run('DELETE FROM t3');
await db.run('PRAGMA wal_checkpoint(RESTART)');
}
}