|
| 1 | +import { createEncryptionOperators } from '@cipherstash/stack/drizzle' |
| 2 | +import type { SQL } from 'drizzle-orm' |
| 3 | +import { afterAll, beforeAll, bench, describe } from 'vitest' |
| 4 | +import { |
| 5 | + type BenchHandle, |
| 6 | + benchTable, |
| 7 | + buildBench, |
| 8 | + teardownBench, |
| 9 | +} from '../../src/drizzle/setup.js' |
| 10 | +import { applySchema } from '../../src/harness/db.js' |
| 11 | +import { seed } from '../../src/harness/seed.js' |
| 12 | + |
| 13 | +let handle: BenchHandle |
| 14 | +let ops: ReturnType<typeof createEncryptionOperators> |
| 15 | + |
| 16 | +beforeAll(async () => { |
| 17 | + handle = await buildBench() |
| 18 | + await applySchema(handle.pgClient) |
| 19 | + await seed(handle) |
| 20 | + ops = createEncryptionOperators(handle.encryptionClient) |
| 21 | +}) |
| 22 | + |
| 23 | +afterAll(async () => { |
| 24 | + if (handle) await teardownBench(handle) |
| 25 | +}) |
| 26 | + |
| 27 | +/** |
| 28 | + * Encryption cost is paid inside each iteration too — folding it into the |
| 29 | + * timed loop reflects what customer code actually does, and the index |
| 30 | + * engagement signal still dominates the differential between operators. |
| 31 | + */ |
| 32 | +describe('drizzle', () => { |
| 33 | + bench('eq (string match)', async () => { |
| 34 | + const where = (await ops.eq(benchTable.encText, 'value-0000042')) as SQL |
| 35 | + await handle.db.select().from(benchTable).where(where) |
| 36 | + }) |
| 37 | + |
| 38 | + bench('inArray (3 string matches)', async () => { |
| 39 | + const where = await ops.inArray(benchTable.encText, [ |
| 40 | + 'value-0000042', |
| 41 | + 'value-0000123', |
| 42 | + 'value-0000999', |
| 43 | + ]) |
| 44 | + await handle.db.select().from(benchTable).where(where) |
| 45 | + }) |
| 46 | + |
| 47 | + bench('like (prefix)', async () => { |
| 48 | + const where = (await ops.like(benchTable.encText, '%value-00000%')) as SQL |
| 49 | + await handle.db.select().from(benchTable).where(where) |
| 50 | + }) |
| 51 | + |
| 52 | + bench('gt (int)', async () => { |
| 53 | + const where = (await ops.gt(benchTable.encInt, 9990)) as SQL |
| 54 | + await handle.db.select().from(benchTable).where(where) |
| 55 | + }) |
| 56 | + |
| 57 | + bench('between (int)', async () => { |
| 58 | + const where = (await ops.between(benchTable.encInt, 4000, 4100)) as SQL |
| 59 | + await handle.db.select().from(benchTable).where(where) |
| 60 | + }) |
| 61 | + |
| 62 | + bench('orderBy desc + limit 10', async () => { |
| 63 | + await handle.db |
| 64 | + .select() |
| 65 | + .from(benchTable) |
| 66 | + .orderBy(ops.desc(benchTable.encInt)) |
| 67 | + .limit(10) |
| 68 | + }) |
| 69 | +}) |
0 commit comments