|
| 1 | +import { readFile } from "fs-extra"; |
| 2 | +import { readJsonlFile } from "../../src/common/jsonl-reader"; |
| 3 | +import { performance } from "perf_hooks"; |
| 4 | +import { join } from "path"; |
| 5 | + |
| 6 | +/** An "obviously correct" implementation to test against. */ |
| 7 | +async function readJsonlReferenceImpl<T>( |
| 8 | + path: string, |
| 9 | + handler: (value: T) => Promise<void>, |
| 10 | +): Promise<void> { |
| 11 | + const logSummary = await readFile(path, "utf-8"); |
| 12 | + |
| 13 | + // Remove newline delimiters because summary is in .jsonl format. |
| 14 | + const jsonSummaryObjects: string[] = logSummary.split(/\r?\n\r?\n/g); |
| 15 | + |
| 16 | + for (const obj of jsonSummaryObjects) { |
| 17 | + const jsonObj = JSON.parse(obj) as T; |
| 18 | + await handler(jsonObj); |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +type ParserFn = ( |
| 23 | + text: string, |
| 24 | + callback: (v: unknown) => Promise<void>, |
| 25 | +) => Promise<void>; |
| 26 | + |
| 27 | +const parsers: Record<string, ParserFn> = { |
| 28 | + readJsonlReferenceImpl, |
| 29 | + readJsonlFile, |
| 30 | +}; |
| 31 | + |
| 32 | +async function main() { |
| 33 | + const args = process.argv.slice(2); |
| 34 | + const file = |
| 35 | + args.length > 0 |
| 36 | + ? args[0] |
| 37 | + : join( |
| 38 | + __dirname, |
| 39 | + "../unit-tests/data/evaluator-log-summaries/bad-join-order.jsonl", |
| 40 | + ); |
| 41 | + const numTrials = args.length > 1 ? Number(args[1]) : 100; |
| 42 | + const referenceValues: any[] = []; |
| 43 | + await readJsonlReferenceImpl(file, async (event) => { |
| 44 | + referenceValues.push(event); |
| 45 | + }); |
| 46 | + const referenceValueString = JSON.stringify(referenceValues); |
| 47 | + // Do warm-up runs and check against reference implementation |
| 48 | + for (const [name, parser] of Object.entries(parsers)) { |
| 49 | + const values: unknown[] = []; |
| 50 | + await parser(file, async (event) => { |
| 51 | + values.push(event); |
| 52 | + }); |
| 53 | + if (JSON.stringify(values) !== referenceValueString) { |
| 54 | + console.error(`${name}: failed to match reference implementation`); |
| 55 | + } |
| 56 | + } |
| 57 | + for (const [name, parser] of Object.entries(parsers)) { |
| 58 | + const startTime = performance.now(); |
| 59 | + for (let i = 0; i < numTrials; ++i) { |
| 60 | + await Promise.all([ |
| 61 | + parser(file, async () => {}), |
| 62 | + parser(file, async () => {}), |
| 63 | + ]); |
| 64 | + } |
| 65 | + const duration = performance.now() - startTime; |
| 66 | + const durationPerTrial = duration / numTrials; |
| 67 | + console.log(`${name}: ${durationPerTrial.toFixed(1)} ms`); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +main().catch((err: unknown) => { |
| 72 | + console.error(err); |
| 73 | +}); |
0 commit comments