bunsane 0.3.2 → 0.5.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.
Files changed (220) hide show
  1. package/CHANGELOG.md +471 -370
  2. package/core/BatchLoader.ts +56 -32
  3. package/core/Entity.ts +93 -1020
  4. package/core/EntityHookManager.ts +52 -754
  5. package/core/Logger.ts +10 -0
  6. package/core/RequestContext.ts +94 -85
  7. package/core/RequestLoaders.ts +98 -5
  8. package/core/SchedulerManager.ts +28 -600
  9. package/core/app/cors.ts +2 -11
  10. package/core/app/preparedStatementWarmup.ts +9 -49
  11. package/core/app/requestRouter.ts +9 -8
  12. package/core/app/restRegistry.ts +8 -0
  13. package/core/archetype/fieldResolvers.ts +85 -40
  14. package/core/archetype/relationLoader.ts +135 -92
  15. package/core/cache/CacheManager.ts +91 -302
  16. package/core/cache/CompressionUtils.ts +34 -3
  17. package/core/cache/MemoryCache.ts +40 -37
  18. package/core/cache/RedisCache.ts +8 -7
  19. package/core/cache/health.ts +30 -0
  20. package/core/cache/invalidation.ts +96 -0
  21. package/core/cache/strategies/writeInvalidate.ts +111 -0
  22. package/core/cache/strategies/writeThrough.ts +233 -0
  23. package/core/components/BaseComponent.ts +25 -10
  24. package/core/components/ComponentRegistry.ts +28 -0
  25. package/core/decorators/IndexedField.ts +1 -1
  26. package/core/entity/cacheStrategies.ts +97 -0
  27. package/core/entity/componentAccess.ts +383 -0
  28. package/core/entity/finders.ts +202 -0
  29. package/core/entity/getCacheManager.ts +10 -0
  30. package/core/entity/pendingOps.ts +72 -0
  31. package/core/entity/saveEntity.ts +375 -0
  32. package/core/health.ts +93 -4
  33. package/core/hooks/dispatcher.ts +439 -0
  34. package/core/hooks/guards.ts +155 -0
  35. package/core/hooks/registry.ts +247 -0
  36. package/core/metadata/definitions/Component.ts +1 -1
  37. package/core/metadata/index.ts +15 -4
  38. package/core/middleware/RateLimit.ts +102 -105
  39. package/core/middleware/RequestId.ts +2 -9
  40. package/core/middleware/SecurityHeaders.ts +2 -11
  41. package/core/middleware/headers.ts +28 -0
  42. package/core/remote/OutboxWorker.ts +213 -183
  43. package/core/remote/RemoteManager.ts +401 -400
  44. package/core/remote/StreamConsumer.ts +535 -535
  45. package/core/remote/types.ts +153 -151
  46. package/core/requestScope.ts +34 -0
  47. package/core/scheduler/cronEvaluator.ts +174 -0
  48. package/core/scheduler/lifecycleHooks.ts +21 -0
  49. package/core/scheduler/lockCoordinator.ts +27 -0
  50. package/core/scheduler/metrics.ts +14 -0
  51. package/core/scheduler/taskRunner.ts +420 -0
  52. package/core/validateEnv.ts +10 -0
  53. package/database/DatabaseHelper.ts +128 -101
  54. package/database/IndexingStrategy.ts +72 -2
  55. package/database/PreparedStatementCache.ts +8 -2
  56. package/database/cancellable.ts +35 -22
  57. package/database/index.ts +29 -3
  58. package/database/instrumentedDb.ts +141 -141
  59. package/database/sqlHelpers.ts +3 -1
  60. package/endpoints/archetypes.ts +2 -8
  61. package/endpoints/tables.ts +6 -1
  62. package/gql/index.ts +1 -1
  63. package/gql/schema/index.ts +15 -4
  64. package/gql/visitors/ResolverGeneratorVisitor.ts +25 -4
  65. package/package.json +22 -1
  66. package/query/CTENode.ts +5 -3
  67. package/query/ComponentInclusionNode.ts +245 -14
  68. package/query/OrNode.ts +8 -19
  69. package/query/Query.ts +208 -79
  70. package/query/QueryContext.ts +6 -0
  71. package/query/QueryDAG.ts +7 -2
  72. package/query/membershipSource.ts +66 -0
  73. package/storage/LocalStorageProvider.ts +8 -3
  74. package/studio/dist/assets/index-BMZ67Npg.js +254 -0
  75. package/studio/dist/assets/index-BpbuYz9g.css +1 -0
  76. package/studio/{index.html → dist/index.html} +3 -2
  77. package/swagger/generator.ts +11 -1
  78. package/upload/UploadManager.ts +8 -6
  79. package/utils/uuid.ts +40 -10
  80. package/.claude/scheduled_tasks.lock +0 -1
  81. package/.claude/settings.local.json +0 -47
  82. package/.prettierrc +0 -4
  83. package/.serena/memories/architectural-decision-no-dependency-injection.md +0 -76
  84. package/.serena/memories/architecture.md +0 -154
  85. package/.serena/memories/cache-interface-refactoring-2026-01-24.md +0 -165
  86. package/.serena/memories/code_style_and_conventions.md +0 -76
  87. package/.serena/memories/project_overview.md +0 -43
  88. package/.serena/memories/schema-dsl-plan.md +0 -107
  89. package/.serena/memories/suggested_commands.md +0 -80
  90. package/.serena/memories/typescript-compilation-status.md +0 -54
  91. package/.serena/project.yml +0 -114
  92. package/BunSane.jpg +0 -0
  93. package/CLAUDE.md +0 -198
  94. package/TODO.md +0 -2
  95. package/bun.lock +0 -302
  96. package/bunfig.toml +0 -10
  97. package/docs/RFC_APP_REFACTOR.md +0 -248
  98. package/docs/RFC_REFACTOR_TARGETS.md +0 -251
  99. package/docs/SCALABILITY_PLAN.md +0 -175
  100. package/studio/bun.lock +0 -482
  101. package/studio/package.json +0 -39
  102. package/studio/postcss.config.js +0 -6
  103. package/studio/src/components/DataTable.tsx +0 -211
  104. package/studio/src/components/Layout.tsx +0 -13
  105. package/studio/src/components/PageContainer.tsx +0 -9
  106. package/studio/src/components/PageHeader.tsx +0 -13
  107. package/studio/src/components/SearchBar.tsx +0 -57
  108. package/studio/src/components/Sidebar.tsx +0 -294
  109. package/studio/src/components/ui/button.tsx +0 -56
  110. package/studio/src/components/ui/checkbox.tsx +0 -26
  111. package/studio/src/components/ui/input.tsx +0 -25
  112. package/studio/src/hooks/useDataTable.ts +0 -131
  113. package/studio/src/index.css +0 -36
  114. package/studio/src/lib/api.ts +0 -186
  115. package/studio/src/lib/utils.ts +0 -13
  116. package/studio/src/main.tsx +0 -17
  117. package/studio/src/pages/ArcheType.tsx +0 -239
  118. package/studio/src/pages/Components.tsx +0 -124
  119. package/studio/src/pages/EntityInspector.tsx +0 -302
  120. package/studio/src/pages/QueryRunner.tsx +0 -246
  121. package/studio/src/pages/Table.tsx +0 -94
  122. package/studio/src/pages/Welcome.tsx +0 -241
  123. package/studio/src/routes.tsx +0 -45
  124. package/studio/src/store/archeTypeSettings.ts +0 -30
  125. package/studio/src/store/studio.ts +0 -65
  126. package/studio/src/utils/columnHelpers.tsx +0 -114
  127. package/studio/studio-instructions.md +0 -81
  128. package/studio/tailwind.config.js +0 -77
  129. package/studio/utils.ts +0 -54
  130. package/studio/vite.config.js +0 -19
  131. package/tests/benchmark/BENCHMARK_DATABASES_PLAN.md +0 -338
  132. package/tests/benchmark/bunfig.toml +0 -9
  133. package/tests/benchmark/fixtures/EcommerceComponents.ts +0 -283
  134. package/tests/benchmark/fixtures/EcommerceDataGenerators.ts +0 -301
  135. package/tests/benchmark/fixtures/RelationTracker.ts +0 -159
  136. package/tests/benchmark/fixtures/index.ts +0 -6
  137. package/tests/benchmark/index.ts +0 -22
  138. package/tests/benchmark/noop-preload.ts +0 -3
  139. package/tests/benchmark/query-lateral-benchmark.test.ts +0 -372
  140. package/tests/benchmark/runners/BenchmarkLoader.ts +0 -132
  141. package/tests/benchmark/runners/index.ts +0 -4
  142. package/tests/benchmark/scenarios/query-benchmarks.test.ts +0 -465
  143. package/tests/benchmark/scripts/generate-db.ts +0 -344
  144. package/tests/benchmark/scripts/run-benchmarks.ts +0 -97
  145. package/tests/e2e/http.test.ts +0 -130
  146. package/tests/fixtures/archetypes/TestUserArchetype.ts +0 -21
  147. package/tests/fixtures/components/TestOrder.ts +0 -23
  148. package/tests/fixtures/components/TestProduct.ts +0 -23
  149. package/tests/fixtures/components/TestUser.ts +0 -20
  150. package/tests/fixtures/components/index.ts +0 -6
  151. package/tests/graphql/SchemaGeneration.test.ts +0 -90
  152. package/tests/graphql/builders/ResolverBuilder.test.ts +0 -223
  153. package/tests/graphql/builders/TypeDefBuilder.test.ts +0 -153
  154. package/tests/helpers/MockRedisClient.ts +0 -113
  155. package/tests/helpers/MockRedisStreamServer.ts +0 -448
  156. package/tests/integration/archetype/ArcheType.persistence.test.ts +0 -241
  157. package/tests/integration/cache/CacheInvalidation.test.ts +0 -259
  158. package/tests/integration/entity/Entity.persistence.test.ts +0 -333
  159. package/tests/integration/entity/Entity.saveTimeout.test.ts +0 -110
  160. package/tests/integration/loaders/RequestLoaders.abort.test.ts +0 -82
  161. package/tests/integration/query/Query.abort.test.ts +0 -66
  162. package/tests/integration/query/Query.complexAnalysis.test.ts +0 -557
  163. package/tests/integration/query/Query.edgeCases.test.ts +0 -595
  164. package/tests/integration/query/Query.exec.test.ts +0 -576
  165. package/tests/integration/query/Query.explainAnalyze.test.ts +0 -233
  166. package/tests/integration/query/Query.jsonbArray.test.ts +0 -214
  167. package/tests/integration/remote/dlq.test.ts +0 -175
  168. package/tests/integration/remote/event-dispatch.test.ts +0 -114
  169. package/tests/integration/remote/outbox.test.ts +0 -130
  170. package/tests/integration/remote/rpc.test.ts +0 -177
  171. package/tests/pglite-setup.ts +0 -62
  172. package/tests/setup.ts +0 -164
  173. package/tests/stress/BenchmarkRunner.ts +0 -203
  174. package/tests/stress/DataSeeder.ts +0 -190
  175. package/tests/stress/StressTestReporter.ts +0 -229
  176. package/tests/stress/cursor-perf-test.ts +0 -171
  177. package/tests/stress/fixtures/RealisticComponents.ts +0 -235
  178. package/tests/stress/fixtures/StressTestComponents.ts +0 -58
  179. package/tests/stress/index.ts +0 -7
  180. package/tests/stress/scenarios/query-benchmarks.test.ts +0 -285
  181. package/tests/stress/scenarios/realistic-scenarios.test.ts +0 -1081
  182. package/tests/stress/scenarios/timeout-investigation.test.ts +0 -522
  183. package/tests/unit/BatchLoader.test.ts +0 -196
  184. package/tests/unit/archetype/ArcheType.test.ts +0 -107
  185. package/tests/unit/cache/CacheManager.test.ts +0 -498
  186. package/tests/unit/cache/MemoryCache.test.ts +0 -260
  187. package/tests/unit/cache/RedisCache.test.ts +0 -411
  188. package/tests/unit/database/cancellable.test.ts +0 -81
  189. package/tests/unit/database/instrumentedDb.test.ts +0 -160
  190. package/tests/unit/entity/Entity.components.test.ts +0 -317
  191. package/tests/unit/entity/Entity.drainSideEffects.test.ts +0 -51
  192. package/tests/unit/entity/Entity.reload.test.ts +0 -63
  193. package/tests/unit/entity/Entity.requireComponents.test.ts +0 -72
  194. package/tests/unit/entity/Entity.test.ts +0 -345
  195. package/tests/unit/gql/depthLimit.test.ts +0 -203
  196. package/tests/unit/gql/operationMiddleware.test.ts +0 -293
  197. package/tests/unit/health/Health.test.ts +0 -129
  198. package/tests/unit/middleware/AccessLog.test.ts +0 -37
  199. package/tests/unit/middleware/Middleware.test.ts +0 -98
  200. package/tests/unit/middleware/RequestId.test.ts +0 -54
  201. package/tests/unit/middleware/SecurityHeaders.test.ts +0 -66
  202. package/tests/unit/query/FilterBuilder.test.ts +0 -111
  203. package/tests/unit/query/JsonbArrayBuilder.test.ts +0 -178
  204. package/tests/unit/query/Query.emptyString.test.ts +0 -69
  205. package/tests/unit/query/Query.test.ts +0 -310
  206. package/tests/unit/remote/CircuitBreaker.test.ts +0 -159
  207. package/tests/unit/remote/RemoteError.test.ts +0 -55
  208. package/tests/unit/remote/decorators.test.ts +0 -195
  209. package/tests/unit/remote/metrics.test.ts +0 -115
  210. package/tests/unit/remote/mockRedisStreamServer.test.ts +0 -104
  211. package/tests/unit/scheduler/DistributedLock.test.ts +0 -274
  212. package/tests/unit/scheduler/SchedulerManager.timeBased.test.ts +0 -95
  213. package/tests/unit/schema/schema-integration.test.ts +0 -426
  214. package/tests/unit/schema/schema.test.ts +0 -580
  215. package/tests/unit/storage/S3StorageProvider.test.ts +0 -567
  216. package/tests/unit/upload/RestUpload.test.ts +0 -267
  217. package/tests/unit/validateEnv.test.ts +0 -82
  218. package/tests/utils/entity-tracker.ts +0 -57
  219. package/tests/utils/index.ts +0 -13
  220. package/tests/utils/test-context.ts +0 -149
