forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.ts
142 lines (124 loc) · 4.14 KB
/
benchmark.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
// tslint:disable:no-implicit-dependencies
// tslint:disable:no-console
import { tags } from '@angular-devkit/core';
import * as colors from 'ansi-colors';
import * as glob from 'glob';
import 'jasmine';
import { SpecReporter as JasmineSpecReporter } from 'jasmine-spec-reporter';
import { join, relative } from 'path';
const Jasmine = require('jasmine');
const projectBaseDir = join(__dirname, '../packages');
require('source-map-support').install({
hookRequire: true,
});
declare const global: {
benchmarkReporter: {};
};
interface BenchmarkResult {
count: number;
slowest: number[];
fastest: number[];
mean: number;
average: number;
base?: BenchmarkResult;
}
class BenchmarkReporter extends JasmineSpecReporter implements jasmine.CustomReporter {
private _stats: BenchmarkResult | null = null;
constructor() {
super({
summary: {},
});
}
reportBenchmark(stats: BenchmarkResult) {
this._stats = stats;
}
jasmineStarted(suiteInfo: jasmine.SuiteInfo): void {
super.jasmineStarted(suiteInfo);
}
suiteStarted(result: jasmine.CustomReporterResult): void {
super.suiteStarted(result);
}
specStarted(result: jasmine.CustomReporterResult): void {
super.specStarted(result);
this._stats = null;
}
specDone(result: jasmine.CustomReporterResult): void {
super.specDone(result);
if (result.status == 'passed' && this._stats) {
const stat = this._stats;
const padding = ' ';
function pad(x: string | number, p: string = padding): string {
const s = ('' + x).replace(/\B(?=(\d{3})+(?!\d))/g, ' ');
return p.substr(0, p.length - ('' + s).length) + s;
}
const count = pad(stat.count);
const fastest = stat.fastest.map(x => pad(x)).join('');
const slowest = stat.slowest.map(x => pad(x)).join('');
const mean = pad(Math.floor(stat.mean));
const average = pad(Math.floor(stat.average));
if (stat.base) {
const precision = (x: number) => {
x = Math.floor(x * 100);
return `${Math.floor(x / 100)}.${Math.floor(x / 10) % 10}${x % 10}`;
};
const multPad = ' ';
const baseFastest = stat.base.fastest.map(x => pad(x)).join('');
const baseSlowest = stat.base.slowest.map(x => pad(x)).join('');
const baseMean = pad(Math.floor(stat.base.mean));
const baseMeanMult = pad(precision(stat.mean / stat.base.mean), multPad);
const baseAverage = pad(Math.floor(stat.base.average));
const baseAverageMult = pad(precision(stat.average / stat.base.average), multPad);
console.log(
colors.yellow(tags.indentBy(6)`
count: ${count}
fastest: ${fastest}
(base) ${baseFastest}
slowest: ${slowest}
(base) ${baseSlowest}
mean: ${mean} (${baseMean}) (${baseMeanMult}x)
average: ${average} (${baseAverage}) (${baseAverageMult}x)
`),
);
} else {
console.log(
colors.yellow(tags.indentBy(6)`
count: ${count}
fastest: ${fastest}
slowest: ${slowest}
mean: ${mean}
average: ${average}
`),
);
}
}
}
suiteDone(result: jasmine.CustomReporterResult): void {
super.suiteDone(result);
}
jasmineDone(runDetails: jasmine.RunDetails): void {
super.jasmineDone(runDetails);
}
}
// Create a Jasmine runner and configure it.
const runner = new Jasmine({ projectBaseDir: projectBaseDir });
runner.env.clearReporters();
global.benchmarkReporter = new BenchmarkReporter();
runner.env.addReporter(global.benchmarkReporter);
// Run the tests.
const allTests = glob
.sync('packages/**/*_benchmark.ts')
.map(p => relative(projectBaseDir, p))
.filter(p => !/schematics_cli\/schematics\//.test(p));
export default function(_args: {}) {
return new Promise(resolve => {
runner.onComplete((passed: boolean) => resolve(passed ? 0 : 1));
runner.execute(allTests);
});
}