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