|
| 1 | +import itemsjs from '../src/index.js'; |
| 2 | +import { performance } from 'node:perf_hooks'; |
| 3 | + |
| 4 | +const defaultSizes = [1000, 10000, 30000]; |
| 5 | +const sizes = process.env.SIZES |
| 6 | + ? process.env.SIZES.split(',').map((v) => parseInt(v, 10)).filter(Boolean) |
| 7 | + : defaultSizes; |
| 8 | + |
| 9 | +const repeats = parseInt(process.env.REPEAT || '5', 10); |
| 10 | + |
| 11 | +const tagsPool = Array.from({ length: 40 }, (_, i) => `tag${i}`); |
| 12 | +const actorsPool = Array.from({ length: 30 }, (_, i) => `actor${i}`); |
| 13 | +const categories = ['catA', 'catB', 'catC', 'catD']; |
| 14 | + |
| 15 | +function makeItems(count) { |
| 16 | + return Array.from({ length: count }, (_, i) => { |
| 17 | + const t1 = tagsPool[i % tagsPool.length]; |
| 18 | + const t2 = tagsPool[(i * 7) % tagsPool.length]; |
| 19 | + const t3 = tagsPool[(i * 11) % tagsPool.length]; |
| 20 | + const actor = actorsPool[i % actorsPool.length]; |
| 21 | + const category = categories[i % categories.length]; |
| 22 | + const popular = i % 2 === 0; |
| 23 | + |
| 24 | + return { |
| 25 | + id: `id-${i}`, |
| 26 | + name: `Item ${i} ${t1}`, |
| 27 | + tags: [t1, t2, t3], |
| 28 | + actors: [actor], |
| 29 | + category, |
| 30 | + popular, |
| 31 | + }; |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +function average(arr) { |
| 36 | + if (!arr.length) return 0; |
| 37 | + return arr.reduce((a, b) => a + b, 0) / arr.length; |
| 38 | +} |
| 39 | + |
| 40 | +function runScenario(engine, input) { |
| 41 | + const totals = []; |
| 42 | + const facets = []; |
| 43 | + const searchTimes = []; |
| 44 | + const sortingTimes = []; |
| 45 | + |
| 46 | + for (let i = 0; i < repeats; i++) { |
| 47 | + const start = performance.now(); |
| 48 | + const res = engine.search(input); |
| 49 | + const end = performance.now(); |
| 50 | + |
| 51 | + totals.push(end - start); |
| 52 | + facets.push(res.timings?.facets ?? 0); |
| 53 | + searchTimes.push(res.timings?.search ?? 0); |
| 54 | + sortingTimes.push(res.timings?.sorting ?? 0); |
| 55 | + } |
| 56 | + |
| 57 | + return { |
| 58 | + totalMs: average(totals), |
| 59 | + facetsMs: average(facets), |
| 60 | + searchMs: average(searchTimes), |
| 61 | + sortingMs: average(sortingTimes), |
| 62 | + }; |
| 63 | +} |
| 64 | + |
| 65 | +function logResult(size, buildMs, results) { |
| 66 | + console.log(`items: ${size}`); |
| 67 | + console.log(` build (ms): ${buildMs.toFixed(1)}`); |
| 68 | + Object.entries(results).forEach(([name, data]) => { |
| 69 | + console.log( |
| 70 | + ` ${name}: total=${data.totalMs.toFixed(2)}ms facets=${data.facetsMs.toFixed( |
| 71 | + 2, |
| 72 | + )}ms search=${data.searchMs.toFixed(2)}ms sorting=${data.sortingMs.toFixed(2)}ms`, |
| 73 | + ); |
| 74 | + }); |
| 75 | + console.log(''); |
| 76 | +} |
| 77 | + |
| 78 | +function main() { |
| 79 | + console.log( |
| 80 | + `Search benchmark – sizes: ${sizes.join( |
| 81 | + ', ', |
| 82 | + )}, repeats per scenario: ${repeats}`, |
| 83 | + ); |
| 84 | + console.log( |
| 85 | + 'Scenarios: empty, query-only, filters-only, query+filters, boolean filter', |
| 86 | + ); |
| 87 | + console.log(''); |
| 88 | + |
| 89 | + sizes.forEach((size) => { |
| 90 | + const data = makeItems(size); |
| 91 | + const config = { |
| 92 | + searchableFields: ['name', 'tags', 'actors'], |
| 93 | + aggregations: { |
| 94 | + tags: { title: 'Tags', size: tagsPool.length }, |
| 95 | + actors: { title: 'Actors', size: actorsPool.length }, |
| 96 | + category: { title: 'Category', size: categories.length }, |
| 97 | + popular: { title: 'Popular' }, |
| 98 | + }, |
| 99 | + }; |
| 100 | + |
| 101 | + const buildStart = performance.now(); |
| 102 | + const engine = itemsjs(data, config); |
| 103 | + const buildEnd = performance.now(); |
| 104 | + |
| 105 | + const scenarios = { |
| 106 | + empty: {}, |
| 107 | + query: { query: tagsPool[1] }, |
| 108 | + filters: { |
| 109 | + filters: { |
| 110 | + tags: [tagsPool[2]], |
| 111 | + category: [categories[1]], |
| 112 | + }, |
| 113 | + }, |
| 114 | + queryAndFilters: { |
| 115 | + query: tagsPool[3], |
| 116 | + filters: { |
| 117 | + tags: [tagsPool[3]], |
| 118 | + actors: [actorsPool[2]], |
| 119 | + }, |
| 120 | + }, |
| 121 | + booleanFilter: { |
| 122 | + filters: { |
| 123 | + popular: [true], |
| 124 | + }, |
| 125 | + }, |
| 126 | + }; |
| 127 | + |
| 128 | + const results = {}; |
| 129 | + Object.entries(scenarios).forEach(([name, input]) => { |
| 130 | + results[name] = runScenario(engine, input); |
| 131 | + }); |
| 132 | + |
| 133 | + logResult(size, buildEnd - buildStart, results); |
| 134 | + }); |
| 135 | +} |
| 136 | + |
| 137 | +main(); |
0 commit comments