@dapplion/benchmark
Version:
Ensures that new code does not introduce performance regressions with CI. Tracks:
65 lines (64 loc) • 2.01 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.emptyBenchmarkHistory = void 0;
exports.validateBenchmark = validateBenchmark;
exports.validateHistory = validateHistory;
const ajv_1 = __importDefault(require("ajv"));
exports.emptyBenchmarkHistory = { benchmarks: {} };
const ajv = new ajv_1.default({ 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"],
};
function validateBenchmark(data) {
const validate = ajv.compile(benchmarkSchema);
const valid = validate(data);
if (!valid) {
throw Error(`Invalid BenchmarkResults ${JSON.stringify(validate.errors, null, 2)}`);
}
}
function validateHistory(history) {
const validate = ajv.compile(benchmarkHistorySchema);
const valid = validate(history);
if (!valid)
throw Error(`Invalid BenchmarkHistory ${JSON.stringify(validate.errors, null, 2)}`);
}