File size: 8,503 Bytes
4e23b01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Runs the real bench script in --quick mode and pins the machine-readable
// JSON report's shape: later phases diff before/after numbers, so the field
// names below are a stable contract.
import { test } from 'vitest';
import assert from 'node:assert/strict';
import { execFile } from 'node:child_process';
import { promisify } from 'node:util';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

const pkgDir = fileURLToPath(new URL('..', import.meta.url));

test('bench --quick emits a JSON report with a stable schema', async () => {
  const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'minidb-bench-json-'));
  try {
    const reportPath = path.join(dir, 'report.json');
    await promisify(execFile)(
      process.execPath,
      ['--import', 'tsx', path.join(pkgDir, 'bench', 'bench.ts'), '--quick', '--json', reportPath],
      { cwd: pkgDir, timeout: 240_000, maxBuffer: 16 * 1024 * 1024 },
    );
    const report = JSON.parse(await fs.readFile(reportPath, 'utf8'));

    // Top-level envelope.
    assert.equal(report.schemaVersion, 1);
    assert.equal(report.tool, 'minidb/bench');
    assert.equal(typeof report.startedAt, 'string');
    assert.ok(!Number.isNaN(Date.parse(report.startedAt)), 'startedAt is an ISO timestamp');
    assert.equal(typeof report.node, 'string');
    assert.equal(typeof report.platform, 'string');
    assert.equal(typeof report.arch, 'string');
    assert.equal(typeof report.seed, 'number');
    assert.ok(Array.isArray(report.scenarios));
    assert.ok(report.scenarios.length >= 10, 'all scenario families ran');

    // Every scenario row carries the shared measurement fields.
    for (const s of report.scenarios) {
      assert.equal(typeof s.name, 'string');
      assert.ok(s.name.length > 0);
      assert.equal(typeof s.durationMs, 'number', `${s.name}.durationMs`);
      assert.ok(s.durationMs >= 0);
      if (s.ops !== undefined) assert.equal(typeof s.ops, 'number', `${s.name}.ops`);
      if (s.opsPerSec !== undefined) assert.equal(typeof s.opsPerSec, 'number', `${s.name}.opsPerSec`);
      for (const k of ['mean', 'p50', 'p95', 'p99', 'max']) {
        assert.equal(typeof s.eventLoopDelayMs[k], 'number', `${s.name}.eventLoopDelayMs.${k}`);
      }
      assert.ok(s.peakRssBytes > 0, `${s.name}.peakRssBytes`);
      assert.ok(s.peakHeapUsedBytes > 0, `${s.name}.peakHeapUsedBytes`);
      if (s.latencyMs !== undefined) {
        for (const k of ['p50', 'p95', 'p99', 'max']) {
          assert.equal(typeof s.latencyMs[k], 'number', `${s.name}.latencyMs.${k}`);
        }
      }
    }

    const byName = (re) => report.scenarios.find((s) => re.test(s.name));

    // Cold open rows expose the recovery/rebuild breakdown.
    const cold = byName(/cold open/);
    assert.ok(cold, 'a cold-open scenario exists');
    for (const k of ['keys', 'recoveryBytes', 'recoveryFrames', 'recoveryDurationMs', 'indexRebuildDurationMs', 'textRebuildDurationMs', 'walFsyncs']) {
      assert.equal(typeof cold.extra[k], 'number', `cold open extra.${k}`);
    }

    // The acceptance scenario: an idle everysec db performs zero fsyncs.
    const idle = byName(/idle everysec/);
    assert.ok(idle, 'the idle everysec scenario exists');
    assert.equal(idle.extra.walFsyncs, 0, 'idle everysec db: zero background fsyncs');
    assert.equal(idle.extra.walFsyncErrors, 0);

    // A write re-arms the background sync.
    const dirty = byName(/write then idle/);
    assert.ok(dirty, 'the dirty-window scenario exists');
    assert.ok(dirty.extra.walFsyncs >= 1, 'a write triggers a background fsync');

    // Search rows report per-query hit counts and medians.
    for (const re of [/search word/, /search ngram/]) {
      const search = byName(re);
      assert.ok(search, `${re} scenario exists`);
      assert.ok(Array.isArray(search.extra.queries));
      for (const q of search.extra.queries) {
        assert.equal(typeof q.q, 'string');
        assert.equal(typeof q.hits, 'number');
        assert.equal(typeof q.medianMs, 'number');
      }
    }

    // The compaction row exposes the phase breakdown.
    const compact = byName(/compact \d/);
    assert.ok(compact, 'a compaction scenario exists');
    for (const k of ['keys', 'compactionDurationMs', 'compactionSnapshotDurationMs', 'compactionRotationDurationMs', 'compactionPostingsDurationMs', 'snapshotBytesWritten']) {
      assert.equal(typeof compact.extra[k], 'number', `compact extra.${k}`);
    }
  } finally {
    await fs.rm(dir, { recursive: true, force: true });
  }
}, 300_000);

