/
index.ts
102 lines (86 loc) 路 2.85 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { writeFile } from 'fs';
import { join } from 'path';
import { suite, add, cycle, complete, save } from 'benny';
import stringify from 'csv-stringify/lib/sync';
import pkg from './package.json';
import { graph } from './graph';
import { DATA } from './data';
import { cases } from './cases';
import { Case } from './cases/abstract';
import { TsJsonValidatorCase } from './cases/ts-json-validator';
import { MarshalCase } from './cases/marshal';
const caseInstances: Case[] = cases.map(caseClass => new caseClass(DATA));
const RESULTS_DIR = join(__dirname, 'results');
const NODE_VERSION = process.env.NODE_VERSION || process.version;
async function main() {
await suiteDataTypeValidation();
await suiteDataTypeValidationSansOutliers();
}
main();
/**
* Benchmarking suite that performs data type validation only.
*
* https://en.wikipedia.org/wiki/Data_validation#Data-type_check
*/
async function suiteDataTypeValidation() {
await run('data-type', caseInstances, 'validate');
}
/**
* Benchmarking suite that performs data type validation only, but skips outliers
* because it is difficult to look at the performance of other packages.
*
* https://en.wikipedia.org/wiki/Data_validation#Data-type_check
*/
async function suiteDataTypeValidationSansOutliers() {
const cases = caseInstances
.filter(caseInstance => !(caseInstance instanceof MarshalCase))
.filter(caseInstance => !(caseInstance instanceof TsJsonValidatorCase));
await run('data-type-sans-outliers', cases, 'validate');
}
async function run(name: string, cases: Case[], methodName: keyof Case) {
const fns = cases.map(caseInstance =>
add(caseInstance.name, () => {
const method = caseInstance[methodName];
if (typeof method === 'function') {
return method.bind(caseInstance)();
} else {
throw new Error(`${methodName} is not a callable method.`);
}
})
);
return suite(
name,
// benchmark functions
...fns,
cycle(),
complete(),
// saves CSV and SVG
complete(async res => {
const csvArr = res.results.map(({ name, ops }) => {
return { name, ops };
});
const csv = stringify(csvArr, {
header: true,
columns: ['name', 'ops'],
});
const csvFilename = join(RESULTS_DIR, `${name}-${NODE_VERSION}.csv`);
await saveFile(csvFilename, csv);
const svg = await graph(csvFilename);
const svgFilename = join(RESULTS_DIR, `${name}-${NODE_VERSION}.svg`);
await saveFile(svgFilename, svg);
}),
// saves raw JSON results
save({
file: `${name}-${NODE_VERSION}`,
folder: RESULTS_DIR,
version: pkg.version,
})
);
}
function saveFile(filename: string, content: string) {
return new Promise((resolve, reject) => {
return writeFile(filename, content, { encoding: 'utf8' }, err =>
err ? reject(err) : resolve()
);
});
}