T-119: M1.3e — @stock-bot/research: DuckDB SQL planner + injected-executor MarketDataStore adapter for curated daily Parquet (bars 1d, events, shortsAsOf, fx 1d) with mandatory Vault / known_at predicates, tests and local benchmark (no new dependencies)
This commit is contained in:
parent
f9bbc1d4a2
commit
57b398694f
6 changed files with 2406 additions and 0 deletions
172
libs/research/scripts/benchmark-market-data-store-duckdb.ts
Normal file
172
libs/research/scripts/benchmark-market-data-store-duckdb.ts
Normal file
|
|
@ -0,0 +1,172 @@
|
|||
import {
|
||||
BARS_ADJUSTMENTS,
|
||||
type BarsQuery,
|
||||
type DuckDbPlanContext,
|
||||
EVENT_KINDS,
|
||||
type EventsQuery,
|
||||
type FxQuery,
|
||||
type PlannedDuckDbQuery,
|
||||
type ShortsAsOfQuery,
|
||||
planBars1dQuery,
|
||||
planEventsQuery,
|
||||
planFx1dQuery,
|
||||
planShortsAsOfQuery,
|
||||
} from '../src/market-data-store-duckdb';
|
||||
|
||||
const SEED = 20261129;
|
||||
const QUERY_COUNT = 120_000;
|
||||
const CURATED_ROOT = '/mnt/data/stockbot/curated';
|
||||
const VAULT_CUTOFF_TS = Date.UTC(2026, 4, 28, 0, 0, 0, 0);
|
||||
|
||||
const CTX: DuckDbPlanContext = {
|
||||
curatedRoot: CURATED_ROOT,
|
||||
vaultCutoffTs: VAULT_CUTOFF_TS,
|
||||
};
|
||||
|
||||
function createMulberry32(seed: number): () => number {
|
||||
let state = seed >>> 0;
|
||||
return () => {
|
||||
state = (state + 0x6d2b79f5) >>> 0;
|
||||
let t = Math.imul(state ^ (state >>> 15), 1 | state);
|
||||
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
|
||||
return ((t ^ (t >>> 14)) >>> 0) / 4_294_967_296;
|
||||
};
|
||||
}
|
||||
|
||||
type WorkloadItem =
|
||||
| { readonly kind: 'bars'; readonly q: BarsQuery }
|
||||
| { readonly kind: 'events'; readonly q: EventsQuery }
|
||||
| { readonly kind: 'shorts'; readonly q: ShortsAsOfQuery }
|
||||
| { readonly kind: 'fx'; readonly q: FxQuery };
|
||||
|
||||
const rng = createMulberry32(SEED);
|
||||
const startBaseMs = Date.UTC(2016, 0, 1, 0, 0, 0, 0);
|
||||
const preCutoffSpanMs = VAULT_CUTOFF_TS - startBaseMs - 86_400_000;
|
||||
const postCutoffSpanMs = 180 * 86_400_000;
|
||||
|
||||
function randomDateOrIso(epochMs: number): string {
|
||||
const iso = new Date(epochMs).toISOString();
|
||||
return rng() < 0.5 ? iso.slice(0, 10) : iso;
|
||||
}
|
||||
|
||||
function randomListingIds(): number[] {
|
||||
const count = 1 + Math.floor(rng() * 4);
|
||||
const ids: number[] = new Array(count);
|
||||
for (let i = 0; i < count; i += 1) {
|
||||
ids[i] = 1 + Math.floor(rng() * 50_000);
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
|
||||
const workload: WorkloadItem[] = new Array(QUERY_COUNT);
|
||||
for (let i = 0; i < QUERY_COUNT; i += 1) {
|
||||
const fromMs = startBaseMs + Math.floor(rng() * preCutoffSpanMs);
|
||||
const toMs = fromMs + Math.floor(rng() * (VAULT_CUTOFF_TS - fromMs + postCutoffSpanMs));
|
||||
const asOfMs =
|
||||
rng() < 0.35
|
||||
? undefined
|
||||
: startBaseMs + Math.floor(rng() * (VAULT_CUTOFF_TS - startBaseMs + postCutoffSpanMs));
|
||||
|
||||
const from = randomDateOrIso(fromMs);
|
||||
const to = randomDateOrIso(toMs);
|
||||
const asOf = asOfMs === undefined ? undefined : randomDateOrIso(asOfMs);
|
||||
|
||||
const slot = i % 7;
|
||||
if (slot < 3) {
|
||||
const adjustment = BARS_ADJUSTMENTS[slot] ?? 'raw';
|
||||
workload[i] = {
|
||||
kind: 'bars',
|
||||
q: {
|
||||
listingIds: randomListingIds(),
|
||||
timeframe: '1d',
|
||||
from,
|
||||
to,
|
||||
adjustment,
|
||||
asOf,
|
||||
},
|
||||
};
|
||||
} else if (slot === 3 || slot === 4) {
|
||||
const kindA = EVENT_KINDS[Math.floor(rng() * EVENT_KINDS.length)] ?? 'filing';
|
||||
const kindB = EVENT_KINDS[Math.floor(rng() * EVENT_KINDS.length)] ?? 'earnings';
|
||||
workload[i] = {
|
||||
kind: 'events',
|
||||
q: {
|
||||
kinds: slot === 3 ? [kindA] : [kindA, kindB],
|
||||
listingIds: slot === 3 ? randomListingIds() : undefined,
|
||||
from,
|
||||
to,
|
||||
asOf,
|
||||
},
|
||||
};
|
||||
} else if (slot === 5) {
|
||||
workload[i] = {
|
||||
kind: 'shorts',
|
||||
q: {
|
||||
listingIds: randomListingIds(),
|
||||
from,
|
||||
to,
|
||||
asOf,
|
||||
},
|
||||
};
|
||||
} else {
|
||||
workload[i] = {
|
||||
kind: 'fx',
|
||||
q: {
|
||||
pair: 'USD.CAD',
|
||||
timeframe: '1d',
|
||||
from,
|
||||
to,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function mixFnv1a(hash: number, value: number): number {
|
||||
let h = (hash ^ (value >>> 0)) >>> 0;
|
||||
h = Math.imul(h, 0x01000193) >>> 0;
|
||||
return h;
|
||||
}
|
||||
|
||||
let checksum = 0x811c9dc5;
|
||||
let totalParams = 0;
|
||||
|
||||
const started = performance.now();
|
||||
for (const item of workload) {
|
||||
let planned: PlannedDuckDbQuery;
|
||||
switch (item.kind) {
|
||||
case 'bars':
|
||||
planned = planBars1dQuery(item.q, CTX);
|
||||
break;
|
||||
case 'events':
|
||||
planned = planEventsQuery(item.q, CTX);
|
||||
break;
|
||||
case 'shorts':
|
||||
planned = planShortsAsOfQuery(item.q, CTX);
|
||||
break;
|
||||
case 'fx':
|
||||
planned = planFx1dQuery(item.q, CTX);
|
||||
break;
|
||||
}
|
||||
totalParams += planned.params.length;
|
||||
checksum = mixFnv1a(checksum, planned.sql.length);
|
||||
checksum = mixFnv1a(checksum, planned.params.length);
|
||||
checksum = mixFnv1a(checksum, Math.trunc(planned.fenced.toMs / 1000));
|
||||
checksum = mixFnv1a(checksum, Math.trunc(planned.fenced.asOfMs / 1000));
|
||||
}
|
||||
const elapsedMs = performance.now() - started;
|
||||
const queriesPerSec = Math.round(QUERY_COUNT / (elapsedMs / 1000));
|
||||
|
||||
console.log(
|
||||
JSON.stringify(
|
||||
{
|
||||
seed: SEED,
|
||||
queries: QUERY_COUNT,
|
||||
totalParams,
|
||||
elapsedMs: Number(elapsedMs.toFixed(2)),
|
||||
queriesPerSec,
|
||||
checksum: checksum.toString(16).padStart(8, '0'),
|
||||
},
|
||||
null,
|
||||
2
|
||||
)
|
||||
);
|
||||
Loading…
Add table
Add a link
Reference in a new issue