-
Notifications
You must be signed in to change notification settings - Fork 0
/
message-store-benchmark.js
276 lines (254 loc) · 10.3 KB
/
message-store-benchmark.js
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
//ts-ignore
import { SortDirection } from '@tbd54566975/dwn-sdk-js';
import { TestDataGenerator } from '@tbd54566975/dwn-sdk-js/dist/esm/tests/utils/test-data-generator.js';
import { Time } from '@tbd54566975/dwn-sdk-js/dist/esm/src/utils/time.js';
export const runMessageStoreBenchmark = async (messageStore , items = 10_000) => {
const tenant = 'did:xyz:alice';
const benchMarkResults = {
create : 0,
clearBefore : 0,
put : 0,
paginationSmallSubset : 0,
paginationSmallSubsetDesc : 0,
paginationLargeSubset : 0,
paginationNonOwner : 0,
paginationDescNonOwner : 0,
querySmallSetEqual : 0,
queryRange : 0,
queryEqual : 0,
queryMultipleEqual : 0,
queryGTRange : 0,
queryLTRange : 0,
queryMultipleRange : 0,
clearAfter : 0
};
console.log('message store benchmarks');
// pre-generate messages
const insertMessages = Array(items).fill(0).map((_,i) => {
// random schema from 1-5
const schemaId = Math.floor(Math.random() * 5) + 1;
const schema = `schema${schemaId}`;
//random protocol from 1-10
const protocolId = Math.floor(Math.random() * 9);
const protocol = `proto${protocolId}`;
const bobId = i % 25;
const recipient = `bob${bobId + 1}`;
const author = i % 50 === 0 ? 'bob1' : 'alice';
const published = i % 100 === 0 ? true : false;
let year;
const mod = i % 3;
switch (mod) {
case 0:
year = 2022;
break;
case 1:
year = 2023;
break;
default:
year = 2024;
}
const messageTimestamp = TestDataGenerator.randomTimestamp({ year });
const dateCreated = TestDataGenerator.randomTimestamp({ year });
const message = {
descriptor: {
interface : 'Records',
method : 'Write',
messageTimestamp
}
};
const indexes = {
...message.descriptor,
schema,
protocol,
dateCreated,
recipient,
author,
published,
};
return { message, indexes };
});
// create
const createStart = Date.now();
await messageStore.open();
const createEnd = Date.now();
benchMarkResults.create = createEnd - createStart;
console.log('\tcreate\t\t\t\t:', createEnd - createStart, 'ms');
// clear - before
const clearBeforeStart = Date.now();
await messageStore.clear();
const clearBeforeEnd = Date.now();
console.log('\tclear - before\t\t\t:', clearBeforeEnd - clearBeforeStart, 'ms');
benchMarkResults.clearBefore = clearBeforeEnd - clearBeforeStart;
// put
const putStart = Date.now();
await Promise.all(insertMessages.map(({ message, indexes }) => messageStore.put(tenant, message, indexes)));
const putEnd = Date.now();
console.log('\tput\t\t\t\t:', putEnd - putStart, 'ms');
benchMarkResults.put = putEnd - putStart;
const firstDayOf2024 = Time.createTimestamp({ year: 2024, month: 1, day: 1 });
// advanced query
const ascOrder = { messageTimestamp: SortDirection.Ascending };
const descOrder = { messageTimestamp: SortDirection.Descending };
// paginate 10 pages of 20 results for a specific schema
// note: published: true is a smaller subset so will perform better if index optimizes for equality filter
let page = 0;
let cursor= undefined;
let messages= [];
let results = [];
const paginationStart = Date.now();
while (page < 10) {
page++;
({ messages, cursor } = await messageStore.query(tenant, [
{ published: true, schema: 'schema2', protocol: 'proto6' }
], ascOrder, { limit: 20, cursor } ));
results.push(...messages);
if (cursor === undefined) {
break;
}
}
const paginationEnd = Date.now();
console.log('\tpagination small subset\t\t:', paginationEnd - paginationStart, 'ms', 'results ', results.length);
benchMarkResults.paginationSmallSubset = paginationEnd - paginationStart;
// descending order
results = [];
page = 0;
cursor = undefined;
const paginationDescStart = Date.now();
while (page < 10) {
page++;
({ messages, cursor } = await messageStore.query(tenant, [
{ published: true, schema: 'schema2', protocol: 'proto6' }
], descOrder, { limit: 20, cursor } ));
results.push(...messages);
if (cursor === undefined) {
break;
}
}
const paginationDescEnd = Date.now();
console.log('\tpagination small subset des\t:', paginationDescEnd - paginationDescStart, 'ms', ' results', results.length);
benchMarkResults.paginationSmallSubsetDesc = paginationDescEnd - paginationDescStart;
// filter for a larger result set.
results = [];
page = 0;
cursor = undefined;
const paginationLargeStart = Date.now();
while (page < 10) {
page++;
({ messages, cursor } = await messageStore.query(tenant, [
{ published: true, schema: 'schema2', protocol: 'proto6' },
{ published: false, schema: 'schema2', protocol: 'proto6' }
], ascOrder, { limit: 20, cursor } ));
results.push(...messages);
if (cursor === undefined) {
break;
}
}
const paginationLargeEnd = Date.now();
console.log('\tpagination large subset\t\t:', paginationLargeEnd - paginationLargeStart, 'ms', ' results', results.length);
benchMarkResults.paginationLargeSubset = paginationLargeEnd - paginationLargeStart;
// ascending multiple filters. similar to non-owner query
results = [];
page = 0;
cursor = undefined;
const paginationNonOwnerStart = Date.now();
while (page < 10) {
page++;
({ messages, cursor } = await messageStore.query(tenant, [
{ schema: 'schema2', published: false, author: 'bob1', protocol: 'proto6' },
{ schema: 'schema2', published: true, protocol: 'proto6' },
{ schema: 'schema2', published: false, recipient: 'bob1', protocol: 'proto6' },
], ascOrder, { limit: 20, cursor } ));
results.push(...messages);
if (cursor === undefined) {
break;
}
}
const paginationNonOwnerEnd = Date.now();
console.log('\tpagination non owner\t\t:', paginationNonOwnerEnd - paginationNonOwnerStart, 'ms', ' results', results.length);
benchMarkResults.paginationNonOwner = paginationNonOwnerEnd - paginationNonOwnerStart;
// descending multiple filters. similar to non-owner query
results = [];
page = 0;
cursor = undefined;
const paginationDescNonOwnerStart = Date.now();
while (page < 10) {
page++;
({ messages, cursor } = await messageStore.query(tenant, [
{ schema: 'schema2', published: false, author: 'bob1', protocol: 'proto6' },
{ schema: 'schema2', published: true, protocol: 'proto6' },
{ schema: 'schema2', published: false, recipient: 'bob1', protocol: 'proto6' },
], descOrder, { limit: 20, cursor } ));
results.push(...messages);
if (cursor === undefined) {
break;
}
}
const paginationDescNonOwnerEnd = Date.now();
console.log('\tpagination desc non owner\t:', paginationDescNonOwnerEnd - paginationDescNonOwnerStart, 'ms', ' results', results.length);
benchMarkResults.paginationDescNonOwner = paginationDescNonOwnerEnd - paginationDescNonOwnerStart;
const smallResultSetStart = Date.now();
({ messages } = await messageStore.query(tenant, [{ published: true, recipient: 'bob1' }]));
const smallResultSetEnd = Date.now();
console.log('\tquery asc - small set equal\t:', smallResultSetEnd - smallResultSetStart, 'ms');
console.log('\t\tresults count\t\t:', messages.length);
benchMarkResults.querySmallSetEqual = smallResultSetEnd - smallResultSetStart;
const lastDayOf2022 = Time.createTimestamp({ year: 2022, month: 12, day: 31 });
const lastDayOf2023 = Time.createTimestamp({ year: 2023, month: 12, day: 31 });
const queryRangeStart = Date.now();
({ messages } = await messageStore.query(tenant, [{
dateCreated: { gt: lastDayOf2022, lt: lastDayOf2023 }
}]));
const queryRangeEnd = Date.now();
console.log('\tquery - range\t\t\t:', queryRangeEnd - queryRangeStart, 'ms');
console.log('\t\tresults count\t\t:', messages.length);
benchMarkResults.queryRange = queryRangeEnd - queryRangeStart;
// larger result set
const queryEqualStart = Date.now();
({ messages } = await messageStore.query(tenant, [{ schema: 'schema2' }]));
const queryEqualEnd = Date.now();
console.log('\tquery - equal\t\t\t:', queryEqualEnd - queryEqualStart, 'ms');
console.log('\t\tresults count\t\t:', messages.length);
benchMarkResults.queryEqual = queryEqualEnd - queryEqualStart;
// multiple queries
const multipleEqualStart = Date.now();
({ messages } = await messageStore.query(tenant, [{ schema: ['schema2', 'schema1'] }, { published: true }]));
const multipleEqualEnd = Date.now();
console.log('\tquery - multiple equal\t\t:', multipleEqualEnd - multipleEqualStart, 'ms');
console.log('\t\tresults count\t\t:', messages.length);
benchMarkResults.queryMultipleEqual = multipleEqualEnd - multipleEqualStart;
//range queries
// gt
const queryGTRangeStart = Date.now();
({ messages } = await messageStore.query(tenant, [{
dateCreated: { gt: lastDayOf2022 }
}]));
const queryGTRangeEnd = Date.now();
console.log('\tquery - gt range\t\t:', queryGTRangeEnd - queryGTRangeStart, 'ms');
console.log('\t\tresults count\t\t:', messages.length);
benchMarkResults.queryGTRange = queryGTRangeEnd - queryGTRangeStart;
// lt
const queryLTRangeStart = Date.now();
({ messages } = await messageStore.query(tenant, [{
dateCreated: { lt: lastDayOf2022 }
}]));
const queryLTRangeEnd = Date.now();
console.log('\tquery - lt range\t\t:', queryLTRangeEnd - queryLTRangeStart, 'ms');
console.log('\t\tresults count\t\t:', messages.length);
benchMarkResults.queryLTRange = queryLTRangeEnd - queryLTRangeStart;
// query - range multiple
const multipleRangeStart = Date.now();
({ messages } = await messageStore.query(tenant, [
{ dateCreated: { gt: lastDayOf2022 } },
{ dateCreated: { lt: firstDayOf2024, gt: lastDayOf2023 } },
]));
const multipleRangeEnd = Date.now();
console.log('\tquery - multiple range\t\t:', multipleRangeEnd - multipleRangeStart, 'ms');
console.log('\t\tresults count\t\t:', messages.length);
benchMarkResults.queryMultipleRange = multipleRangeEnd - multipleRangeStart;
// clear - after
const clearAfterStart = Date.now();
await messageStore.clear();
const clearAfterEnd = Date.now();
console.log('\tclear - after\t\t\t:', clearAfterEnd - clearAfterStart, 'ms');
return benchMarkResults;
};