|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { |
| 4 | + readFileSync, |
| 5 | + writeFileSync, |
| 6 | +} from "node:fs"; |
| 7 | +import path from "node:path"; |
| 8 | +import process from "node:process"; |
| 9 | +import { pathToFileURL } from "node:url"; |
| 10 | + |
| 11 | +export const PRIMARY_METRICS_TOKEN = "@@QUICKJS_OXIDE_TEST262_PRIMARY@@"; |
| 12 | +export const DETAIL_METRICS_TOKEN = "@@QUICKJS_OXIDE_TEST262_DETAIL@@"; |
| 13 | + |
| 14 | +const DEFAULT_SPEC_PATH = path.resolve( |
| 15 | + import.meta.dirname, |
| 16 | + "../dev-support/test262/current.conf", |
| 17 | +); |
| 18 | +const NUMERIC_KEYS = [ |
| 19 | + "focused_variants", |
| 20 | + "focused_eligible", |
| 21 | + "focused_runnable", |
| 22 | + "focused_passes", |
| 23 | + "full_variants", |
| 24 | + "full_eligible", |
| 25 | + "full_runnable", |
| 26 | + "full_passes", |
| 27 | +]; |
| 28 | + |
| 29 | +function parseSpec(source) { |
| 30 | + const values = new Map(); |
| 31 | + for (const [index, rawLine] of source.split("\n").entries()) { |
| 32 | + const line = rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine; |
| 33 | + if (line === "" || line.startsWith("#")) { |
| 34 | + continue; |
| 35 | + } |
| 36 | + const match = line.match(/^([a-z][a-z0-9_]*)=(.*)$/u); |
| 37 | + if (!match) { |
| 38 | + throw new TypeError(`malformed Test262 spec line ${index + 1}`); |
| 39 | + } |
| 40 | + const [, key, value] = match; |
| 41 | + if ( |
| 42 | + value.length === 0 || |
| 43 | + value.trim() !== value || |
| 44 | + !/^[\x20-\x7e]+$/u.test(value) |
| 45 | + ) { |
| 46 | + throw new TypeError(`invalid Test262 spec value for ${key}`); |
| 47 | + } |
| 48 | + if (values.has(key)) { |
| 49 | + throw new TypeError(`duplicate Test262 spec key ${key}`); |
| 50 | + } |
| 51 | + values.set(key, value); |
| 52 | + } |
| 53 | + return values; |
| 54 | +} |
| 55 | + |
| 56 | +function required(values, key) { |
| 57 | + const value = values.get(key); |
| 58 | + if (value === undefined) { |
| 59 | + throw new TypeError(`missing Test262 spec key ${key}`); |
| 60 | + } |
| 61 | + return value; |
| 62 | +} |
| 63 | + |
| 64 | +function canonicalInteger(values, key) { |
| 65 | + const value = required(values, key); |
| 66 | + if (!/^(0|[1-9][0-9]*)$/u.test(value)) { |
| 67 | + throw new TypeError(`non-canonical Test262 integer ${key}`); |
| 68 | + } |
| 69 | + const number = Number(value); |
| 70 | + if (!Number.isSafeInteger(number)) { |
| 71 | + throw new TypeError(`Test262 integer exceeds the safe range: ${key}`); |
| 72 | + } |
| 73 | + return number; |
| 74 | +} |
| 75 | + |
| 76 | +function parseSummary(value, label) { |
| 77 | + const outcomes = new Map(); |
| 78 | + let previous = ""; |
| 79 | + for (const field of value.split(" ")) { |
| 80 | + const match = field.match(/^([a-z][a-z0-9-]*)=(0|[1-9][0-9]*)$/u); |
| 81 | + if (!match || match[1] <= previous || outcomes.has(match[1])) { |
| 82 | + throw new TypeError(`non-canonical ${label} Test262 summary`); |
| 83 | + } |
| 84 | + const count = Number(match[2]); |
| 85 | + if (!Number.isSafeInteger(count)) { |
| 86 | + throw new TypeError(`${label} Test262 summary exceeds the safe range`); |
| 87 | + } |
| 88 | + outcomes.set(match[1], count); |
| 89 | + previous = match[1]; |
| 90 | + } |
| 91 | + return outcomes; |
| 92 | +} |
| 93 | + |
| 94 | +function formatInteger(value) { |
| 95 | + return String(value).replace(/\B(?=(?:[0-9]{3})+(?![0-9]))/gu, ","); |
| 96 | +} |
| 97 | + |
| 98 | +function displayMilestone(value) { |
| 99 | + if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/u.test(value)) { |
| 100 | + throw new TypeError("invalid Test262 milestone name"); |
| 101 | + } |
| 102 | + return value |
| 103 | + .split("-") |
| 104 | + .map((part, index) => |
| 105 | + index === 0 |
| 106 | + ? `${part[0].toUpperCase()}${part.slice(1)}` |
| 107 | + : part.toUpperCase(), |
| 108 | + ) |
| 109 | + .join("-"); |
| 110 | +} |
| 111 | + |
| 112 | +export function parseCurrentTest262Metrics(source) { |
| 113 | + const values = parseSpec(source); |
| 114 | + if (required(values, "schema") !== "test262-gate-v2") { |
| 115 | + throw new TypeError("unsupported Test262 gate schema"); |
| 116 | + } |
| 117 | + const numbers = Object.fromEntries( |
| 118 | + NUMERIC_KEYS.map((key) => [key, canonicalInteger(values, key)]), |
| 119 | + ); |
| 120 | + const { |
| 121 | + focused_variants: focusedVariants, |
| 122 | + focused_eligible: focusedEligible, |
| 123 | + focused_runnable: focusedRunnable, |
| 124 | + focused_passes: focusedPasses, |
| 125 | + full_variants: fullVariants, |
| 126 | + full_eligible: fullEligible, |
| 127 | + full_runnable: fullRunnable, |
| 128 | + full_passes: fullPasses, |
| 129 | + } = numbers; |
| 130 | + if ( |
| 131 | + focusedPasses > focusedRunnable || |
| 132 | + focusedRunnable !== focusedEligible || |
| 133 | + focusedEligible > focusedVariants || |
| 134 | + fullRunnable === 0 || |
| 135 | + fullPasses > fullRunnable || |
| 136 | + fullRunnable !== fullEligible || |
| 137 | + fullEligible > fullVariants |
| 138 | + ) { |
| 139 | + throw new TypeError("inconsistent Test262 metric ordering"); |
| 140 | + } |
| 141 | + |
| 142 | + const focusedSummary = parseSummary( |
| 143 | + required(values, "focused_summary"), |
| 144 | + "focused", |
| 145 | + ); |
| 146 | + const fullSummary = parseSummary(required(values, "full_summary"), "full"); |
| 147 | + const focusedTotal = [...focusedSummary.values()].reduce( |
| 148 | + (total, count) => total + count, |
| 149 | + 0, |
| 150 | + ); |
| 151 | + const fullTotal = [...fullSummary.values()].reduce( |
| 152 | + (total, count) => total + count, |
| 153 | + 0, |
| 154 | + ); |
| 155 | + const fullIneligible = [...fullSummary] |
| 156 | + .filter(([name]) => /^(?:skipped|unsupported)-/u.test(name)) |
| 157 | + .reduce((total, [, count]) => total + count, 0); |
| 158 | + if ( |
| 159 | + focusedTotal !== focusedVariants || |
| 160 | + focusedSummary.get("pass") !== focusedPasses || |
| 161 | + fullTotal !== fullVariants || |
| 162 | + fullSummary.get("pass") !== fullPasses || |
| 163 | + fullVariants - fullIneligible !== fullEligible |
| 164 | + ) { |
| 165 | + throw new TypeError("Test262 summaries disagree with the official metrics"); |
| 166 | + } |
| 167 | + |
| 168 | + const milestone = displayMilestone(required(values, "milestone")); |
| 169 | + const runnableQuality = ((fullPasses / fullRunnable) * 100).toFixed(3); |
| 170 | + return Object.freeze({ |
| 171 | + detailText: |
| 172 | + `${milestone} authenticated vector · ` + |
| 173 | + `${formatInteger(focusedPasses)}/${formatInteger(focusedEligible)} focused · ` + |
| 174 | + `runnable quality ${formatInteger(fullPasses)}/${formatInteger(fullRunnable)} ` + |
| 175 | + `(${runnableQuality}%, secondary) · pre-parity`, |
| 176 | + focusedEligible, |
| 177 | + focusedPasses, |
| 178 | + fullEligible, |
| 179 | + fullPasses, |
| 180 | + fullRunnable, |
| 181 | + fullVariants, |
| 182 | + milestone, |
| 183 | + primaryText: |
| 184 | + `${formatInteger(fullPasses)} full passes / ` + |
| 185 | + `${formatInteger(fullEligible)} eligible / ` + |
| 186 | + `${formatInteger(fullVariants)} total`, |
| 187 | + runnableQuality, |
| 188 | + }); |
| 189 | +} |
| 190 | + |
| 191 | +export function readCurrentTest262Metrics(specPath = DEFAULT_SPEC_PATH) { |
| 192 | + return parseCurrentTest262Metrics(readFileSync(specPath, "utf8")); |
| 193 | +} |
| 194 | + |
| 195 | +function htmlText(value) { |
| 196 | + return value |
| 197 | + .replaceAll("&", "&") |
| 198 | + .replaceAll("<", "<") |
| 199 | + .replaceAll(">", ">") |
| 200 | + .replaceAll('"', """); |
| 201 | +} |
| 202 | + |
| 203 | +function replaceExactlyOnce(source, token, value) { |
| 204 | + const count = source.split(token).length - 1; |
| 205 | + if (count !== 1) { |
| 206 | + throw new TypeError( |
| 207 | + `expected exactly one ${token} placeholder, found ${count}`, |
| 208 | + ); |
| 209 | + } |
| 210 | + return source.replace(token, htmlText(value)); |
| 211 | +} |
| 212 | + |
| 213 | +export function renderCurrentTest262Metrics( |
| 214 | + htmlPath, |
| 215 | + specPath = DEFAULT_SPEC_PATH, |
| 216 | +) { |
| 217 | + const metrics = readCurrentTest262Metrics(specPath); |
| 218 | + let html = readFileSync(htmlPath, "utf8"); |
| 219 | + html = replaceExactlyOnce(html, PRIMARY_METRICS_TOKEN, metrics.primaryText); |
| 220 | + html = replaceExactlyOnce(html, DETAIL_METRICS_TOKEN, metrics.detailText); |
| 221 | + writeFileSync(htmlPath, html); |
| 222 | + return metrics; |
| 223 | +} |
| 224 | + |
| 225 | +const invokedAsScript = process.argv[1] |
| 226 | + ? pathToFileURL(path.resolve(process.argv[1])).href === import.meta.url |
| 227 | + : false; |
| 228 | +if (invokedAsScript) { |
| 229 | + if (process.argv.length !== 4 || process.argv[2] !== "--render") { |
| 230 | + console.error( |
| 231 | + "usage: current-test262-metrics.mjs --render PATH_TO_INDEX_HTML", |
| 232 | + ); |
| 233 | + process.exitCode = 2; |
| 234 | + } else { |
| 235 | + const metrics = renderCurrentTest262Metrics(path.resolve(process.argv[3])); |
| 236 | + console.log(`Rendered Pages metrics: ${metrics.primaryText}`); |
| 237 | + } |
| 238 | +} |
0 commit comments