|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +import fs from "fs/promises"; |
| 3 | +import path from "path"; |
| 4 | +import Papa from "papaparse"; |
| 5 | +import yaml from "js-yaml"; |
| 6 | + |
| 7 | +// Type definitions |
| 8 | +interface MetaData { |
| 9 | + [key: string]: any; |
| 10 | +} |
| 11 | + |
| 12 | +interface BenchmarkResult { |
| 13 | + Benchmark: string; |
| 14 | + Size: string; |
| 15 | + benchmarkId: string; |
| 16 | + [key: string]: any; |
| 17 | +} |
| 18 | + |
| 19 | +interface Size { |
| 20 | + size: string; |
| 21 | + [key: string]: any; |
| 22 | +} |
| 23 | + |
| 24 | +// Path constants |
| 25 | +const RESULTS_DIR = path.resolve(process.cwd(), "../results"); |
| 26 | +const METADATA_PATH = path.join(RESULTS_DIR, "metadata.yaml"); |
| 27 | +const RESULTS_PATH = path.join(RESULTS_DIR, "benchmark_results.csv"); |
| 28 | + |
| 29 | +/** |
| 30 | + * Reads and parses the metadata YAML file |
| 31 | + * @returns The parsed metadata or null on error |
| 32 | + */ |
| 33 | +const getMetaData = async (): Promise<{ benchmarks: MetaData } | null> => { |
| 34 | + try { |
| 35 | + const yamlText = await fs.readFile(METADATA_PATH, "utf-8"); |
| 36 | + const rawData = yaml.load(yamlText) as { benchmarks: MetaData }; |
| 37 | + return rawData; |
| 38 | + } catch (error) { |
| 39 | + console.error("Error reading metadata:", error); |
| 40 | + return null; |
| 41 | + } |
| 42 | +}; |
| 43 | + |
| 44 | +/** |
| 45 | + * Reads and parses the benchmark results CSV file |
| 46 | + * @returns Array of benchmark results |
| 47 | + */ |
| 48 | +async function getBenchmarkResults(): Promise<BenchmarkResult[]> { |
| 49 | + try { |
| 50 | + const fileContent = await fs.readFile(RESULTS_PATH, "utf-8"); |
| 51 | + const parsedResults = Papa.parse<BenchmarkResult>(fileContent, { |
| 52 | + header: true, |
| 53 | + skipEmptyLines: true, |
| 54 | + dynamicTyping: true, |
| 55 | + }); |
| 56 | + return parsedResults.data; |
| 57 | + } catch (error) { |
| 58 | + console.error("Error reading benchmark results:", error); |
| 59 | + throw error; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Validates that all benchmark results have corresponding metadata entries |
| 65 | + */ |
| 66 | +export async function validateData(): Promise<void> { |
| 67 | + try { |
| 68 | + const [results, metaData] = await Promise.all([ |
| 69 | + getBenchmarkResults(), |
| 70 | + getMetaData(), |
| 71 | + ]); |
| 72 | + |
| 73 | + if (!metaData) { |
| 74 | + throw new Error("Failed to load metadata"); |
| 75 | + } |
| 76 | + const invalidResults = results.filter((result) => { |
| 77 | + const benchmark = metaData.benchmarks[result.Benchmark]; |
| 78 | + return ( |
| 79 | + !benchmark || |
| 80 | + !benchmark.Sizes?.some((s: Size) => s.Name === result.Size) |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + if (invalidResults.length > 0) { |
| 85 | + invalidResults.forEach((result) => { |
| 86 | + console.error( |
| 87 | + `Validation failed for benchmark: ${result.Benchmark} with size: ${result.Size}`, |
| 88 | + ); |
| 89 | + }); |
| 90 | + throw new Error( |
| 91 | + `${invalidResults.length} benchmark results failed validation`, |
| 92 | + ); |
| 93 | + } |
| 94 | + } catch (error) { |
| 95 | + console.error("Error validating data:", error); |
| 96 | + throw error; |
| 97 | + } |
| 98 | +} |
0 commit comments