|
| 1 | +import { bench, describe } from "vitest"; |
| 2 | +import { |
| 3 | + createSqlLanguageService, |
| 4 | + duckdbDialect, |
| 5 | +} from "../index.js"; |
| 6 | +import type { |
| 7 | + SqlCatalogRelation, |
| 8 | +} from "../relation-completion-types.js"; |
| 9 | +import type { |
| 10 | + SqlDocumentContext, |
| 11 | + SqlDocumentSession, |
| 12 | + SqlLanguageService, |
| 13 | +} from "../types.js"; |
| 14 | + |
| 15 | +interface BenchmarkContext extends SqlDocumentContext { |
| 16 | + readonly engine: "benchmark"; |
| 17 | +} |
| 18 | + |
| 19 | +const dialect = duckdbDialect(); |
| 20 | +const tenKibibytes = 10 * 1_024; |
| 21 | +const prefix = "WITH local_cte AS (SELECT 1) SELECT "; |
| 22 | +const suffix = " FROM local"; |
| 23 | +const projectionLength = |
| 24 | + tenKibibytes - prefix.length - suffix.length; |
| 25 | +const projection = `${"x,".repeat( |
| 26 | + Math.floor((projectionLength - 1) / 2), |
| 27 | +)}x`; |
| 28 | +const tenKibibyteText = `${prefix}${projection}${" ".repeat( |
| 29 | + projectionLength - projection.length, |
| 30 | +)}${suffix}`; |
| 31 | +if (tenKibibyteText.length !== tenKibibytes) { |
| 32 | + throw new Error("Session completion benchmark must be exactly 10 KiB"); |
| 33 | +} |
| 34 | + |
| 35 | +function openLocalSession( |
| 36 | + service: SqlLanguageService<BenchmarkContext>, |
| 37 | +): SqlDocumentSession<BenchmarkContext> { |
| 38 | + return service.openDocument({ |
| 39 | + context: { dialect: "duckdb", engine: "benchmark" }, |
| 40 | + text: tenKibibyteText, |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +const localService = |
| 45 | + createSqlLanguageService<BenchmarkContext>({ |
| 46 | + dialects: [dialect], |
| 47 | + }); |
| 48 | +const warmLocalSession = openLocalSession(localService); |
| 49 | +const warmPreflight = await warmLocalSession.complete({ |
| 50 | + position: tenKibibyteText.length, |
| 51 | + trigger: { kind: "invoked" }, |
| 52 | +}); |
| 53 | +if ( |
| 54 | + warmPreflight.status !== "ready" || |
| 55 | + warmPreflight.value.items.length !== 1 |
| 56 | +) { |
| 57 | + throw new Error("Warm session benchmark preflight failed"); |
| 58 | +} |
| 59 | + |
| 60 | +const catalogRelations = Array.from( |
| 61 | + { length: 100 }, |
| 62 | + (_, index): SqlCatalogRelation => ({ |
| 63 | + canonicalPath: [ |
| 64 | + { |
| 65 | + quoted: false, |
| 66 | + role: "relation" as const, |
| 67 | + value: `relation_${index}`, |
| 68 | + }, |
| 69 | + ], |
| 70 | + completionPathStart: 0, |
| 71 | + entityId: `relation-${index}`, |
| 72 | + matchQuality: "exact" as const, |
| 73 | + relationKind: "table" as const, |
| 74 | + }), |
| 75 | +); |
| 76 | + |
| 77 | +function createCatalogService(): SqlLanguageService<BenchmarkContext> { |
| 78 | + return createSqlLanguageService<BenchmarkContext>({ |
| 79 | + catalog: { |
| 80 | + id: "benchmark-catalog", |
| 81 | + search: async () => ({ |
| 82 | + coverage: { kind: "complete" }, |
| 83 | + epoch: { generation: 0, token: "initial" }, |
| 84 | + relations: catalogRelations, |
| 85 | + status: "ready", |
| 86 | + }), |
| 87 | + }, |
| 88 | + dialects: [dialect], |
| 89 | + }); |
| 90 | +} |
| 91 | + |
| 92 | +const cachedCatalogService = createCatalogService(); |
| 93 | +const cachedCatalogSession = cachedCatalogService.openDocument({ |
| 94 | + context: { |
| 95 | + catalog: { scope: "benchmark:cached" }, |
| 96 | + dialect: "duckdb", |
| 97 | + engine: "benchmark", |
| 98 | + }, |
| 99 | + text: "SELECT * FROM relation_", |
| 100 | +}); |
| 101 | +const cachedPreflight = await cachedCatalogSession.complete({ |
| 102 | + position: 23, |
| 103 | + trigger: { kind: "invoked" }, |
| 104 | +}); |
| 105 | +if ( |
| 106 | + cachedPreflight.status !== "ready" || |
| 107 | + cachedPreflight.value.items.length !== 100 |
| 108 | +) { |
| 109 | + throw new Error("Cached catalog benchmark preflight failed"); |
| 110 | +} |
| 111 | + |
| 112 | +describe("session completion", () => { |
| 113 | + bench("cold 10 KiB local completion", async () => { |
| 114 | + const session = openLocalSession(localService); |
| 115 | + await session.complete({ |
| 116 | + position: tenKibibyteText.length, |
| 117 | + trigger: { kind: "invoked" }, |
| 118 | + }); |
| 119 | + session.dispose(); |
| 120 | + }); |
| 121 | + |
| 122 | + bench("warm 10 KiB local completion", async () => { |
| 123 | + await warmLocalSession.complete({ |
| 124 | + position: tenKibibyteText.length, |
| 125 | + trigger: { kind: "invoked" }, |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + bench("cached 100-candidate catalog completion", async () => { |
| 130 | + await cachedCatalogSession.complete({ |
| 131 | + position: 23, |
| 132 | + trigger: { kind: "invoked" }, |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + bench("cold 100-candidate catalog completion", async () => { |
| 137 | + const service = createCatalogService(); |
| 138 | + const session = service.openDocument({ |
| 139 | + context: { |
| 140 | + catalog: { scope: "benchmark:cold" }, |
| 141 | + dialect: "duckdb", |
| 142 | + engine: "benchmark", |
| 143 | + }, |
| 144 | + text: "SELECT * FROM relation_", |
| 145 | + }); |
| 146 | + await session.complete({ |
| 147 | + position: 23, |
| 148 | + trigger: { kind: "invoked" }, |
| 149 | + }); |
| 150 | + service.dispose(); |
| 151 | + }); |
| 152 | +}); |
0 commit comments