-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverdue-cards-test.mjs
executable file
·351 lines (293 loc) · 9.01 KB
/
overdue-cards-test.mjs
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import { waitForIdle } from './wait-for-idle.mjs';
import { getTestData } from './test-data.mjs';
import {
log,
logResults,
clear,
prepareTestDatabase,
compareQueryResults,
} from './utils.mjs';
const MS_PER_DAY = 1000 * 60 * 60 * 24;
const EXP_FACTOR = 0.00225;
const output = document.querySelector('output');
export async function runOverdueCardsTest({ numCards, numNotes }) {
clear(output);
const queryResults = [];
try {
log(
`Generating test data for ${numCards} card(s) and ${numNotes} note(s)...`
);
const testData = getTestData({ numCards, numNotes });
log('Running overdueness test...');
queryResults.push(await runViewTest(testData));
queryResults.push(await runIndexTest(testData));
queryResults.push(await runIndexWithIdTest(testData));
queryResults.push(await runNoIndexTest(testData));
log('Re-running all tests a second time in reverse order...');
await runNoIndexTest(testData);
await runIndexWithIdTest(testData);
await runIndexTest(testData);
await runViewTest(testData);
log('Done.');
} catch (e) {
log(e.message, 'error');
}
compareQueryResults(queryResults);
}
// 1. With separate overdueness view (current setup)
async function runViewTest(testData, searchKeys) {
// Prep
log('1. Overdueness view', 'heading');
log('Preparing database...');
const db = await prepareTestDatabase(testData);
await waitForIdle();
log('Running test...');
const reviewTime = new Date();
// Run test
const startTime = performance.now();
await db.put({
_id: `_design/overdueness`,
views: {
overdueness: {
map: `function(doc) {
if (
!doc._id.startsWith('progress-') ||
typeof doc.level !== 'number' ||
typeof doc.due !== 'number'
) {
return;
}
if (doc.level === 0) {
// Unfortunately 'Infinity' doesn't seem to work here
emit(Number.MAX_VALUE, {
_id: 'card-' + doc._id.substr('progress-'.length),
progress: {
level: 0,
due: doc.due,
},
});
return;
}
const daysOverdue = (${reviewTime.getTime()} - doc.due) / ${MS_PER_DAY};
const linearComponent = daysOverdue / doc.level;
const expComponent = Math.exp(${EXP_FACTOR} * daysOverdue) - 1;
const overdueValue = linearComponent + expComponent;
emit(overdueValue, {
_id: 'card-' + doc._id.substr('progress-'.length),
progress: {
level: doc.level,
due: doc.due,
}
});
}`,
},
},
});
await db.query('overdueness', { limit: 0 });
const indexCreationTimeMs = performance.now() - startTime;
const timeResults = [];
let queryResult;
for (let i = 0; i < 5; i++) {
const runStartTime = performance.now();
const result = await db.query('overdueness', {
include_docs: true,
descending: true,
endkey: 0,
});
queryResult = result.rows.map(row => ({
...row.doc,
progress: row.value.progress,
}));
timeResults.push(performance.now() - runStartTime);
}
const durationMs = performance.now() - startTime;
// Clean up
await db.destroy();
log(`Index creation took ${indexCreationTimeMs}ms`);
logResults(durationMs, timeResults);
return queryResult;
}
// 2. Using an index on due field
async function runIndexTest(testData, searchKeys) {
// Prep
log('2. Due index', 'heading');
log('Preparing database...');
const db = await prepareTestDatabase(testData);
await waitForIdle();
log('Running test...');
const reviewTime = new Date();
// Run test
const startTime = performance.now();
await db.createIndex({
index: {
fields: ['due'],
name: 'due',
ddoc: 'progress_by_due_date',
},
});
const indexCreationTimeMs = performance.now() - startTime;
const timeResults = [];
let queryResult;
for (let i = 0; i < 5; i++) {
const runStartTime = performance.now();
const findResult = await db.find({
selector: {
_id: { $gt: 'progress-', $lt: 'progress-\ufff0' },
due: { $gt: 0, $lte: reviewTime.getTime() },
},
use_index: ['progress_by_due_date', 'due'],
});
const reviewTimeAsNumber = reviewTime.getTime();
const progressByOverdueness = [];
for (const doc of findResult.docs) {
progressByOverdueness.push([
getOverdueness(doc, reviewTimeAsNumber),
doc,
]);
}
progressByOverdueness.sort((a, b) => b[0] - a[0]);
const progressDocs = progressByOverdueness.map(([_, doc]) => doc);
queryResult = await getCardsFromProgressDocs(db, progressDocs);
timeResults.push(performance.now() - runStartTime);
}
const durationMs = performance.now() - startTime;
// Clean up
await db.destroy();
log(`Index creation took ${indexCreationTimeMs}ms`);
logResults(durationMs, timeResults);
return queryResult;
}
// 3. Using an index on due and ID fields
//
// XXX This currently produces wrong results when using indexeddb adapter on
// both Firefox and Chrome. It works with the idb adapter but takes longer so
// it's possibly just wrong.
async function runIndexWithIdTest(testData, searchKeys) {
// Prep
log('3. Due and ID index', 'heading');
log('Preparing database...');
const db = await prepareTestDatabase(testData);
await waitForIdle();
log('Running test...');
const reviewTime = new Date();
// Run test
const startTime = performance.now();
await db.createIndex({
index: {
fields: ['_id', 'due'],
name: 'due',
ddoc: 'progress_by_due_date',
},
});
const indexCreationTimeMs = performance.now() - startTime;
const timeResults = [];
let queryResult;
for (let i = 0; i < 5; i++) {
const runStartTime = performance.now();
const findResult = await db.find({
selector: {
_id: { $gt: 'progress-', $lt: 'progress-\ufff0' },
due: { $gt: 0, $lte: reviewTime.getTime() },
},
use_index: ['progress_by_due_date', 'due'],
});
const reviewTimeAsNumber = reviewTime.getTime();
const progressByOverdueness = [];
for (const doc of findResult.docs) {
progressByOverdueness.push([
getOverdueness(doc, reviewTimeAsNumber),
doc,
]);
}
progressByOverdueness.sort((a, b) => b[0] - a[0]);
const progressDocs = progressByOverdueness.map(([_, doc]) => doc);
queryResult = await getCardsFromProgressDocs(db, progressDocs);
timeResults.push(performance.now() - runStartTime);
}
const durationMs = performance.now() - startTime;
// Clean up
await db.destroy();
log(`Index creation took ${indexCreationTimeMs}ms`);
logResults(durationMs, timeResults);
return queryResult;
}
// 4. Using no index
async function runNoIndexTest(testData, searchKeys) {
// Prep
log('4. No index', 'heading');
log('Preparing database...');
const db = await prepareTestDatabase(testData);
await waitForIdle();
log('Running test...');
const reviewTime = new Date();
// Run test
const startTime = performance.now();
const timeResults = [];
let queryResult;
for (let i = 0; i < 5; i++) {
const runStartTime = performance.now();
const findResult = await db.find({
selector: {
_id: { $gt: 'progress-', $lt: 'progress-\ufff0' },
due: { $gt: 0, $lte: reviewTime.getTime() },
},
});
const reviewTimeAsNumber = reviewTime.getTime();
const progressByOverdueness = [];
for (const doc of findResult.docs) {
progressByOverdueness.push([
getOverdueness(doc, reviewTimeAsNumber),
doc,
]);
}
progressByOverdueness.sort((a, b) => b[0] - a[0]);
const progressDocs = progressByOverdueness.map(([_, doc]) => doc);
queryResult = await getCardsFromProgressDocs(db, progressDocs);
timeResults.push(performance.now() - runStartTime);
}
const durationMs = performance.now() - startTime;
// Clean up
await db.destroy();
logResults(durationMs, timeResults);
return queryResult;
}
function getOverdueness(doc, reviewTimeAsNumber) {
const daysOverdue = (reviewTimeAsNumber - doc.due) / MS_PER_DAY;
const linearComponent = daysOverdue / doc.level;
const expComponent = Math.exp(EXP_FACTOR * daysOverdue) - 1;
return linearComponent + expComponent;
}
async function getCardsFromProgressDocs(db, progressDocs) {
const keys = progressDocs.map(doc => doc._id.replace('progress-', 'card-'));
const cards = await db.allDocs({
include_docs: true,
keys,
});
if (cards.rows.length !== progressDocs.length) {
throw new Error('Got mismatched number of card records');
}
const result = [];
for (let i = 0; i < cards.rows.length; i++) {
const cardDoc = cards.rows[i];
if (!cardDoc.doc || cardDoc.error) {
console.warn(
`Got missing card for progress record ${progressDocs[i]._id}`
);
continue;
}
if (cardDoc.value.deleted) {
console.warn(
`Got deleted card for progress record ${progressDocs[i]._id}`
);
continue;
}
result.push({
...cardDoc.doc,
progress: {
level: progressDocs[i].level,
due: progressDocs[i].due,
},
});
}
return result;
}