-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlang.bench.ts
More file actions
136 lines (118 loc) · 3.6 KB
/
lang.bench.ts
File metadata and controls
136 lines (118 loc) · 3.6 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
import { InvertedIndexMap } from "./lang";
interface TestRecord {
id: string;
category: string;
price: number;
tags: string[];
brand: string;
inStock: boolean;
rating: number;
}
// Benchmark configuration
const DATASET_SIZES = [1000, 10000, 100000];
const ITERATIONS = 100;
// Test data generators
function generateId(index: number): string {
return `id-${index.toString().padStart(8, "0")}`;
}
function generateTestRecord(index: number): TestRecord {
const categories = ["Electronics", "Clothing", "Food", "Books", "Sports"];
const brands = ["Brand-A", "Brand-B", "Brand-C", "Brand-D", "Brand-E"];
const tags = ["new", "sale", "premium", "limited", "exclusive"];
return {
id: generateId(index),
category: categories[index % categories.length],
price: Math.floor(10 + Math.random() * 990),
tags: [tags[index % tags.length]],
brand: brands[index % brands.length],
inStock: Math.random() > 0.2,
rating: Math.floor(1 + Math.random() * 5),
};
}
// Benchmark utilities
function measureTime(fn: () => void): number {
const start = process.hrtime.bigint();
fn();
const end = process.hrtime.bigint();
return Number(end - start) / 1_000_000; // Convert to milliseconds
}
async function runBenchmark() {
console.log("Starting InvertedIndexMap Benchmark\n");
for (const size of DATASET_SIZES) {
console.log(`=== Dataset Size: ${size.toLocaleString()} records ===`);
// Generate test data
const records = Array.from({ length: size }, (_, i) =>
generateTestRecord(i),
);
// Test different index configurations
const configs = [
{ name: "Single Index", fields: ["category"] },
{ name: "Multiple Indices", fields: ["category", "brand", "inStock"] },
{
name: "All Fields",
fields: ["category", "brand", "inStock", "rating", "price"],
},
];
for (const config of configs) {
console.log(`\nTesting ${config.name}`);
// Initialize index
const index = new InvertedIndexMap<TestRecord>(
(r) => r.id,
config.fields as (keyof TestRecord)[],
);
// Measure insertion time
const insertionTime = measureTime(() => {
for (const record of records) {
index.add(record);
}
});
console.log(`Insertion time: ${insertionTime.toFixed(2)}ms`);
// Test queries
const queries = [
{
name: "Single field query",
query: { category: "Electronics" },
},
{
name: "Two field query",
query: { category: "Electronics", inStock: true },
},
{
name: "Complex query",
query: { category: "Electronics", inStock: true, brand: "Brand-A" },
},
];
for (const testQuery of queries) {
let totalTime = 0;
let totalResults = 0;
for (let i = 0; i < ITERATIONS; i++) {
const queryTime = measureTime(() => {
const results = index.query(testQuery.query);
totalResults = results.length;
});
totalTime += queryTime;
}
const avgTime = totalTime / ITERATIONS;
console.log(
`${testQuery.name}: ${avgTime.toFixed(2)}ms avg ` +
`(${ITERATIONS} iterations, ${totalResults} results)`,
);
}
}
console.log("\n" + "=".repeat(50) + "\n");
}
}
// Run benchmark
console.log(
"Memory usage before:",
process.memoryUsage().heapUsed / 1024 / 1024,
"MB",
);
runBenchmark().then(() => {
console.log(
"Memory usage after:",
process.memoryUsage().heapUsed / 1024 / 1024,
"MB",
);
console.log("Benchmark complete");
});