Open History to see the last commit
1import assert from 'node:assert/strict'
2import { randomUUID, createHash } from 'node:crypto'
3import { readFile, writeFile } from 'node:fs/promises'
4import { expectedReplay } from '../test/fixtures/replay-data.mjs'
5
6const env = process.env
7const state = JSON.parse(await readFile(env.NEON_TEST_STATE_FILE ?? '.neon-test-state-replay.json', 'utf8'))
8if (state.branchId !== env.NEON_TEST_BRANCH_ID || state.branchId !== env.TEST_FUNCTION_BRANCH_ID || !state.name.startsWith('ds-test-')) throw new Error('Recorded replay environment required')
9const request = (path, options = {}) => fetch(new URL(path, env.TEST_FUNCTION_URL), { ...options,
10 headers: { Authorization: `Bearer ${env.TEST_FUNCTION_API_KEY}`, ...options.headers }, signal: AbortSignal.timeout(180000) })
11const health = await (await request('_benchmark/health')).json()
12assert.equal(health.build, env.TEST_FUNCTION_BUNDLE_SHA)
13assert.equal(health.settings.firstBlockProbe, true)
14assert.equal(health.settings.blockBytes, 65536)
15const run = `replay-${randomUUID()}`
16const seeded = await request('_benchmark/seed', { method: 'POST', body: JSON.stringify({ run, profile: 'binary', frames: 256 }) })
17assert.equal(seeded.status, 200)
18const dataset = await seeded.json()
19for (let pass = 0; pass < 3; pass++) {
20 const sealed = await request('_benchmark/maintain', { method: 'POST' })
21 assert.equal(sealed.status, 200); await sealed.arrayBuffer()
22}
23const samples = []
24async function page(from, narrow, mode) {
25 const started = performance.now()
26 const response = await request(`v1/stream/${dataset.path}?offset=${encodeURIComponent(dataset.offsets[from])}`, {
27 headers: { 'x-replay-cache': 'cold', ...(narrow ? { 'x-replay-first-block': '1' } : {}) },
28 })
29 assert.equal(response.status, 200)
30 const headerMs = performance.now() - started
31 const reader = response.body.getReader(), parts = []
32 let firstByteMs
33 for (;;) {
34 const part = await reader.read()
35 if (part.done) break
36 firstByteMs ??= performance.now() - started
37 parts.push(part.value)
38 }
39 const next = dataset.offsets.indexOf(response.headers.get('Stream-Next-Offset'))
40 const bytes = Buffer.concat(parts)
41 assert.ok(next > from)
42 assert.deepEqual(bytes, expectedReplay('binary', from, next))
43 if (narrow) assert.ok(bytes.length <= 65536)
44 samples.push({ mode, from, next, pageBytes: narrow ? 65536 : 2097152, headerMs, firstByteMs,
45 completeMs: performance.now() - started, deliveredBytes: bytes.length, metrics: JSON.parse(response.headers.get('x-replay-metrics')) })
46 return next
47}
48for (let repetition = 0; repetition < 3; repetition++) {
49 for (const from of [0, 32, 126]) { await page(from, false, 'normal-seek'); await page(from, true, 'first-block-seek') }
50}
51const scans = []
52for (const narrow of [false, true]) {
53 let from = 0, pages = 0
54 const started = performance.now(), sampleStart = samples.length
55 while (from < dataset.frames) {
56 from = await page(from, narrow, narrow ? 'first-block-scan' : 'normal-scan')
57 if (++pages % 16 === 0) console.log(`Verified ${pages} ${narrow ? 'first-block' : 'normal'} scan pages`)
58 }
59 scans.push({ pageBytes: narrow ? 65536 : 2097152, pages, durationMs: performance.now() - started,
60 sqlCalls: samples.slice(sampleStart).reduce((sum, sample) => sum + sample.metrics.sqlCalls, 0),
61 objectGets: samples.slice(sampleStart).reduce((sum, sample) => sum + sample.metrics.objectGets, 0) })
62}
63await writeFile(env.REPLAY_REPORT ?? 'qualification-results/replay-first-block.json', JSON.stringify({
64 sourceCommit: env.TEST_FUNCTION_SOURCE_COMMIT, bundleSha256: env.TEST_FUNCTION_BUNDLE_SHA, branchId: state.branchId,
65 harnessSha256: createHash('sha256').update(await readFile(new URL(import.meta.url))).digest('hex'),
66 settings: health.settings, scans, samples,
67 scope: 'Same deployed Function, deterministic 8 MiB binary stream of 32 KiB frames, cold cache per request. Compare a 2 MiB page with a 64 KiB page ending at a verified block. Both verify complete pages before headers. Remote coordinator timings include networking; this is not a capacity benchmark.',
68}, null, 2) + '\n')
69console.log(JSON.stringify({ passed: true, scans }))