@@ -1,159 +0,0 @@
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
- }
@@ -1,6 +0,0 @@
1
- /**
2
- * Benchmark fixtures barrel export
3
- */
4
- export * from './EcommerceComponents';
5
- export * from './EcommerceDataGenerators';
6
- export * from './RelationTracker';
@@ -1,22 +0,0 @@
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';
@@ -1,3 +0,0 @@
1
- // Minimal preload for benchmarks - does nothing
2
- // This replaces tests/setup.ts to avoid env var conflicts
3
- export {};
@@ -1,372 +0,0 @@
1
- /**
2
- * Benchmark: LATERAL joins vs EXISTS subqueries for multi-component queries
3
- *
4
- * Tests the performance impact of the INTERSECT query fix that disables
5
- * LATERAL joins for multi-component non-CTE queries.
6
- */
7
- import { describe, test, expect, beforeAll, afterAll } from 'bun:test';
8
- import { Entity } from '../../core/Entity';
9
- import { Query, FilterOp } from '../../query/Query';
10
- import { Component, CompData, BaseComponent } from '../../core/components';
11
- import { ComponentRegistry } from '../../core/components/ComponentRegistry';
12
- import { ensureComponentsRegistered } from '../utils';
13
-
14
- // Benchmark components
15
- @Component
16
- class BenchUser extends BaseComponent {
17
- @CompData({ indexed: true }) name: string = '';
18
- @CompData({ indexed: true }) email: string = '';
19
- @CompData() age: number = 0;
20
- @CompData() status: string = 'active';
21
- }
22
-
23
- @Component
24
- class BenchProfile extends BaseComponent {
25
- @CompData({ indexed: true }) username: string = '';
26
- @CompData() bio: string = '';
27
- @CompData() verified: boolean = false;
28
- }
29
-
30
- @Component
31
- class BenchSettings extends BaseComponent {
32
- @CompData() theme: string = 'light';
33
- @CompData() notifications: boolean = true;
34
- @CompData() language: string = 'en';
35
- }
36
-
37
- // Test configuration
38
- const DATASET_SIZES = {
39
- small: 100,
40
- medium: 1000,
41
- large: 5000
42
- };
43
-
44
- const ITERATIONS = 5; // Number of times to run each query for averaging
45
-
46
- interface BenchmarkResult {
47
- name: string;
48
- datasetSize: number;
49
- avgTimeMs: number;
50
- minTimeMs: number;
51
- maxTimeMs: number;
52
- resultCount: number;
53
- }
54
-
55
- async function runBenchmark(
56
- name: string,
57
- datasetSize: number,
58
- queryFn: () => Promise<Entity[]>
59
- ): Promise<BenchmarkResult> {
60
- const times: number[] = [];
61
- let resultCount = 0;
62
-
63
- for (let i = 0; i < ITERATIONS; i++) {
64
- const start = performance.now();
65
- const results = await queryFn();
66
- const end = performance.now();
67
- times.push(end - start);
68
- resultCount = results.length;
69
- }
70
-
71
- return {
72
- name,
73
- datasetSize,
74
- avgTimeMs: times.reduce((a, b) => a + b, 0) / times.length,
75
- minTimeMs: Math.min(...times),
76
- maxTimeMs: Math.max(...times),
77
- resultCount
78
- };
79
- }
80
-
81
- describe('Query Performance Benchmark', () => {
82
- const createdEntityIds: string[] = [];
83
- const datasetSize = DATASET_SIZES.large; // 5000 entities
84
-
85
- beforeAll(async () => {
86
- await ensureComponentsRegistered(BenchUser, BenchProfile, BenchSettings);
87
-
88
- console.log(`\nšŸ“Š Creating ${datasetSize} test entities...`);
89
- const startCreate = performance.now();
90
-
91
- // Create entities with various component combinations
92
- for (let i = 0; i < datasetSize; i++) {
93
- const entity = Entity.Create();
94
-
95
- // All entities have BenchUser
96
- entity.add(BenchUser, {
97
- name: `User ${i}`,
98
- email: `user${i}@test.com`,
99
- age: 18 + (i % 60),
100
- status: i % 3 === 0 ? 'active' : (i % 3 === 1 ? 'inactive' : 'pending')
101
- });
102
-
103
- // 80% have BenchProfile
104
- if (i % 5 !== 0) {
105
- entity.add(BenchProfile, {
106
- username: `user_${i}`,
107
- bio: `Bio for user ${i}`,
108
- verified: i % 2 === 0
109
- });
110
- }
111
-
112
- // 60% have BenchSettings
113
- if (i % 5 < 3) {
114
- entity.add(BenchSettings, {
115
- theme: i % 2 === 0 ? 'light' : 'dark',
116
- notifications: i % 3 !== 0,
117
- language: ['en', 'es', 'fr', 'de'][i % 4]!
118
- });
119
- }
120
-
121
- await entity.save();
122
- createdEntityIds.push(entity.id);
123
- }
124
-
125
- const endCreate = performance.now();
126
- console.log(`āœ… Created ${datasetSize} entities in ${(endCreate - startCreate).toFixed(0)}ms\n`);
127
- }, 120000); // 2 minute timeout for setup
128
-
129
- afterAll(async () => {
130
- // Cleanup
131
- console.log(`\n🧹 Cleaning up ${createdEntityIds.length} entities...`);
132
- for (const id of createdEntityIds) {
133
- try {
134
- const entity = await Entity.findById(id);
135
- if (entity) {
136
- await entity.delete();
137
- }
138
- } catch {
139
- // Ignore cleanup errors
140
- }
141
- }
142
- }, 120000);
143
-
144
- describe('Single component queries (baseline)', () => {
145
- test('single component, no filter', async () => {
146
- const result = await runBenchmark(
147
- 'Single component, no filter',
148
- datasetSize,
149
- () => new Query().with(BenchUser).take(100).exec()
150
- );
151
- console.log(` ${result.name}: ${result.avgTimeMs.toFixed(2)}ms avg (${result.resultCount} results)`);
152
- expect(result.avgTimeMs).toBeLessThan(1000);
153
- });
154
-
155
- test('single component, with filter', async () => {
156
- const result = await runBenchmark(
157
- 'Single component, with filter',
158
- datasetSize,
159
- () => new Query()
160
- .with(BenchUser, {
161
- filters: [Query.filter('status', FilterOp.EQ, 'active')]
162
- })
163
- .take(100)
164
- .exec()
165
- );
166
- console.log(` ${result.name}: ${result.avgTimeMs.toFixed(2)}ms avg (${result.resultCount} results)`);
167
- expect(result.avgTimeMs).toBeLessThan(1000);
168
- });
169
- });
170
-
171
- describe('Two component queries (affected by fix)', () => {
172
- test('2 components, no filter', async () => {
173
- const result = await runBenchmark(
174
- '2 components, no filter',
175
- datasetSize,
176
- () => new Query()
177
- .with(BenchUser)
178
- .with(BenchProfile)
179
- .take(100)
180
- .exec()
181
- );
182
- console.log(` ${result.name}: ${result.avgTimeMs.toFixed(2)}ms avg (${result.resultCount} results)`);
183
- expect(result.avgTimeMs).toBeLessThan(2000);
184
- });
185
-
186
- test('2 components, filter on first (bug pattern)', async () => {
187
- const result = await runBenchmark(
188
- '2 components, filter on first',
189
- datasetSize,
190
- () => new Query()
191
- .with(BenchUser, {
192
- filters: [Query.filter('status', FilterOp.EQ, 'active')]
193
- })
194
- .with(BenchProfile)
195
- .take(100)
196
- .exec()
197
- );
198
- console.log(` ${result.name}: ${result.avgTimeMs.toFixed(2)}ms avg (${result.resultCount} results)`);
199
- expect(result.avgTimeMs).toBeLessThan(2000);
200
- });
201
-
202
- test('2 components, filter on second (bug pattern)', async () => {
203
- const result = await runBenchmark(
204
- '2 components, filter on second',
205
- datasetSize,
206
- () => new Query()
207
- .with(BenchUser)
208
- .with(BenchProfile, {
209
- filters: [Query.filter('verified', FilterOp.EQ, true)]
210
- })
211
- .take(100)
212
- .exec()
213
- );
214
- console.log(` ${result.name}: ${result.avgTimeMs.toFixed(2)}ms avg (${result.resultCount} results)`);
215
- expect(result.avgTimeMs).toBeLessThan(2000);
216
- });
217
-
218
- test('2 components, filters on both', async () => {
219
- const result = await runBenchmark(
220
- '2 components, filters on both',
221
- datasetSize,
222
- () => new Query()
223
- .with(BenchUser, {
224
- filters: [Query.filter('status', FilterOp.EQ, 'active')]
225
- })
226
- .with(BenchProfile, {
227
- filters: [Query.filter('verified', FilterOp.EQ, true)]
228
- })
229
- .take(100)
230
- .exec()
231
- );
232
- console.log(` ${result.name}: ${result.avgTimeMs.toFixed(2)}ms avg (${result.resultCount} results)`);
233
- expect(result.avgTimeMs).toBeLessThan(2000);
234
- });
235
-
236
- test('2 components, IN filter (bug pattern)', async () => {
237
- const result = await runBenchmark(
238
- '2 components, IN filter',
239
- datasetSize,
240
- () => new Query()
241
- .with(BenchUser, {
242
- filters: [Query.filter('status', FilterOp.IN, ['active', 'pending'])]
243
- })
244
- .with(BenchProfile)
245
- .take(100)
246
- .exec()
247
- );
248
- console.log(` ${result.name}: ${result.avgTimeMs.toFixed(2)}ms avg (${result.resultCount} results)`);
249
- expect(result.avgTimeMs).toBeLessThan(2000);
250
- });
251
- });
252
-
253
- describe('Three component queries', () => {
254
- test('3 components, no filter', async () => {
255
- const result = await runBenchmark(
256
- '3 components, no filter',
257
- datasetSize,
258
- () => new Query()
259
- .with(BenchUser)
260
- .with(BenchProfile)
261
- .with(BenchSettings)
262
- .take(100)
263
- .exec()
264
- );
265
- console.log(` ${result.name}: ${result.avgTimeMs.toFixed(2)}ms avg (${result.resultCount} results)`);
266
- expect(result.avgTimeMs).toBeLessThan(3000);
267
- });
268
-
269
- test('3 components, filter on one', async () => {
270
- const result = await runBenchmark(
271
- '3 components, filter on one',
272
- datasetSize,
273
- () => new Query()
274
- .with(BenchUser, {
275
- filters: [Query.filter('age', FilterOp.GTE, 30)]
276
- })
277
- .with(BenchProfile)
278
- .with(BenchSettings)
279
- .take(100)
280
- .exec()
281
- );
282
- console.log(` ${result.name}: ${result.avgTimeMs.toFixed(2)}ms avg (${result.resultCount} results)`);
283
- expect(result.avgTimeMs).toBeLessThan(3000);
284
- });
285
-
286
- test('3 components, filters on all', async () => {
287
- const result = await runBenchmark(
288
- '3 components, filters on all',
289
- datasetSize,
290
- () => new Query()
291
- .with(BenchUser, {
292
- filters: [Query.filter('status', FilterOp.EQ, 'active')]
293
- })
294
- .with(BenchProfile, {
295
- filters: [Query.filter('verified', FilterOp.EQ, true)]
296
- })
297
- .with(BenchSettings, {
298
- filters: [Query.filter('theme', FilterOp.EQ, 'dark')]
299
- })
300
- .take(100)
301
- .exec()
302
- );
303
- console.log(` ${result.name}: ${result.avgTimeMs.toFixed(2)}ms avg (${result.resultCount} results)`);
304
- expect(result.avgTimeMs).toBeLessThan(3000);
305
- });
306
- });
307
-
308
- describe('Sorting with multi-component (affected by fix)', () => {
309
- test('2 components, sort on first', async () => {
310
- const result = await runBenchmark(
311
- '2 components, sort on first',
312
- datasetSize,
313
- () => new Query()
314
- .with(BenchUser)
315
- .with(BenchProfile)
316
- .sortBy(BenchUser, 'age', 'DESC')
317
- .take(100)
318
- .exec()
319
- );
320
- console.log(` ${result.name}: ${result.avgTimeMs.toFixed(2)}ms avg (${result.resultCount} results)`);
321
- expect(result.avgTimeMs).toBeLessThan(3000);
322
- });
323
-
324
- test('2 components, filter + sort on different components', async () => {
325
- const result = await runBenchmark(
326
- '2 components, filter + sort different',
327
- datasetSize,
328
- () => new Query()
329
- .with(BenchUser)
330
- .with(BenchProfile, {
331
- filters: [Query.filter('verified', FilterOp.EQ, true)]
332
- })
333
- .sortBy(BenchUser, 'age', 'ASC')
334
- .take(100)
335
- .exec()
336
- );
337
- console.log(` ${result.name}: ${result.avgTimeMs.toFixed(2)}ms avg (${result.resultCount} results)`);
338
- expect(result.avgTimeMs).toBeLessThan(3000);
339
- });
340
- });
341
-
342
- describe('Count operations', () => {
343
- test('2 components count with filter', async () => {
344
- const times: number[] = [];
345
- let count = 0;
346
-
347
- for (let i = 0; i < ITERATIONS; i++) {
348
- const start = performance.now();
349
- count = await new Query()
350
- .with(BenchUser, {
351
- filters: [Query.filter('status', FilterOp.EQ, 'active')]
352
- })
353
- .with(BenchProfile)
354
- .count();
355
- const end = performance.now();
356
- times.push(end - start);
357
- }
358
-
359
- const avgTime = times.reduce((a, b) => a + b, 0) / times.length;
360
- console.log(` 2 components count with filter: ${avgTime.toFixed(2)}ms avg (count: ${count})`);
361
- expect(avgTime).toBeLessThan(2000);
362
- });
363
- });
364
-
365
- test('Performance summary', () => {
366
- console.log('\nšŸ“ˆ Benchmark Summary:');
367
- console.log(` Dataset size: ${datasetSize} entities`);
368
- console.log(` Iterations per query: ${ITERATIONS}`);
369
- console.log(' All queries completed within acceptable thresholds');
370
- expect(true).toBe(true);
371
- });
372
- });
@@ -1,132 +0,0 @@
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
- }
@@ -1,4 +0,0 @@
1
- /**
2
- * Benchmark runners barrel export
3
- */
4
- export * from './BenchmarkLoader';