@dapplion/benchmark
Version:
Ensures that new code does not introduce performance regressions with CI. Tracks:
57 lines (56 loc) • 1.65 kB
JavaScript
import Ajv from "ajv";
export const emptyBenchmarkHistory = { benchmarks: {} };
const ajv = new Ajv({ allErrors: true });
/** TS type `BenchmarkResults` */
const benchmarkResultsSchema = {
type: "array",
items: {
type: "object",
properties: {
id: { type: "string" },
averageNs: { type: "number" },
runsDone: { type: "integer" },
totalMs: { type: "number" },
factor: { type: "number" },
},
required: ["id", "averageNs", "runsDone", "totalMs"],
},
};
/** TS type `Benchmark` */
const benchmarkSchema = {
type: "object",
properties: {
commitSha: { type: "string" },
results: benchmarkResultsSchema,
},
required: ["commitSha", "results"],
};
/** TS type `BenchmarkHistory` */
const benchmarkHistorySchema = {
type: "object",
properties: {
benchmarks: {
type: "object",
patternProperties: {
"^.*$": {
type: "array",
items: benchmarkSchema,
},
},
},
},
required: ["benchmarks"],
};
export function validateBenchmark(data) {
const validate = ajv.compile(benchmarkSchema);
const valid = validate(data);
if (!valid) {
throw Error(`Invalid BenchmarkResults ${JSON.stringify(validate.errors, null, 2)}`);
}
}
export function validateHistory(history) {
const validate = ajv.compile(benchmarkHistorySchema);
const valid = validate(history);
if (!valid)
throw Error(`Invalid BenchmarkHistory ${JSON.stringify(validate.errors, null, 2)}`);
}