@peerbit/indexer-tests 1.0.4 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/benchmarks.ts CHANGED
@@ -4,7 +4,7 @@ import {
4
4
  type Index,
5
5
  type IndexEngineInitProperties,
6
6
  type Indices,
7
- SearchRequest,
7
+ StringMatch,
8
8
  getIdProperty,
9
9
  id,
10
10
  } from "@peerbit/indexer-interface";
@@ -34,6 +34,118 @@ const setup = async <T>(
34
34
  return { indices, store, directory };
35
35
  };
36
36
 
37
+ let preFillCount = 2e4;
38
+ const strinbBenchmark = async (
39
+ createIndicies: (directory?: string) => Indices | Promise<Indices>,
40
+ type: "transient" | "persist" = "transient",
41
+ ) => {
42
+ class StringDocument {
43
+ @id({ type: "string" })
44
+ id: string;
45
+
46
+ @field({ type: "string" })
47
+ string: string;
48
+
49
+ constructor(id: string, string: string) {
50
+ this.id = id;
51
+ this.string = string;
52
+ }
53
+ }
54
+
55
+ const fs = await import("fs");
56
+
57
+ const stringIndexPreFilled = await setup(
58
+ { schema: StringDocument },
59
+ createIndicies,
60
+ type,
61
+ );
62
+ let docCount = preFillCount;
63
+ let fixed = uuid();
64
+ for (let i = 0; i < docCount; i++) {
65
+ await stringIndexPreFilled.store.put(
66
+ new StringDocument(uuid(), i % 100 === 0 ? fixed : uuid()),
67
+ );
68
+ }
69
+
70
+ const stringIndexEmpty = await setup(
71
+ { schema: StringDocument },
72
+ createIndicies,
73
+ type,
74
+ );
75
+
76
+ let done = pDefer();
77
+ const suite = new B.Suite({ delay: 100 });
78
+ suite
79
+ .add("string put - " + type, {
80
+ fn: async (deferred: any) => {
81
+ await stringIndexEmpty.store.put(new StringDocument(uuid(), uuid()));
82
+ deferred.resolve();
83
+ },
84
+ defer: true,
85
+ maxTime: 5,
86
+ })
87
+ .add("string query matching - " + type, {
88
+ fn: async (deferred: any) => {
89
+ const iterator = stringIndexPreFilled.store.iterate({
90
+ query: new StringMatch({ key: "string", value: fixed }),
91
+ });
92
+ await iterator.next(10);
93
+ await iterator.close();
94
+ deferred.resolve();
95
+ },
96
+ defer: true,
97
+ maxTime: 5,
98
+ })
99
+
100
+ .add("string count matching - " + type, {
101
+ fn: async (deferred: any) => {
102
+ await stringIndexPreFilled.store.count({
103
+ query: new StringMatch({ key: "string", value: fixed }),
104
+ });
105
+ deferred.resolve();
106
+ },
107
+ defer: true,
108
+ maxTime: 5,
109
+ })
110
+ .add("string count no-matches - " + type, {
111
+ fn: async (deferred: any) => {
112
+ const out = Math.random() > 0.5 ? true : false;
113
+ await stringIndexPreFilled.store.count({
114
+ query: new StringMatch({ key: "string", value: uuid() }),
115
+ });
116
+ deferred.resolve();
117
+ },
118
+ defer: true,
119
+ maxTime: 5,
120
+ })
121
+ .on("cycle", async (event: any) => {
122
+ // eslint-disable-next-line no-console
123
+ console.log(String(event.target));
124
+ })
125
+ .on("error", (err: any) => {
126
+ throw err;
127
+ })
128
+ .on("complete", async () => {
129
+ await stringIndexEmpty.indices.stop();
130
+ stringIndexEmpty.directory &&
131
+ fs.rmSync(stringIndexEmpty.directory, { recursive: true, force: true });
132
+
133
+ await stringIndexPreFilled.indices.stop();
134
+ stringIndexPreFilled.directory &&
135
+ fs.rmSync(stringIndexPreFilled.directory, {
136
+ recursive: true,
137
+ force: true,
138
+ });
139
+
140
+ done.resolve();
141
+ })
142
+ .on("error", (e) => {
143
+ done.reject(e);
144
+ })
145
+ .run();
146
+ return done.promise;
147
+ };
148
+
37
149
  const boolQueryBenchmark = async (
38
150
  createIndicies: (directory?: string) => Indices | Promise<Indices>,
39
151
  type: "transient" | "persist" = "transient",
@@ -58,7 +170,7 @@ const boolQueryBenchmark = async (
58
170
  createIndicies,
59
171
  type,
60
172
  );
61
- let docCount = 1e4;
173
+ let docCount = preFillCount;
62
174
  for (let i = 0; i < docCount; i++) {
63
175
  await boolIndexPrefilled.store.put(
64
176
  new BoolQueryDocument(uuid(), Math.random() > 0.5 ? true : false),
@@ -77,11 +189,11 @@ const boolQueryBenchmark = async (
77
189
  .add("bool query - " + type, {
78
190
  fn: async (deferred: any) => {
79
191
  const out = Math.random() > 0.5 ? true : false;
80
- await boolIndexPrefilled.store.query(
81
- new SearchRequest({
82
- query: new BoolQuery({ key: "bool", value: out }),
83
- }),
84
- );
192
+ const iterator = await boolIndexPrefilled.store.iterate({
193
+ query: new BoolQuery({ key: "bool", value: out }),
194
+ });
195
+ await iterator.next(10);
196
+ await iterator.close();
85
197
  deferred.resolve();
86
198
  },
87
199
  defer: true,
@@ -159,7 +271,7 @@ const nestedBoolQueryBenchmark = async (
159
271
  type,
160
272
  );
161
273
 
162
- let docCount = 1e4;
274
+ let docCount = preFillCount;
163
275
  for (let i = 0; i < docCount; i++) {
164
276
  await boolIndexPrefilled.store.put(
165
277
  new NestedBoolQueryDocument(uuid(), i % 2 === 0 ? true : false),
@@ -179,12 +291,11 @@ const nestedBoolQueryBenchmark = async (
179
291
  .add("nested bool query - " + type, {
180
292
  fn: async (deferred: any) => {
181
293
  const out = Math.random() > 0.5 ? true : false;
182
- await boolIndexPrefilled.store.query(
183
- new SearchRequest({
184
- query: new BoolQuery({ key: ["nested", "bool"], value: out }),
185
- fetch: 10,
186
- }),
187
- );
294
+ const iterator = await boolIndexPrefilled.store.iterate({
295
+ query: new BoolQuery({ key: ["nested", "bool"], value: out }),
296
+ });
297
+ await iterator.next(10);
298
+ await iterator.close();
188
299
  deferred.resolve();
189
300
  },
190
301
  defer: true,
@@ -276,16 +387,15 @@ const shapedQueryBenchmark = async (
276
387
  const suite = new B.Suite({ delay: 100 });
277
388
  let fetch = 10;
278
389
  suite
279
- .add("unshaped - " + type, {
390
+ .add("unshaped nested query - " + type, {
280
391
  fn: async (deferred: any) => {
281
392
  const out = Math.random() > 0.5 ? true : false;
282
- const results = await boolIndexPrefilled.store.query(
283
- new SearchRequest({
284
- query: new BoolQuery({ key: ["nested", "bool"], value: out }),
285
- fetch,
286
- }),
287
- );
288
- if (results.results.length !== fetch) {
393
+ let iterator = await boolIndexPrefilled.store.iterate({
394
+ query: new BoolQuery({ key: ["nested", "bool"], value: out }),
395
+ });
396
+ const results = await iterator.next(fetch);
397
+ await iterator.close();
398
+ if (results.length !== fetch) {
289
399
  throw new Error("Missing results");
290
400
  }
291
401
  deferred.resolve();
@@ -294,17 +404,18 @@ const shapedQueryBenchmark = async (
294
404
  maxTime: 5,
295
405
  async: true,
296
406
  })
297
- .add("shaped - " + type, {
407
+ .add("shaped nested query - " + type, {
298
408
  fn: async (deferred: any) => {
299
409
  const out = Math.random() > 0.5 ? true : false;
300
- const results = await boolIndexPrefilled.store.query(
301
- new SearchRequest({
410
+ const iterator = await boolIndexPrefilled.store.iterate(
411
+ {
302
412
  query: new BoolQuery({ key: ["nested", "bool"], value: out }),
303
- fetch,
304
- }),
413
+ },
305
414
  { shape: { id: true } },
306
415
  );
307
- if (results.results.length !== fetch) {
416
+ const results = await iterator.next(fetch);
417
+ await iterator.close();
418
+ if (results.length !== fetch) {
308
419
  throw new Error("Missing results");
309
420
  }
310
421
  deferred.resolve();
@@ -334,9 +445,10 @@ const shapedQueryBenchmark = async (
334
445
  };
335
446
 
336
447
  export const benchmarks = async (
337
- createIndicies: <T>(directory?: string) => Indices | Promise<Indices>,
448
+ createIndicies: (directory?: string) => Indices | Promise<Indices>,
338
449
  type: "transient" | "persist" = "transient",
339
450
  ) => {
451
+ await strinbBenchmark(createIndicies, type);
340
452
  await shapedQueryBenchmark(createIndicies, type);
341
453
  await boolQueryBenchmark(createIndicies, type);
342
454
  await nestedBoolQueryBenchmark(createIndicies, type);