test('open-lifecycle bench --quick emits the phase-1 baseline report', async () => {
  const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'minidb-open-bench-json-'));
  try {
    const reportPath = path.join(dir, 'report.json');
    await promisify(execFile)(
      process.execPath,
      ['--import', 'tsx', path.join(pkgDir, 'bench', 'open-lifecycle.ts'), '--quick', '--json', reportPath],
      { cwd: pkgDir, timeout: 240_000, maxBuffer: 16 * 1024 * 1024 },
    );
    const report = JSON.parse(await fs.readFile(reportPath, 'utf8'));

    // Top-level envelope.
    assert.equal(report.schemaVersion, 1);
    assert.equal(report.tool, 'minidb/bench/open-lifecycle');
    assert.ok(Array.isArray(report.scenarios));

    const byName = (re) => report.scenarios.find((s) => re.test(s.name));

    // Every row carries the event-loop-delay + input-latency baselines.
    for (const s of report.scenarios) {
      assert.equal(typeof s.durationMs, 'number', `${s.name}.durationMs`);
      for (const k of ['mean', 'p50', 'p95', 'p99', 'max']) {
        assert.equal(typeof s.eventLoopDelayMs[k], 'number', `${s.name}.eventLoopDelayMs.${k}`);
      }
      assert.ok(s.inputLatencyMs, `${s.name}.inputLatencyMs`);
      for (const k of ['count', 'p50', 'p95', 'p99', 'max']) {
        assert.equal(typeof s.inputLatencyMs[k], 'number', `${s.name}.inputLatencyMs.${k}`);
      }
    }

    // The four scenario families exist with their lifecycle phase breakdown.
    const PHASES = [
      'openMs',
      'generationCandidateLoadMs',
      'storeImageLoadMs',
      'nonTextImageLoadMs',
      'textImageLoadMs',
      'postingsIntegrityCheckMs',
      'walScanMs',
      'walApplyMs',
      'fullRecoveryMs',
      'textRebuildMs',
    ];
    const small = byName(/small corpus, healthy generation/);
    const wal = byName(/large WAL delta/);
    const large = byName(/large full-text generation/);
    const corrupt = byName(/corrupt generation \(full-recovery fallback\)/);
    const deferred = byName(/deferred text rebuild \(degraded -> ready\)/);
    for (const [name, s] of Object.entries({ small, wal, large, corrupt, deferred })) {
      assert.ok(s, `${name} scenario exists`);
      for (const k of PHASES) assert.equal(typeof s.extra.phases[k], 'number', `${name}.extra.phases.${k}`);
    }

    // Healthy generation opens: no full recovery, no corpus tokenization.
    for (const [name, s] of Object.entries({ small, wal, large })) {
      assert.equal(s.extra.state, 'ready', name);
      assert.deepEqual(s.extra.path, ['no-generation', 'generation-load', 'wal-catch-up', 'ready'], name);
      assert.equal(s.extra.phases.fullRecoveryMs, 0, name);
      assert.equal(s.extra.phases.textRebuildMs, 0, name);
      assert.equal(s.extra.generationLoads, 1, name);
      assert.equal(s.extra.indexRebuildDecoded, 0, name);
    }
    // The WAL-delta open really replayed the delta (and close did NOT
    // republish it into a fresh generation — the scenario would be void).
    assert.ok(wal.extra.walDeltaAppliedOps > 0, 'wal delta replayed');
    assert.equal(wal.extra.closePublishedGeneration, false, 'close left the generation untouched');
    assert.ok(wal.extra.phases.walApplyMs > 0, 'wal apply measured');

    // The corrupt generation fell back to a full recovery and returned
    // degraded; the background deferred build then brought it to ready.
    assert.equal(corrupt.extra.state, 'degraded');
    assert.deepEqual(corrupt.extra.path, ['no-generation', 'generation-load', 'full-rebuild', 'degraded']);
    assert.ok(corrupt.extra.phases.fullRecoveryMs > 0, 'full recovery measured');
    assert.equal(corrupt.extra.generationLoadFallbacks, 1);
    assert.equal(deferred.extra.state, 'ready');
    assert.ok(deferred.extra.textDeferredBuilds >= 1, 'deferred build committed');
  } finally {
    await fs.rm(dir, { recursive: true, force: true });
  }
}, 300_000);