bunsane 0.2.3 → 0.2.7
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/config/cache.config.ts +2 -0
- package/core/ArcheType.ts +67 -34
- package/core/BatchLoader.ts +215 -30
- package/core/Entity.ts +2 -2
- package/core/RequestContext.ts +15 -10
- package/core/RequestLoaders.ts +4 -2
- package/core/cache/CacheFactory.ts +3 -1
- package/core/cache/CacheProvider.ts +1 -0
- package/core/cache/CacheWarmer.ts +45 -23
- package/core/cache/MemoryCache.ts +10 -1
- package/core/cache/RedisCache.ts +26 -7
- package/core/validateEnv.ts +8 -0
- package/database/DatabaseHelper.ts +113 -1
- package/database/index.ts +78 -45
- package/docs/SCALABILITY_PLAN.md +175 -0
- package/package.json +13 -2
- package/query/CTENode.ts +44 -24
- package/query/ComponentInclusionNode.ts +181 -91
- package/query/Query.ts +9 -9
- package/tests/benchmark/BENCHMARK_DATABASES_PLAN.md +338 -0
- package/tests/benchmark/bunfig.toml +9 -0
- package/tests/benchmark/fixtures/EcommerceComponents.ts +283 -0
- package/tests/benchmark/fixtures/EcommerceDataGenerators.ts +301 -0
- package/tests/benchmark/fixtures/RelationTracker.ts +159 -0
- package/tests/benchmark/fixtures/index.ts +6 -0
- package/tests/benchmark/index.ts +22 -0
- package/tests/benchmark/noop-preload.ts +3 -0
- package/tests/benchmark/runners/BenchmarkLoader.ts +132 -0
- package/tests/benchmark/runners/index.ts +4 -0
- package/tests/benchmark/scenarios/query-benchmarks.test.ts +465 -0
- package/tests/benchmark/scripts/generate-db.ts +344 -0
- package/tests/benchmark/scripts/run-benchmarks.ts +97 -0
- package/tests/integration/query/Query.complexAnalysis.test.ts +557 -0
- package/tests/integration/query/Query.explainAnalyze.test.ts +233 -0
- package/tests/stress/fixtures/RealisticComponents.ts +235 -0
- package/tests/stress/scenarios/realistic-scenarios.test.ts +1081 -0
- package/tests/stress/scenarios/timeout-investigation.test.ts +522 -0
- package/tests/unit/BatchLoader.test.ts +139 -25
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Data generators for benchmark e-commerce components.
|
|
3
|
+
*
|
|
4
|
+
* Uses deterministic seeding for reproducible data generation.
|
|
5
|
+
* Implements realistic distributions (power-law, exponential).
|
|
6
|
+
*/
|
|
7
|
+
import {
|
|
8
|
+
USER_TIERS, USER_STATUSES, PRODUCT_STATUSES, ORDER_STATUSES,
|
|
9
|
+
ORDER_ITEM_STATUSES, PAYMENT_METHODS, REGIONS, CATEGORIES,
|
|
10
|
+
SUBCATEGORIES, BRANDS
|
|
11
|
+
} from './EcommerceComponents';
|
|
12
|
+
import type { RelationTracker } from './RelationTracker';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Seeded random number generator (Mulberry32)
|
|
16
|
+
*/
|
|
17
|
+
export class SeededRandom {
|
|
18
|
+
private state: number;
|
|
19
|
+
|
|
20
|
+
constructor(seed: number) {
|
|
21
|
+
this.state = seed;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
next(): number {
|
|
25
|
+
let t = this.state += 0x6D2B79F5;
|
|
26
|
+
t = Math.imul(t ^ t >>> 15, t | 1);
|
|
27
|
+
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
|
|
28
|
+
return ((t ^ t >>> 14) >>> 0) / 4294967296;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
nextInt(min: number, max: number): number {
|
|
32
|
+
return Math.floor(this.next() * (max - min + 1)) + min;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
pick<T>(arr: readonly T[]): T {
|
|
36
|
+
return arr[Math.floor(this.next() * arr.length)]!;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
pickWeighted<T>(arr: readonly T[], weights: number[]): T {
|
|
40
|
+
const total = weights.reduce((a, b) => a + b, 0);
|
|
41
|
+
let r = this.next() * total;
|
|
42
|
+
for (let i = 0; i < arr.length; i++) {
|
|
43
|
+
r -= weights[i]!;
|
|
44
|
+
if (r <= 0) return arr[i]!;
|
|
45
|
+
}
|
|
46
|
+
return arr[arr.length - 1]!;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Power-law distribution (Pareto-like)
|
|
51
|
+
* Used for category popularity, user activity, etc.
|
|
52
|
+
*/
|
|
53
|
+
powerLaw(min: number, max: number, alpha: number = 1.5): number {
|
|
54
|
+
const u = this.next();
|
|
55
|
+
const minP = Math.pow(min, 1 - alpha);
|
|
56
|
+
const maxP = Math.pow(max, 1 - alpha);
|
|
57
|
+
return Math.pow((maxP - minP) * u + minP, 1 / (1 - alpha));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Exponential distribution
|
|
62
|
+
* Used for order frequency, review counts, etc.
|
|
63
|
+
*/
|
|
64
|
+
exponential(lambda: number = 1): number {
|
|
65
|
+
return -Math.log(1 - this.next()) / lambda;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const FIRST_NAMES = [
|
|
70
|
+
'James', 'Mary', 'John', 'Patricia', 'Robert', 'Jennifer', 'Michael', 'Linda',
|
|
71
|
+
'William', 'Elizabeth', 'David', 'Barbara', 'Richard', 'Susan', 'Joseph', 'Jessica',
|
|
72
|
+
'Thomas', 'Sarah', 'Charles', 'Karen', 'Christopher', 'Lisa', 'Daniel', 'Nancy',
|
|
73
|
+
'Alex', 'Emma', 'Olivia', 'Liam', 'Noah', 'Ava', 'Sophia', 'Isabella'
|
|
74
|
+
];
|
|
75
|
+
|
|
76
|
+
const LAST_NAMES = [
|
|
77
|
+
'Smith', 'Johnson', 'Williams', 'Brown', 'Jones', 'Garcia', 'Miller', 'Davis',
|
|
78
|
+
'Rodriguez', 'Martinez', 'Hernandez', 'Lopez', 'Gonzalez', 'Wilson', 'Anderson',
|
|
79
|
+
'Thomas', 'Taylor', 'Moore', 'Jackson', 'Martin', 'Lee', 'Perez', 'Thompson', 'White'
|
|
80
|
+
];
|
|
81
|
+
|
|
82
|
+
const ADJECTIVES = [
|
|
83
|
+
'Premium', 'Professional', 'Ultra', 'Classic', 'Essential', 'Advanced',
|
|
84
|
+
'Pro', 'Elite', 'Basic', 'Standard', 'Deluxe', 'Supreme', 'Ultimate'
|
|
85
|
+
];
|
|
86
|
+
|
|
87
|
+
const PRODUCT_NOUNS = [
|
|
88
|
+
'Widget', 'Device', 'Tool', 'Kit', 'Set', 'Pack', 'Bundle',
|
|
89
|
+
'System', 'Unit', 'Module', 'Gear', 'Equipment', 'Accessory'
|
|
90
|
+
];
|
|
91
|
+
|
|
92
|
+
const REVIEW_TITLES = [
|
|
93
|
+
'Great product!', 'Exactly what I needed', 'Highly recommended',
|
|
94
|
+
'Good value', 'Works perfectly', 'Exceeded expectations',
|
|
95
|
+
'Not bad', 'Could be better', 'Disappointed', 'Amazing quality',
|
|
96
|
+
'Fast shipping', 'Would buy again', 'Perfect fit', 'Love it'
|
|
97
|
+
];
|
|
98
|
+
|
|
99
|
+
const REVIEW_CONTENT_TEMPLATES = [
|
|
100
|
+
'This product is exactly what I was looking for. {adj} quality and {adj2} performance.',
|
|
101
|
+
'I\'ve been using this for {weeks} weeks now and it\'s been {adj}.',
|
|
102
|
+
'The {feature} is really {adj}. Would recommend to anyone looking for a {type}.',
|
|
103
|
+
'Shipping was fast and the product arrived in perfect condition. Very {adj}.',
|
|
104
|
+
'For the price, this is an {adj} deal. The {feature} works great.',
|
|
105
|
+
'Had some issues initially but customer support was {adj}. Now it works perfectly.',
|
|
106
|
+
'Compared to other {type}s I\'ve tried, this one is by far the most {adj}.',
|
|
107
|
+
'The build quality is {adj}. Feels {adj2} in hand.'
|
|
108
|
+
];
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Generate user data
|
|
112
|
+
*/
|
|
113
|
+
export function generateUserData(index: number, rng: SeededRandom) {
|
|
114
|
+
const firstName = rng.pick(FIRST_NAMES);
|
|
115
|
+
const lastName = rng.pick(LAST_NAMES);
|
|
116
|
+
const username = `${firstName.toLowerCase()}${lastName.toLowerCase()}${index}`;
|
|
117
|
+
|
|
118
|
+
// Power-law distribution for tier (most users are free)
|
|
119
|
+
const tierWeights = [0.6, 0.25, 0.12, 0.03]; // free, basic, premium, enterprise
|
|
120
|
+
const tier = rng.pickWeighted(USER_TIERS, tierWeights);
|
|
121
|
+
|
|
122
|
+
// Most users are active
|
|
123
|
+
const statusWeights = [0.85, 0.12, 0.03];
|
|
124
|
+
const status = rng.pickWeighted(USER_STATUSES, statusWeights);
|
|
125
|
+
|
|
126
|
+
const createdAt = new Date(Date.now() - rng.nextInt(0, 365 * 2) * 24 * 60 * 60 * 1000);
|
|
127
|
+
const hasLoggedIn = rng.next() > 0.1;
|
|
128
|
+
const lastLoginAt = hasLoggedIn
|
|
129
|
+
? new Date(createdAt.getTime() + rng.nextInt(0, Date.now() - createdAt.getTime()))
|
|
130
|
+
: null;
|
|
131
|
+
|
|
132
|
+
return {
|
|
133
|
+
email: `${username}@example.com`,
|
|
134
|
+
username,
|
|
135
|
+
firstName,
|
|
136
|
+
lastName,
|
|
137
|
+
status,
|
|
138
|
+
tier,
|
|
139
|
+
region: rng.pick(REGIONS),
|
|
140
|
+
orderCount: Math.floor(rng.exponential(0.1)),
|
|
141
|
+
totalSpent: Math.floor(rng.exponential(0.01) * 100),
|
|
142
|
+
createdAt,
|
|
143
|
+
lastLoginAt
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Generate product data
|
|
149
|
+
*/
|
|
150
|
+
export function generateProductData(index: number, rng: SeededRandom) {
|
|
151
|
+
const category = rng.pick(CATEGORIES);
|
|
152
|
+
const subcategories = SUBCATEGORIES[category] || ['General'];
|
|
153
|
+
const subcategory = rng.pick(subcategories);
|
|
154
|
+
const brand = rng.pick(BRANDS);
|
|
155
|
+
|
|
156
|
+
const adjective = rng.pick(ADJECTIVES);
|
|
157
|
+
const noun = rng.pick(PRODUCT_NOUNS);
|
|
158
|
+
const name = `${brand} ${adjective} ${noun} ${subcategory}`;
|
|
159
|
+
const sku = `${category.substring(0, 3).toUpperCase()}-${index.toString().padStart(6, '0')}`;
|
|
160
|
+
|
|
161
|
+
const basePrice = Math.floor(rng.powerLaw(10, 1000, 1.2));
|
|
162
|
+
const cost = Math.floor(basePrice * (0.3 + rng.next() * 0.3));
|
|
163
|
+
|
|
164
|
+
// Power-law for stock (most products have moderate stock)
|
|
165
|
+
const stock = Math.floor(rng.powerLaw(0, 1000, 1.5));
|
|
166
|
+
|
|
167
|
+
const statusWeights = [0.85, 0.10, 0.05];
|
|
168
|
+
const status = rng.pickWeighted(PRODUCT_STATUSES, statusWeights);
|
|
169
|
+
|
|
170
|
+
return {
|
|
171
|
+
sku,
|
|
172
|
+
name,
|
|
173
|
+
description: `${name} - High quality ${subcategory.toLowerCase()} from ${brand}. Features include premium materials and excellent craftsmanship.`,
|
|
174
|
+
category,
|
|
175
|
+
subcategory,
|
|
176
|
+
brand,
|
|
177
|
+
price: basePrice,
|
|
178
|
+
cost,
|
|
179
|
+
stock,
|
|
180
|
+
status,
|
|
181
|
+
rating: Math.round((3 + rng.next() * 2) * 10) / 10, // 3.0 - 5.0
|
|
182
|
+
reviewCount: Math.floor(rng.exponential(0.05)),
|
|
183
|
+
createdAt: new Date(Date.now() - rng.nextInt(0, 365 * 3) * 24 * 60 * 60 * 1000)
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Generate order data
|
|
189
|
+
*/
|
|
190
|
+
export function generateOrderData(
|
|
191
|
+
index: number,
|
|
192
|
+
rng: SeededRandom,
|
|
193
|
+
tracker: RelationTracker
|
|
194
|
+
) {
|
|
195
|
+
const userId = tracker.getRandomUserId(rng);
|
|
196
|
+
const orderNumber = `ORD-${Date.now().toString(36).toUpperCase()}-${index.toString().padStart(6, '0')}`;
|
|
197
|
+
|
|
198
|
+
// Most orders are delivered
|
|
199
|
+
const statusWeights = [0.05, 0.10, 0.15, 0.60, 0.07, 0.03];
|
|
200
|
+
const status = rng.pickWeighted(ORDER_STATUSES, statusWeights);
|
|
201
|
+
|
|
202
|
+
const subtotal = Math.floor(rng.powerLaw(20, 500, 1.3));
|
|
203
|
+
const tax = Math.floor(subtotal * 0.08);
|
|
204
|
+
const shipping = subtotal > 50 ? 0 : Math.floor(5 + rng.next() * 10);
|
|
205
|
+
const total = subtotal + tax + shipping;
|
|
206
|
+
|
|
207
|
+
const orderedAt = new Date(Date.now() - rng.nextInt(0, 365) * 24 * 60 * 60 * 1000);
|
|
208
|
+
const isShipped = ['shipped', 'delivered'].includes(status);
|
|
209
|
+
const shippedAt = isShipped
|
|
210
|
+
? new Date(orderedAt.getTime() + rng.nextInt(1, 5) * 24 * 60 * 60 * 1000)
|
|
211
|
+
: null;
|
|
212
|
+
const deliveredAt = status === 'delivered' && shippedAt
|
|
213
|
+
? new Date(shippedAt.getTime() + rng.nextInt(1, 7) * 24 * 60 * 60 * 1000)
|
|
214
|
+
: null;
|
|
215
|
+
|
|
216
|
+
return {
|
|
217
|
+
userId,
|
|
218
|
+
orderNumber,
|
|
219
|
+
status,
|
|
220
|
+
subtotal,
|
|
221
|
+
tax,
|
|
222
|
+
shipping,
|
|
223
|
+
total,
|
|
224
|
+
itemCount: rng.nextInt(1, 5),
|
|
225
|
+
paymentMethod: rng.pick(PAYMENT_METHODS),
|
|
226
|
+
shippingRegion: rng.pick(REGIONS),
|
|
227
|
+
orderedAt,
|
|
228
|
+
shippedAt,
|
|
229
|
+
deliveredAt
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Generate order item data
|
|
235
|
+
*/
|
|
236
|
+
export function generateOrderItemData(
|
|
237
|
+
index: number,
|
|
238
|
+
rng: SeededRandom,
|
|
239
|
+
tracker: RelationTracker,
|
|
240
|
+
orderId?: string,
|
|
241
|
+
orderUserId?: string
|
|
242
|
+
) {
|
|
243
|
+
const resolvedOrderId = orderId || tracker.getRandomOrderId(rng);
|
|
244
|
+
const productId = tracker.getRandomProductId(rng);
|
|
245
|
+
const userId = orderUserId || tracker.getOrderUserId(resolvedOrderId) || tracker.getRandomUserId(rng);
|
|
246
|
+
|
|
247
|
+
const quantity = rng.nextInt(1, 3);
|
|
248
|
+
const unitPrice = Math.floor(rng.powerLaw(10, 200, 1.3));
|
|
249
|
+
const discount = rng.next() < 0.3 ? Math.floor(unitPrice * rng.next() * 0.3) : 0;
|
|
250
|
+
const total = (unitPrice - discount) * quantity;
|
|
251
|
+
|
|
252
|
+
const statusWeights = [0.10, 0.80, 0.07, 0.03];
|
|
253
|
+
const status = rng.pickWeighted(ORDER_ITEM_STATUSES, statusWeights);
|
|
254
|
+
|
|
255
|
+
return {
|
|
256
|
+
orderId: resolvedOrderId,
|
|
257
|
+
productId,
|
|
258
|
+
userId,
|
|
259
|
+
quantity,
|
|
260
|
+
unitPrice,
|
|
261
|
+
discount,
|
|
262
|
+
total,
|
|
263
|
+
status
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Generate review data
|
|
269
|
+
*/
|
|
270
|
+
export function generateReviewData(
|
|
271
|
+
index: number,
|
|
272
|
+
rng: SeededRandom,
|
|
273
|
+
tracker: RelationTracker
|
|
274
|
+
) {
|
|
275
|
+
const productId = tracker.getRandomProductId(rng);
|
|
276
|
+
const userId = tracker.getRandomUserId(rng);
|
|
277
|
+
|
|
278
|
+
// Rating distribution skewed toward positive
|
|
279
|
+
const ratingWeights = [0.05, 0.08, 0.15, 0.32, 0.40]; // 1-5 stars
|
|
280
|
+
const rating = rng.pickWeighted([1, 2, 3, 4, 5], ratingWeights);
|
|
281
|
+
|
|
282
|
+
const title = rng.pick(REVIEW_TITLES);
|
|
283
|
+
const template = rng.pick(REVIEW_CONTENT_TEMPLATES);
|
|
284
|
+
const content = template
|
|
285
|
+
.replace('{adj}', rng.pick(['excellent', 'great', 'good', 'decent', 'amazing']))
|
|
286
|
+
.replace('{adj2}', rng.pick(['solid', 'reliable', 'impressive', 'premium']))
|
|
287
|
+
.replace('{weeks}', String(rng.nextInt(1, 12)))
|
|
288
|
+
.replace('{feature}', rng.pick(['design', 'build quality', 'performance', 'value']))
|
|
289
|
+
.replace('{type}', rng.pick(['product', 'item', 'device']));
|
|
290
|
+
|
|
291
|
+
return {
|
|
292
|
+
productId,
|
|
293
|
+
userId,
|
|
294
|
+
rating,
|
|
295
|
+
title,
|
|
296
|
+
content,
|
|
297
|
+
verified: rng.next() > 0.3,
|
|
298
|
+
helpfulCount: Math.floor(rng.exponential(0.2)),
|
|
299
|
+
createdAt: new Date(Date.now() - rng.nextInt(0, 365) * 24 * 60 * 60 * 1000)
|
|
300
|
+
};
|
|
301
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tracks entity IDs during benchmark seeding for foreign key references.
|
|
3
|
+
*
|
|
4
|
+
* Provides methods to retrieve random IDs for establishing relationships
|
|
5
|
+
* between entities (User -> Order -> OrderItem, Product -> Review, etc.)
|
|
6
|
+
*/
|
|
7
|
+
import type { SeededRandom } from './EcommerceDataGenerators';
|
|
8
|
+
|
|
9
|
+
export class RelationTracker {
|
|
10
|
+
private userIds: string[] = [];
|
|
11
|
+
private productIds: string[] = [];
|
|
12
|
+
private orderIds: string[] = [];
|
|
13
|
+
private orderUserMap: Map<string, string> = new Map();
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Add a user ID to the tracker
|
|
17
|
+
*/
|
|
18
|
+
addUser(entityId: string): void {
|
|
19
|
+
this.userIds.push(entityId);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Add multiple user IDs
|
|
24
|
+
*/
|
|
25
|
+
addUsers(entityIds: string[]): void {
|
|
26
|
+
this.userIds.push(...entityIds);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Add a product ID to the tracker
|
|
31
|
+
*/
|
|
32
|
+
addProduct(entityId: string): void {
|
|
33
|
+
this.productIds.push(entityId);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Add multiple product IDs
|
|
38
|
+
*/
|
|
39
|
+
addProducts(entityIds: string[]): void {
|
|
40
|
+
this.productIds.push(...entityIds);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Add an order ID with its associated user ID
|
|
45
|
+
*/
|
|
46
|
+
addOrder(entityId: string, userId: string): void {
|
|
47
|
+
this.orderIds.push(entityId);
|
|
48
|
+
this.orderUserMap.set(entityId, userId);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Add multiple order IDs (without user mapping - use addOrder for relationship tracking)
|
|
53
|
+
*/
|
|
54
|
+
addOrders(entityIds: string[]): void {
|
|
55
|
+
this.orderIds.push(...entityIds);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Get a random user ID using the provided RNG
|
|
60
|
+
*/
|
|
61
|
+
getRandomUserId(rng: SeededRandom): string {
|
|
62
|
+
if (this.userIds.length === 0) {
|
|
63
|
+
throw new Error('No user IDs available. Seed users first.');
|
|
64
|
+
}
|
|
65
|
+
return this.userIds[Math.floor(rng.next() * this.userIds.length)]!;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Get a random product ID using the provided RNG
|
|
70
|
+
*/
|
|
71
|
+
getRandomProductId(rng: SeededRandom): string {
|
|
72
|
+
if (this.productIds.length === 0) {
|
|
73
|
+
throw new Error('No product IDs available. Seed products first.');
|
|
74
|
+
}
|
|
75
|
+
return this.productIds[Math.floor(rng.next() * this.productIds.length)]!;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Get a random order ID using the provided RNG
|
|
80
|
+
*/
|
|
81
|
+
getRandomOrderId(rng: SeededRandom): string {
|
|
82
|
+
if (this.orderIds.length === 0) {
|
|
83
|
+
throw new Error('No order IDs available. Seed orders first.');
|
|
84
|
+
}
|
|
85
|
+
return this.orderIds[Math.floor(rng.next() * this.orderIds.length)]!;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Get the user ID associated with an order
|
|
90
|
+
*/
|
|
91
|
+
getOrderUserId(orderId: string): string | undefined {
|
|
92
|
+
return this.orderUserMap.get(orderId);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Get user IDs with power-law distribution (some users more active)
|
|
97
|
+
*/
|
|
98
|
+
getActiveUserId(rng: SeededRandom): string {
|
|
99
|
+
if (this.userIds.length === 0) {
|
|
100
|
+
throw new Error('No user IDs available. Seed users first.');
|
|
101
|
+
}
|
|
102
|
+
// Power-law: top 20% of users are selected 80% of the time
|
|
103
|
+
const idx = Math.floor(Math.pow(rng.next(), 2) * this.userIds.length);
|
|
104
|
+
return this.userIds[idx]!;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Get product IDs with power-law distribution (popular products)
|
|
109
|
+
*/
|
|
110
|
+
getPopularProductId(rng: SeededRandom): string {
|
|
111
|
+
if (this.productIds.length === 0) {
|
|
112
|
+
throw new Error('No product IDs available. Seed products first.');
|
|
113
|
+
}
|
|
114
|
+
const idx = Math.floor(Math.pow(rng.next(), 1.5) * this.productIds.length);
|
|
115
|
+
return this.productIds[idx]!;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Get counts for reporting
|
|
120
|
+
*/
|
|
121
|
+
getCounts(): { users: number; products: number; orders: number } {
|
|
122
|
+
return {
|
|
123
|
+
users: this.userIds.length,
|
|
124
|
+
products: this.productIds.length,
|
|
125
|
+
orders: this.orderIds.length
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Get all user IDs (for batch operations)
|
|
131
|
+
*/
|
|
132
|
+
getAllUserIds(): string[] {
|
|
133
|
+
return [...this.userIds];
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Get all product IDs (for batch operations)
|
|
138
|
+
*/
|
|
139
|
+
getAllProductIds(): string[] {
|
|
140
|
+
return [...this.productIds];
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Get all order IDs (for batch operations)
|
|
145
|
+
*/
|
|
146
|
+
getAllOrderIds(): string[] {
|
|
147
|
+
return [...this.orderIds];
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Clear all tracked IDs
|
|
152
|
+
*/
|
|
153
|
+
clear(): void {
|
|
154
|
+
this.userIds = [];
|
|
155
|
+
this.productIds = [];
|
|
156
|
+
this.orderIds = [];
|
|
157
|
+
this.orderUserMap.clear();
|
|
158
|
+
}
|
|
159
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PGlite Persistent Benchmark Databases
|
|
3
|
+
*
|
|
4
|
+
* Provides pre-generated databases at various tiers for consistent
|
|
5
|
+
* query performance benchmarking.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* # Generate a database tier
|
|
9
|
+
* bun run bench:generate:xs
|
|
10
|
+
*
|
|
11
|
+
* # Run benchmarks against a tier
|
|
12
|
+
* bun run bench:run:xs
|
|
13
|
+
*
|
|
14
|
+
* Database Tiers:
|
|
15
|
+
* xs: 10k entities (1k users, 2k products, 3k orders, 3k items, 1k reviews)
|
|
16
|
+
* sm: 50k entities (5k users, 10k products, 15k orders, 15k items, 5k reviews)
|
|
17
|
+
* md: 100k entities (10k users, 20k products, 30k orders, 30k items, 10k reviews)
|
|
18
|
+
* lg: 500k entities (50k users, 100k products, 150k orders, 150k items, 50k reviews)
|
|
19
|
+
* xl: 1M entities (100k users, 200k products, 300k orders, 300k items, 100k reviews)
|
|
20
|
+
*/
|
|
21
|
+
export * from './fixtures';
|
|
22
|
+
export * from './runners';
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Loads pre-generated PGlite benchmark databases for running benchmarks.
|
|
3
|
+
*
|
|
4
|
+
* Starts a PGLiteSocketServer connected to the persistent database,
|
|
5
|
+
* allowing the BunSane framework to connect via standard PostgreSQL protocol.
|
|
6
|
+
*/
|
|
7
|
+
import { PGlite } from '@electric-sql/pglite';
|
|
8
|
+
import { PGLiteSocketServer } from '@electric-sql/pglite-socket';
|
|
9
|
+
import { existsSync } from 'node:fs';
|
|
10
|
+
import { join, dirname } from 'node:path';
|
|
11
|
+
import { fileURLToPath } from 'node:url';
|
|
12
|
+
|
|
13
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
14
|
+
const DATABASES_DIR = join(__dirname, '..', 'databases');
|
|
15
|
+
|
|
16
|
+
export type BenchmarkTier = 'xs' | 'sm' | 'md' | 'lg' | 'xl';
|
|
17
|
+
|
|
18
|
+
export interface BenchmarkLoaderOptions {
|
|
19
|
+
tier: BenchmarkTier;
|
|
20
|
+
port?: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface LoadedBenchmark {
|
|
24
|
+
pg: PGlite;
|
|
25
|
+
server: PGLiteSocketServer;
|
|
26
|
+
port: number;
|
|
27
|
+
tier: BenchmarkTier;
|
|
28
|
+
stop: () => Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const TIER_ENTITY_COUNTS: Record<BenchmarkTier, number> = {
|
|
32
|
+
xs: 10_000,
|
|
33
|
+
sm: 50_000,
|
|
34
|
+
md: 100_000,
|
|
35
|
+
lg: 500_000,
|
|
36
|
+
xl: 1_000_000
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Load a pre-generated benchmark database and start the socket server.
|
|
41
|
+
*
|
|
42
|
+
* @throws Error if database doesn't exist (prompts to generate)
|
|
43
|
+
*/
|
|
44
|
+
export async function loadBenchmarkDatabase(options: BenchmarkLoaderOptions): Promise<LoadedBenchmark> {
|
|
45
|
+
const { tier, port = 54321 } = options;
|
|
46
|
+
const dbPath = join(DATABASES_DIR, tier);
|
|
47
|
+
|
|
48
|
+
if (!existsSync(dbPath)) {
|
|
49
|
+
throw new Error(
|
|
50
|
+
`Benchmark database for tier '${tier}' not found at ${dbPath}\n\n` +
|
|
51
|
+
`Generate it first with:\n` +
|
|
52
|
+
` bun tests/benchmark/scripts/generate-db.ts ${tier}\n\n` +
|
|
53
|
+
`Or generate all tiers:\n` +
|
|
54
|
+
` bun tests/benchmark/scripts/generate-db.ts --all`
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
console.log(`[benchmark] Loading ${tier.toUpperCase()} tier database from ${dbPath}...`);
|
|
59
|
+
|
|
60
|
+
// Open persistent database (read-only would be ideal but PGlite doesn't support it)
|
|
61
|
+
const pg = new PGlite(dbPath);
|
|
62
|
+
await pg.waitReady;
|
|
63
|
+
|
|
64
|
+
// Verify database has data
|
|
65
|
+
const countResult = await pg.query<{ count: string }>('SELECT COUNT(*) as count FROM entities');
|
|
66
|
+
const entityCount = parseInt(countResult.rows[0]?.count || '0');
|
|
67
|
+
|
|
68
|
+
if (entityCount === 0) {
|
|
69
|
+
await pg.close();
|
|
70
|
+
throw new Error(
|
|
71
|
+
`Benchmark database for tier '${tier}' exists but is empty.\n` +
|
|
72
|
+
`Regenerate with:\n` +
|
|
73
|
+
` bun tests/benchmark/scripts/generate-db.ts ${tier} --force`
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
console.log(`[benchmark] Loaded ${entityCount.toLocaleString()} entities`);
|
|
78
|
+
|
|
79
|
+
// Start socket server
|
|
80
|
+
const server = new PGLiteSocketServer({ db: pg, port });
|
|
81
|
+
await server.start();
|
|
82
|
+
console.log(`[benchmark] Socket server running on port ${port}`);
|
|
83
|
+
|
|
84
|
+
const stop = async () => {
|
|
85
|
+
console.log('[benchmark] Stopping server...');
|
|
86
|
+
try { await server.stop(); } catch {}
|
|
87
|
+
try { await pg.close(); } catch {}
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
return { pg, server, port, tier, stop };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Get expected entity count for a tier
|
|
95
|
+
*/
|
|
96
|
+
export function getTierEntityCount(tier: BenchmarkTier): number {
|
|
97
|
+
return TIER_ENTITY_COUNTS[tier];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Get tier from environment variable BENCHMARK_TIER
|
|
102
|
+
*/
|
|
103
|
+
export function getTierFromEnv(): BenchmarkTier {
|
|
104
|
+
const tier = process.env.BENCHMARK_TIER || 'xs';
|
|
105
|
+
if (!['xs', 'sm', 'md', 'lg', 'xl'].includes(tier)) {
|
|
106
|
+
console.warn(`Invalid BENCHMARK_TIER '${tier}', defaulting to 'xs'`);
|
|
107
|
+
return 'xs';
|
|
108
|
+
}
|
|
109
|
+
return tier as BenchmarkTier;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Get database path for a tier
|
|
114
|
+
*/
|
|
115
|
+
export function getDatabasePath(tier: BenchmarkTier): string {
|
|
116
|
+
return join(DATABASES_DIR, tier);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Check if a tier's database exists
|
|
121
|
+
*/
|
|
122
|
+
export function databaseExists(tier: BenchmarkTier): boolean {
|
|
123
|
+
return existsSync(getDatabasePath(tier));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Get all available (generated) tiers
|
|
128
|
+
*/
|
|
129
|
+
export function getAvailableTiers(): BenchmarkTier[] {
|
|
130
|
+
const tiers: BenchmarkTier[] = ['xs', 'sm', 'md', 'lg', 'xl'];
|
|
131
|
+
return tiers.filter(tier => databaseExists(tier));
|
|
132
|
+
}
|