T-122: M1.3g — @stock-bot/research: Vault-fenced universe planner + adapter method (survivorship-free active listings on a date from curated bars_1d Parquet, US_EQ / CA_EQ / US_CA_EQ) with tests and local benchmark (no new dependencies)
This commit is contained in:
parent
aecafdebe1
commit
91c7411ed9
4 changed files with 675 additions and 23 deletions
|
|
@ -2,6 +2,7 @@ import {
|
|||
type AdjFactorsSnapshotMetadata,
|
||||
BARS_ADJUSTMENTS,
|
||||
type BarsQuery,
|
||||
type DuckDbExecutor,
|
||||
type DuckDbParam,
|
||||
type DuckDbPlanContext,
|
||||
EVENT_KINDS,
|
||||
|
|
@ -9,12 +10,15 @@ import {
|
|||
type FxQuery,
|
||||
type PlannedDuckDbQuery,
|
||||
type ShortsAsOfQuery,
|
||||
UNIVERSE_REFS,
|
||||
type UniverseQuery,
|
||||
createDuckDbMarketDataStore,
|
||||
planBars1dQuery,
|
||||
planEventsQuery,
|
||||
planFx1dQuery,
|
||||
planShortsAsOfQuery,
|
||||
} from '../src/market-data-store-duckdb';
|
||||
import { applyVaultFence } from '../src/market-data-store';
|
||||
import { type TableRow, applyVaultFence } from '../src/market-data-store';
|
||||
|
||||
const SEED = 20261129;
|
||||
const QUERY_COUNT = 120_000;
|
||||
|
|
@ -231,17 +235,118 @@ if (checksumHex !== EXPECTED_CHECKSUM) {
|
|||
);
|
||||
}
|
||||
|
||||
console.log(
|
||||
JSON.stringify(
|
||||
{
|
||||
seed: SEED,
|
||||
queries: QUERY_COUNT,
|
||||
totalParams,
|
||||
elapsedMs: Number(elapsedMs.toFixed(2)),
|
||||
queriesPerSec,
|
||||
checksum: checksumHex,
|
||||
// Fixed-seed universe benchmark: plan + adapter mapping for 15,000 listing IDs via in-memory executor.
|
||||
const UNIVERSE_LISTING_COUNT = 15_000;
|
||||
const UNIVERSE_CALL_COUNT = 200;
|
||||
|
||||
async function runUniverseBenchmark() {
|
||||
const universeRng = createMulberry32(SEED);
|
||||
const listingRows: TableRow[] = new Array(UNIVERSE_LISTING_COUNT);
|
||||
let nextListingId = 1;
|
||||
for (let i = 0; i < UNIVERSE_LISTING_COUNT; i += 1) {
|
||||
listingRows[i] = { listing_id: nextListingId };
|
||||
nextListingId += 1 + Math.floor(universeRng() * 5);
|
||||
}
|
||||
|
||||
const universeQueries: UniverseQuery[] = new Array(UNIVERSE_CALL_COUNT);
|
||||
for (let i = 0; i < UNIVERSE_CALL_COUNT; i += 1) {
|
||||
const ref = UNIVERSE_REFS[i % UNIVERSE_REFS.length] ?? 'US_EQ';
|
||||
const dateMs = startBaseMs + Math.floor(universeRng() * preCutoffSpanMs);
|
||||
const date = new Date(dateMs).toISOString().slice(0, 10);
|
||||
universeQueries[i] = { ref, date };
|
||||
}
|
||||
|
||||
const timedExecutor: DuckDbExecutor = {
|
||||
async run(): Promise<void> {},
|
||||
async query(): Promise<readonly TableRow[]> {
|
||||
return listingRows;
|
||||
},
|
||||
null,
|
||||
2
|
||||
)
|
||||
);
|
||||
};
|
||||
const timedStore = createDuckDbMarketDataStore({
|
||||
executor: timedExecutor,
|
||||
curatedRoot: CURATED_ROOT,
|
||||
vaultCutoffTs: VAULT_CUTOFF_TS,
|
||||
});
|
||||
|
||||
// Timed section: universe query planning + adapter row validation/mapping.
|
||||
let totalListingsMapped = 0;
|
||||
const uStarted = performance.now();
|
||||
for (const q of universeQueries) {
|
||||
const ids = await timedStore.universe(q);
|
||||
totalListingsMapped += ids.length;
|
||||
}
|
||||
const uElapsedMs = performance.now() - uStarted;
|
||||
const uCallsPerSec = Math.round(UNIVERSE_CALL_COUNT / (uElapsedMs / 1000));
|
||||
const uRowsPerSec = Math.round(totalListingsMapped / (uElapsedMs / 1000));
|
||||
|
||||
// Untimed verification pass: hash SQL, ordered params, and mapped listing IDs.
|
||||
let uChecksum = 0x811c9dc5;
|
||||
const verifyExecutor: DuckDbExecutor = {
|
||||
async run(): Promise<void> {},
|
||||
async query(sql: string, params: readonly DuckDbParam[]): Promise<readonly TableRow[]> {
|
||||
uChecksum = fnv1aString(uChecksum, sql);
|
||||
uChecksum = fnv1aByte(uChecksum, params.length);
|
||||
for (const param of params) {
|
||||
uChecksum = fnv1aParam(uChecksum, param);
|
||||
}
|
||||
return listingRows;
|
||||
},
|
||||
};
|
||||
const verifyStore = createDuckDbMarketDataStore({
|
||||
executor: verifyExecutor,
|
||||
curatedRoot: CURATED_ROOT,
|
||||
vaultCutoffTs: VAULT_CUTOFF_TS,
|
||||
});
|
||||
|
||||
for (const q of universeQueries) {
|
||||
const ids = await verifyStore.universe(q);
|
||||
for (let i = 0; i < ids.length; i += 1) {
|
||||
const id = ids[i] ?? 0;
|
||||
uChecksum = fnv1aByte(uChecksum, id & 0xff);
|
||||
uChecksum = fnv1aByte(uChecksum, (id >>> 8) & 0xff);
|
||||
uChecksum = fnv1aByte(uChecksum, (id >>> 16) & 0xff);
|
||||
uChecksum = fnv1aByte(uChecksum, (id >>> 24) & 0xff);
|
||||
}
|
||||
}
|
||||
const uChecksumHex = uChecksum.toString(16).padStart(8, '0');
|
||||
const EXPECTED_UNIVERSE_CHECKSUM = 'ef5f1136';
|
||||
if (uChecksumHex !== EXPECTED_UNIVERSE_CHECKSUM) {
|
||||
throw new Error(
|
||||
`Universe benchmark checksum mismatch: expected ${EXPECTED_UNIVERSE_CHECKSUM}, got ${uChecksumHex}.`
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
seed: SEED,
|
||||
calls: UNIVERSE_CALL_COUNT,
|
||||
listingsPerCall: UNIVERSE_LISTING_COUNT,
|
||||
totalListingsMapped,
|
||||
elapsedMs: Number(uElapsedMs.toFixed(2)),
|
||||
callsPerSec: uCallsPerSec,
|
||||
rowsPerSec: uRowsPerSec,
|
||||
checksum: uChecksumHex,
|
||||
};
|
||||
}
|
||||
|
||||
runUniverseBenchmark()
|
||||
.then((universe) => {
|
||||
console.log(
|
||||
JSON.stringify(
|
||||
{
|
||||
seed: SEED,
|
||||
queries: QUERY_COUNT,
|
||||
totalParams,
|
||||
elapsedMs: Number(elapsedMs.toFixed(2)),
|
||||
queriesPerSec,
|
||||
checksum: checksumHex,
|
||||
universe,
|
||||
},
|
||||
null,
|
||||
2
|
||||
)
|
||||
);
|
||||
})
|
||||
.catch((err: unknown) => {
|
||||
console.error(err);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue