forked from rocicorp/replicache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidb.js
More file actions
112 lines (106 loc) · 2.81 KB
/
Copy pathidb.js
File metadata and controls
112 lines (106 loc) · 2.81 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
import {openDB, deleteDB} from './node_modules/idb/with-async-ittr.js';
import xbytes from './node_modules/xbytes/dist/index.mjs';
import {randomData} from './data.js';
/**
* @param {{dataType: import("./data").RandomDataType; group: string; valSize: number; numKeys: number;}} opts
* @returns Benchmark
*/
export function benchmarkIDBRead(opts) {
return {
name: `idb read tx (${opts.dataType}) ${opts.numKeys}x${xbytes(
opts.valSize,
{
fixed: 0,
iec: true,
},
)}`,
group: opts.group,
byteSize: opts.valSize * opts.numKeys,
async setup() {
await deleteDB('db1');
const db = await openDB('db1', 1, {
upgrade(db) {
db.createObjectStore('store1');
},
});
try {
await idbPopulate(
db,
randomData(opts.dataType, opts.numKeys, opts.valSize),
);
} finally {
db.close();
}
},
/**
* @param {import('./perf.js').Bencher} bench
*/
async run(bench) {
const db = await openDB('db1');
try {
bench.reset();
const tx = db.transaction('store1', 'readonly', {
durability: 'relaxed',
});
const store = await tx.objectStore('store1');
const vals = await store.getAll(IDBKeyRange.bound(0, opts.numKeys - 1));
bench.stop();
// Use the values to ensure they aren't optimized away.
console.log(`Read ${vals.length} values`);
} finally {
db.close();
}
},
};
}
/**
*
* @param {import('idb').IDBPDatabase} db
* @param {(string | ArrayBuffer | Record<string, string> | Blob)[]} data
*/
async function idbPopulate(db, data) {
const tx = db.transaction('store1', 'readwrite', {durability: 'relaxed'});
const store = await tx.objectStore('store1');
await Promise.all(data.map((v, i) => store.put(v, i)));
await tx.done;
}
/**
* @param {{dataType: import('./data').RandomDataType; group: string; valSize: number; numKeys: number;}} opts
* @returns Benchmark
*/
export function benchmarkIDBWrite(opts) {
return {
name: `idb write tx (${opts.dataType}) ${opts.numKeys}x${xbytes(
opts.valSize,
{
fixed: 0,
iec: true,
},
)}`,
group: opts.group,
byteSize: opts.valSize * opts.numKeys,
async setup() {
await deleteDB('db1');
const db = await openDB('db1', 1, {
upgrade(db) {
db.createObjectStore('store1');
},
});
db.close();
},
/**
* @param {import('./perf.js').Bencher} bench
*/
async run(bench) {
const db = await openDB('db1');
try {
const data = randomData(opts.dataType, opts.numKeys, opts.valSize);
bench.reset();
await idbPopulate(db, data);
bench.stop();
} finally {
db.close();
}
},
};
}