-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbenchmarks.ts
259 lines (240 loc) · 6.74 KB
/
benchmarks.ts
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
import * as path from '@std/path';
import { shuffle } from './base/array.ts';
import { uniqueId } from './base/common.ts';
import { coreValueCompare } from './base/core-types/comparable.ts';
import { assert } from './base/error.ts';
import { randomInt } from './base/math.ts';
import { SchemaManager } from './cfds/base/schema-manager.ts';
import { BloomFilter } from './cpp/bloom_filter.ts';
import { GoatDB } from './db/db.ts';
import { Query } from './repo/query.ts';
const ITEM_COUNT = 100000;
export const kSchemeNote = {
ns: 'note',
version: 1,
fields: {
text: {
type: 'string',
// default: () => initRichText(),
required: true,
},
},
} as const;
type SchemeNoteType = typeof kSchemeNote;
SchemaManager.default.register(kSchemeNote);
const kWords = [
'lorem',
'ipsum',
'dolor',
'sit',
'amet',
'proident',
'fuga',
'sed.',
'possimus',
'fuga',
'illo',
'non',
'sibi.',
'id',
'unum',
'enim',
'quibusdam',
'officii.',
'eaque',
'qui',
'pariatur,',
'tempor',
'in.',
'error',
'cum',
'tenetur,',
'ullamco',
'magnam.',
'enim',
'velit',
'laudantium,',
'ut',
'ipsa.',
'quoddam',
'colebatur',
'propter',
'officia.',
];
const repoPath = '/test/notes/';
async function createNewNote(db: GoatDB): Promise<void> {
const numWords = randomInt(1, kWords.length);
let text = '';
for (let i = 0; i < numWords; ++i) {
text += kWords[randomInt(0, kWords.length)] + ' ';
}
await db.load(repoPath + uniqueId(), kSchemeNote, {
text,
});
}
function changeText(text: string): string {
const textWords = text.split(' ');
const remove = textWords.length > 1 && randomInt(0, 2) === 1;
const add = textWords.length === 0 || randomInt(0, 2) === 1;
if (remove) {
textWords.splice(randomInt(0, textWords.length), 1);
}
if (add) {
textWords.splice(
randomInt(0, textWords.length),
0,
kWords[randomInt(0, kWords.length)],
);
}
return textWords.join(' ');
}
async function editDB(db: GoatDB, scale = 0.1): Promise<number> {
const promises: Promise<void>[] = [];
const totalEdits = Math.round(db.count(repoPath) * scale);
let editCount = 0;
while (editCount < totalEdits) {
for (const key of db.keys(repoPath)) {
// if (randomInt(0, Math.round(1 / scale)) === 0) {
const item = db.item<SchemeNoteType>(repoPath, key);
const updatedText = changeText(item.get('text'));
if (updatedText !== item.get('text')) {
item.set('text', updatedText);
promises.push(item.commit());
if (++editCount === totalEdits) {
break;
}
// }
}
}
}
await Promise.allSettled(promises);
return editCount;
}
async function populateDB(db: GoatDB): Promise<void> {
const createPromises: Promise<void>[] = [];
for (let i = 0; i < ITEM_COUNT; ++i) {
createPromises.push(createNewNote(db));
}
await Promise.allSettled(createPromises);
// let editCount = await editDB(db);
// editCount += await editDB(db);
// editCount += await editDB(db);
// console.log(`Edited ${editCount}`);
}
const REPO_FILE_PATH = path.resolve('test_data/test/notes.jsonl');
const DB_PATH = path.resolve('test_data/');
export async function testsMain(): Promise<void> {
console.log(`Running benchmark at ${DB_PATH}`);
const fileStart = performance.now();
try {
await Deno.readFile(REPO_FILE_PATH);
console.log(`File read in ${(performance.now() - fileStart) / 1000} sec`);
} catch (_: unknown) {}
// await BloomFilter.initNativeFunctions();
const db = new GoatDB({
path: DB_PATH,
});
console.log(`Opening Repo...`);
const repoPath = '/test/notes';
const openStart = performance.now();
const repo = await db.open(repoPath);
// const repo = db.getRepository(repoId)!;
console.log(
`Done. Open took ${
(performance.now() - openStart) / 1000
} sec.\n# Commits = ${repo
.numberOfCommits()
.toLocaleString()}\n# Keys = ${repo.storage
.numberOfKeys()
.toLocaleString()}`,
);
if (repo.numberOfCommits() === 0) {
console.log(`Repository is empty. Populating...`);
const start = performance.now();
await populateDB(db);
await db.flush(repoPath);
const populatingTime = performance.now() - start;
console.log(
`Populating repo ended. Took ${populatingTime / 1000} sec, avg ${
populatingTime / ITEM_COUNT
}ms/item`,
);
} else {
// const editCount = await editDB(db, 0.5);
// console.log(
// `Edited ${editCount}.\n# Commits = ${repo
// .numberOfCommits()
// .toLocaleString()}\n# Keys = ${repo.storage
// .numberOfKeys()
// .toLocaleString()}`,
// );
}
console.log(`Starting read test...`);
const keys = shuffle(Array.from(db.keys(repoPath))).slice(0, 10);
const readStart = performance.now();
const testCount = 1000;
for (let i = 0; i < testCount; ++i) {
for (const k of keys) {
const item = db.item<SchemeNoteType>(repoPath, k);
item.get('text');
}
}
const readTime = (performance.now() - readStart) / testCount;
console.log(
`Reading ${keys.length.toLocaleString()} items took ${
readTime / 1000
} sec. Avg ${readTime / keys.length} ms / key`,
);
// debugger;
// console.log(`Starting plain search...`);
// const dummyStart = performance.now();
// const results: [string, Document<SchemeNoteType>][] = [];
// const predicate = (key: string, doc: Document<SchemeNoteType>) =>
// doc.get('text').startsWith('lorem');
// for (const k of repo.keys()) {
// const doc = repo.valueForKey<SchemeNoteType>(k)![0];
// if (predicate(k, doc)) {
// results.push([k, doc]);
// }
// }
// results.sort((a1, a2) =>
// coreValueCompare(a1[1].get('text'), a2[1].get('text')),
// );
// console.log(
// `Dummy finished in ${
// (performance.now() - dummyStart) / 1000
// } sec, found ${results.length.toLocaleString()}`,
// );
// debugger;
console.log(`Starting query...`);
const queryStart = performance.now();
let prevCount: number | undefined;
const queryIter = 10;
for (let i = 0; i < queryIter; ++i) {
const query = db.query({
source: repoPath,
schema: kSchemeNote,
predicate: ({ item, ctx }) => item.get('text').startsWith(ctx.word),
sortDescriptor: ({ left, right }) =>
coreValueCompare(left.get('text'), right.get('text')),
ctx: {
word: 'lorem',
},
});
await query.loadingFinished();
query.results();
query.close();
if (!prevCount) {
prevCount = query.count;
} else {
assert(prevCount === query.count);
}
}
console.log(
`Query finished in ${
(performance.now() - queryStart) / queryIter
} ms.\n# Results = ${prevCount}`,
);
// Deno.exit();
}
if (import.meta.main) testsMain();