|
| 1 | +import { bench } from "vitest"; |
| 2 | +import { openLix } from "../lix/open-lix.js"; |
| 3 | +import { createVersion } from "../version/create-version.js"; |
| 4 | + |
| 5 | +// Keep sizes moderate to avoid long CI runs while still being meaningful |
| 6 | +const ROWS_SIMPLE = 100; |
| 7 | +const ENTITIES_LINEAR = 50; |
| 8 | +const COMMITS_LINEAR = 5; |
| 9 | +const INHERIT_BASE = 50; |
| 10 | + |
| 11 | +bench("materializer: select all from single version", async () => { |
| 12 | + const lix = await openLix({}); |
| 13 | + |
| 14 | + // Seed a single commit containing many entities in the global version |
| 15 | + await lix.db |
| 16 | + .insertInto("state_all") |
| 17 | + .values( |
| 18 | + Array.from({ length: ROWS_SIMPLE }, (_, i) => ({ |
| 19 | + entity_id: `bench_entity_${i}`, |
| 20 | + version_id: "global", |
| 21 | + snapshot_content: { |
| 22 | + id: `bench_entity_${i}`, |
| 23 | + v: 1, |
| 24 | + }, |
| 25 | + schema_key: "bench_entity", |
| 26 | + file_id: "bench_file", |
| 27 | + plugin_key: "bench_plugin", |
| 28 | + schema_version: "1.0", |
| 29 | + })) |
| 30 | + ) |
| 31 | + .execute(); |
| 32 | + |
| 33 | + // Read from the final materializer view |
| 34 | + await lix.db |
| 35 | + .selectFrom("internal_state_materializer" as any) |
| 36 | + .where("version_id", "=", "global") |
| 37 | + .selectAll() |
| 38 | + .execute(); |
| 39 | +}); |
| 40 | + |
| 41 | +bench("materializer: linear history (entities x commits)", async () => { |
| 42 | + const lix = await openLix({}); |
| 43 | + |
| 44 | + // Create an isolated version for this benchmark |
| 45 | + await createVersion({ lix, id: "bench_linear" }); |
| 46 | + |
| 47 | + // Generate a linear chain of commits. Each commit updates all entities. |
| 48 | + for (let c = 0; c < COMMITS_LINEAR; c++) { |
| 49 | + await lix.db |
| 50 | + .insertInto("state_all") |
| 51 | + .values( |
| 52 | + Array.from({ length: ENTITIES_LINEAR }, (_, i) => ({ |
| 53 | + entity_id: `lin_entity_${i}`, |
| 54 | + version_id: "bench_linear", |
| 55 | + snapshot_content: { id: `lin_entity_${i}`, rev: c }, |
| 56 | + schema_key: "bench_entity", |
| 57 | + file_id: "bench_file", |
| 58 | + plugin_key: "bench_plugin", |
| 59 | + schema_version: "1.0", |
| 60 | + })) |
| 61 | + ) |
| 62 | + .execute(); |
| 63 | + } |
| 64 | + |
| 65 | + // Query latest visible state resolved through the full pipeline |
| 66 | + await lix.db |
| 67 | + .selectFrom("internal_state_materializer" as any) |
| 68 | + .where("version_id", "=", "bench_linear") |
| 69 | + .selectAll() |
| 70 | + .execute(); |
| 71 | +}); |
| 72 | + |
| 73 | +bench("materializer: inheritance (global -> A -> B -> C)", async () => { |
| 74 | + const lix = await openLix({}); |
| 75 | + |
| 76 | + // Create a 3-level inheritance chain from global |
| 77 | + const versionA = await createVersion({ |
| 78 | + lix, |
| 79 | + id: "bench_A", |
| 80 | + inheritsFrom: { id: "global" }, |
| 81 | + }); |
| 82 | + const versionB = await createVersion({ |
| 83 | + lix, |
| 84 | + id: "bench_B", |
| 85 | + inheritsFrom: versionA, |
| 86 | + }); |
| 87 | + const versionC = await createVersion({ |
| 88 | + lix, |
| 89 | + id: "bench_C", |
| 90 | + inheritsFrom: versionB, |
| 91 | + }); |
| 92 | + |
| 93 | + // Base data on global |
| 94 | + await lix.db |
| 95 | + .insertInto("state_all") |
| 96 | + .values( |
| 97 | + Array.from({ length: INHERIT_BASE }, (_, i) => ({ |
| 98 | + entity_id: `inh_entity_${i}`, |
| 99 | + version_id: "global", |
| 100 | + snapshot_content: { id: `inh_entity_${i}`, src: "global" }, |
| 101 | + schema_key: "bench_entity", |
| 102 | + file_id: "bench_file", |
| 103 | + plugin_key: "bench_plugin", |
| 104 | + schema_version: "1.0", |
| 105 | + })) |
| 106 | + ) |
| 107 | + .execute(); |
| 108 | + |
| 109 | + // A overrides first 100 |
| 110 | + await lix.db |
| 111 | + .insertInto("state_all") |
| 112 | + .values( |
| 113 | + Array.from({ length: 100 }, (_, i) => ({ |
| 114 | + entity_id: `inh_entity_${i}`, |
| 115 | + version_id: versionA.id, |
| 116 | + snapshot_content: { id: `inh_entity_${i}`, src: "A" }, |
| 117 | + schema_key: "bench_entity", |
| 118 | + file_id: "bench_file", |
| 119 | + plugin_key: "bench_plugin", |
| 120 | + schema_version: "1.0", |
| 121 | + })) |
| 122 | + ) |
| 123 | + .execute(); |
| 124 | + |
| 125 | + // C adds 100 new and overrides next 50 from base |
| 126 | + await lix.db |
| 127 | + .insertInto("state_all") |
| 128 | + .values([ |
| 129 | + // overrides 100..149 |
| 130 | + ...Array.from({ length: 50 }, (_, i) => ({ |
| 131 | + entity_id: `inh_entity_${100 + i}`, |
| 132 | + version_id: versionC.id, |
| 133 | + snapshot_content: { id: `inh_entity_${100 + i}`, src: "C" }, |
| 134 | + schema_key: "bench_entity", |
| 135 | + file_id: "bench_file", |
| 136 | + plugin_key: "bench_plugin", |
| 137 | + schema_version: "1.0", |
| 138 | + })), |
| 139 | + // adds 100 new entities 300..399 |
| 140 | + ...Array.from({ length: 100 }, (_, i) => ({ |
| 141 | + entity_id: `inh_entity_${INHERIT_BASE + i}`, |
| 142 | + version_id: versionC.id, |
| 143 | + snapshot_content: { |
| 144 | + id: `inh_entity_${INHERIT_BASE + i}`, |
| 145 | + src: "C_new", |
| 146 | + }, |
| 147 | + schema_key: "bench_entity", |
| 148 | + file_id: "bench_file", |
| 149 | + plugin_key: "bench_plugin", |
| 150 | + schema_version: "1.0", |
| 151 | + })), |
| 152 | + ]) |
| 153 | + .execute(); |
| 154 | + |
| 155 | + // Query materialized state for C (should include inherited + overrides) |
| 156 | + await lix.db |
| 157 | + .selectFrom("internal_state_materializer" as any) |
| 158 | + .where("version_id", "=", versionC.id) |
| 159 | + .selectAll() |
| 160 | + .execute(); |
| 161 | +}); |
| 162 | + |
| 163 | +bench("materializer: point lookup by entity_id", async () => { |
| 164 | + const lix = await openLix({}); |
| 165 | + |
| 166 | + // Seed a reasonable set of entities in a dedicated version |
| 167 | + await createVersion({ lix, id: "bench_point" }); |
| 168 | + |
| 169 | + await lix.db |
| 170 | + .insertInto("state_all") |
| 171 | + .values( |
| 172 | + Array.from({ length: ROWS_SIMPLE }, (_, i) => ({ |
| 173 | + entity_id: `point_entity_${i}`, |
| 174 | + version_id: "bench_point", |
| 175 | + snapshot_content: { id: `point_entity_${i}` }, |
| 176 | + schema_key: "bench_entity", |
| 177 | + file_id: "bench_file", |
| 178 | + plugin_key: "bench_plugin", |
| 179 | + schema_version: "1.0", |
| 180 | + })) |
| 181 | + ) |
| 182 | + .execute(); |
| 183 | + |
| 184 | + // Point query by entity_id |
| 185 | + await lix.db |
| 186 | + .selectFrom("internal_state_materializer" as any) |
| 187 | + .where("version_id", "=", "bench_point") |
| 188 | + .where("entity_id", "=", "point_entity_123") |
| 189 | + .selectAll() |
| 190 | + .execute(); |
| 191 | +}); |
0 commit comments