-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathsearch.js
More file actions
164 lines (144 loc) · 4.45 KB
/
search.js
File metadata and controls
164 lines (144 loc) · 4.45 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
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
import itemsjs from '../src/index.js';
import { performance } from 'node:perf_hooks';
const defaultSizes = [1000, 10000, 30000];
const sizes = process.env.SIZES
? process.env.SIZES.split(',').map((v) => parseInt(v, 10)).filter(Boolean)
: defaultSizes;
const repeats = parseInt(process.env.REPEAT || '5', 10);
const extraFacetsCount = parseInt(process.env.EXTRA_FACETS || '0', 10);
const extraFacetValues = ['a', 'b', 'c'];
const tagsPool = Array.from({ length: 40 }, (_, i) => `tag${i}`);
const actorsPool = Array.from({ length: 30 }, (_, i) => `actor${i}`);
const categories = ['catA', 'catB', 'catC', 'catD'];
function makeItems(count) {
return Array.from({ length: count }, (_, i) => {
const t1 = tagsPool[i % tagsPool.length];
const t2 = tagsPool[(i * 7) % tagsPool.length];
const t3 = tagsPool[(i * 11) % tagsPool.length];
const actor = actorsPool[i % actorsPool.length];
const category = categories[i % categories.length];
const popular = i % 2 === 0;
return {
id: `id-${i}`,
name: `Item ${i} ${t1}`,
tags: [t1, t2, t3],
actors: [actor],
category,
popular,
...makeExtraFacets(i),
};
});
}
function makeExtraFacets(index) {
const result = {};
for (let j = 0; j < extraFacetsCount; j++) {
const val = extraFacetValues[(index + j) % extraFacetValues.length];
result[`facet_${j}`] = val;
}
return result;
}
function average(arr) {
if (!arr.length) return 0;
return arr.reduce((a, b) => a + b, 0) / arr.length;
}
function runScenario(engine, input) {
const totals = [];
const facets = [];
const searchTimes = [];
const sortingTimes = [];
for (let i = 0; i < repeats; i++) {
const start = performance.now();
const res = engine.search(input);
const end = performance.now();
totals.push(end - start);
facets.push(res.timings?.facets ?? 0);
searchTimes.push(res.timings?.search ?? 0);
sortingTimes.push(res.timings?.sorting ?? 0);
}
return {
totalMs: average(totals),
facetsMs: average(facets),
searchMs: average(searchTimes),
sortingMs: average(sortingTimes),
};
}
function logResult(size, buildMs, results) {
console.log(`items: ${size}`);
console.log(
` facets: tags(${tagsPool.length}), actors(${actorsPool.length}), category(${categories.length}), popular(boolean)`,
);
if (extraFacetsCount > 0) {
console.log(` extra facets: ${extraFacetsCount} (3 values each)`);
}
console.log(
' fields: name (boosted), tags, actors; each item has 3 tags, 1 actor, 1 category, boolean popular',
);
console.log(` build (ms): ${buildMs.toFixed(1)}`);
Object.entries(results).forEach(([name, data]) => {
console.log(
` ${name}: total=${data.totalMs.toFixed(2)}ms facets=${data.facetsMs.toFixed(
2,
)}ms search=${data.searchMs.toFixed(2)}ms sorting=${data.sortingMs.toFixed(2)}ms`,
);
});
console.log('');
}
function main() {
console.log(
`Search benchmark – sizes: ${sizes.join(
', ',
)}, repeats per scenario: ${repeats}`,
);
console.log(
'Scenarios: empty, query-only, filters-only, query+filters, boolean filter',
);
console.log('');
sizes.forEach((size) => {
const data = makeItems(size);
const config = {
searchableFields: ['name', 'tags', 'actors'],
aggregations: {
tags: { title: 'Tags', size: tagsPool.length },
actors: { title: 'Actors', size: actorsPool.length },
category: { title: 'Category', size: categories.length },
popular: { title: 'Popular' },
},
};
if (extraFacetsCount > 0) {
for (let i = 0; i < extraFacetsCount; i++) {
config.aggregations[`facet_${i}`] = { title: `Facet ${i}` };
}
}
const buildStart = performance.now();
const engine = itemsjs(data, config);
const buildEnd = performance.now();
const scenarios = {
empty: {},
query: { query: tagsPool[1] },
filters: {
filters: {
tags: [tagsPool[2]],
category: [categories[1]],
},
},
queryAndFilters: {
query: tagsPool[3],
filters: {
tags: [tagsPool[3]],
actors: [actorsPool[2]],
},
},
booleanFilter: {
filters: {
popular: [true],
},
},
};
const results = {};
Object.entries(scenarios).forEach(([name, input]) => {
results[name] = runScenario(engine, input);
});
logResult(size, buildEnd - buildStart, results);
});
}
main();