|
1 | | -import { Type, Static } from '@sinclair/typebox' |
2 | 1 | import console from 'console' |
3 | 2 | import { readFileSync } from 'fs' |
4 | | -import process from 'process' |
5 | | -import createAjv from '../lib/ajv' |
6 | 3 |
|
7 | 4 | export const PREFIX = 'tmp.benchmark-report' |
8 | 5 |
|
@@ -36,26 +33,40 @@ const postProcessSchema = <Schema extends {}>(schema: Schema) => ({ |
36 | 33 | additionalProperties: true, |
37 | 34 | }) |
38 | 35 |
|
39 | | -export const ReportUnit = postProcessSchema(Type.Object({ |
40 | | - command: Type.String(), |
41 | | - mean: Type.Number(), |
42 | | - min: Type.Number(), |
43 | | - max: Type.Number(), |
44 | | -})) |
45 | | -export type ReportUnit = Static<typeof ReportUnit> |
| 36 | +export interface ReportUnit { |
| 37 | + command: string |
| 38 | + mean: number |
| 39 | + min: number |
| 40 | + max: number |
| 41 | +} |
46 | 42 |
|
47 | | -export const Report = postProcessSchema(Type.Object({ |
48 | | - results: Type.Array(ReportUnit), |
49 | | -})) |
50 | | -export type Report = Static<typeof Report> |
| 43 | +export interface Report { |
| 44 | + results: ReportUnit[] |
| 45 | +} |
51 | 46 |
|
52 | 47 | export function assertReport(data: unknown): asserts data is Report { |
53 | | - const ajv = createAjv() |
54 | | - const valid = ajv.validate(Report, data) |
55 | | - if (valid) return |
56 | | - console.error('ValidationError', { data }) |
57 | | - console.error(ajv.errorsText(ajv.errors)) |
58 | | - throw process.exit(1) |
| 48 | + if (typeof data !== 'object' || data === null) { |
| 49 | + console.error(data) |
| 50 | + throw new TypeError(`Data is not an object: ${data}`) |
| 51 | + } |
| 52 | + const { results } = data as { [_ in string]: unknown } |
| 53 | + if (!Array.isArray(results)) { |
| 54 | + console.error(data) |
| 55 | + throw new TypeError(`Property 'results' is not an array`) |
| 56 | + } |
| 57 | + for (const item of results) { |
| 58 | + if (typeof item !== 'object' || data === null) { |
| 59 | + console.error(item) |
| 60 | + throw new TypeError(`An item is not an object: ${item}`) |
| 61 | + } |
| 62 | + for (const name of ['command', 'mean', 'min', 'max'] as const) { |
| 63 | + if (name in item) { |
| 64 | + continue |
| 65 | + } |
| 66 | + console.error(item) |
| 67 | + throw new TypeError(`Property '${name}' does not exist in an item`) |
| 68 | + } |
| 69 | + } |
59 | 70 | } |
60 | 71 |
|
61 | 72 | export function loadByPath(path: string): Report { |
|
0 commit comments