@@ -4,43 +4,172 @@ import { Compiler } from 'jsii/lib/compiler';
4
4
import { loadProjectInfo } from 'jsii/lib/project-info' ;
5
5
import * as os from 'os' ;
6
6
import * as path from 'path' ;
7
+ import * as ts from 'typescript-3.9' ;
7
8
8
9
import { Benchmark } from './benchmark' ;
9
10
import { cdkv2_21_1 , cdkTagv2_21_1 } from './constants' ;
10
- import { streamUntar } from './util' ;
11
-
12
- // Always run against the same version of CDK source
13
- const cdk = new Benchmark ( `Compile aws-cdk-lib@${ cdkTagv2_21_1 } ` )
14
- . setup ( async ( ) => {
15
- const sourceDir = fs . mkdtempSync (
16
- path . join ( os . tmpdir ( ) , 'jsii-cdk-bench-snapshot' ) ,
17
- ) ;
18
- await streamUntar ( cdkv2_21_1 , { cwd : sourceDir } ) ;
19
- cp . execSync ( 'npm ci' , { cwd : sourceDir } ) ;
20
-
21
- // Working directory for benchmark
22
- const workingDir = fs . mkdtempSync (
23
- path . join ( os . tmpdir ( ) , `jsii-cdk-bench@${ cdkTagv2_21_1 } ` ) ,
24
- ) ;
25
-
26
- return {
27
- workingDir,
28
- sourceDir,
29
- } as const ;
30
- } )
31
- . beforeEach ( ( { workingDir, sourceDir } ) => {
32
- fs . removeSync ( workingDir ) ;
33
- fs . copySync ( sourceDir , workingDir ) ;
34
- } )
35
- . subject ( ( { workingDir } ) => {
36
- const { projectInfo } = loadProjectInfo ( workingDir ) ;
37
- const compiler = new Compiler ( { projectInfo } ) ;
38
-
39
- compiler . emit ( ) ;
40
- } )
41
- . teardown ( ( { workingDir, sourceDir } ) => {
42
- fs . removeSync ( workingDir ) ;
43
- fs . removeSync ( sourceDir ) ;
44
- } ) ;
45
-
46
- export const benchmarks = [ cdk ] ;
11
+ import { inDirectory , streamUntar } from './util' ;
12
+
13
+ // Using the local `npm` package (from dependencies)
14
+ const npm = path . resolve ( __dirname , '..' , 'node_modules' , '.bin' , 'npm' ) ;
15
+
16
+ export const benchmarks = [
17
+ // Reference comparison using the TypeScript compiler
18
+ new Benchmark ( `Compile aws-cdk-lib@${ cdkTagv2_21_1 } (tsc)` )
19
+ . setup ( async ( ) => {
20
+ const sourceDir = fs . mkdtempSync (
21
+ path . join ( os . tmpdir ( ) , 'jsii-cdk-bench-snapshot' ) ,
22
+ ) ;
23
+ await streamUntar ( cdkv2_21_1 , { cwd : sourceDir } ) ;
24
+ cp . execSync ( `${ npm } ci` , { cwd : sourceDir } ) ;
25
+
26
+ // Working directory for benchmark
27
+ const workingDir = fs . mkdtempSync (
28
+ path . join ( os . tmpdir ( ) , `tsc-cdk-bench@${ cdkTagv2_21_1 } ` ) ,
29
+ ) ;
30
+
31
+ return {
32
+ workingDir,
33
+ sourceDir,
34
+ } as const ;
35
+ } )
36
+ . beforeEach ( ( { workingDir, sourceDir } ) => {
37
+ fs . removeSync ( workingDir ) ;
38
+ fs . copySync ( sourceDir , workingDir ) ;
39
+ } )
40
+ . subject ( ( { workingDir } ) =>
41
+ inDirectory ( workingDir , ( ) => {
42
+ const { host, options, rootNames } = ( function ( ) {
43
+ const parsed = ts . parseJsonConfigFileContent (
44
+ fs . readJsonSync ( path . join ( workingDir , 'tsconfig.json' ) ) ,
45
+ ts . sys ,
46
+ workingDir ,
47
+ {
48
+ module : ts . ModuleKind . CommonJS ,
49
+ moduleResolution : ts . ModuleResolutionKind . NodeJs ,
50
+ newLine : ts . NewLineKind . LineFeed ,
51
+ tsBuildInfoFile : 'tsconfig.tsbuildinfo' ,
52
+ } ,
53
+ 'tsconfig.json' ,
54
+ ) ;
55
+
56
+ const host = ts . createIncrementalCompilerHost ( parsed . options , ts . sys ) ;
57
+
58
+ return {
59
+ host,
60
+ options : parsed . options ,
61
+ rootNames : [
62
+ ...parsed . fileNames ,
63
+ ...( parsed . options . lib && host . getDefaultLibLocation != null
64
+ ? parsed . options . lib . map ( ( lib ) =>
65
+ path . join ( host . getDefaultLibLocation ! ( ) , lib ) ,
66
+ )
67
+ : [ ] ) ,
68
+ ] ,
69
+ } ;
70
+ } ) ( ) ;
71
+
72
+ const program = ts
73
+ . createIncrementalProgram ( {
74
+ createProgram : ts . createEmitAndSemanticDiagnosticsBuilderProgram ,
75
+ host,
76
+ options,
77
+ rootNames,
78
+ } )
79
+ . getProgram ( ) ;
80
+
81
+ const preEmitDiagnostics = ts . getPreEmitDiagnostics ( program ) ;
82
+ if (
83
+ preEmitDiagnostics . some (
84
+ ( diag ) => diag . category === ts . DiagnosticCategory . Error ,
85
+ )
86
+ ) {
87
+ console . error (
88
+ ts . formatDiagnosticsWithColorAndContext (
89
+ preEmitDiagnostics
90
+ . filter ( ( diag ) => diag . category === ts . DiagnosticCategory . Error )
91
+ . slice ( 0 , 10 ) ,
92
+ host ,
93
+ ) ,
94
+ ) ;
95
+ throw new Error ( `TypeScript compiler emitted pre-emit errors!` ) ;
96
+ }
97
+
98
+ const emitResult = program . emit ( ) ;
99
+ if (
100
+ emitResult . diagnostics . some (
101
+ ( diag ) => diag . category === ts . DiagnosticCategory . Error ,
102
+ )
103
+ ) {
104
+ console . error (
105
+ ts . formatDiagnosticsWithColorAndContext (
106
+ emitResult . diagnostics . filter (
107
+ ( diag ) => diag . category === ts . DiagnosticCategory . Error ,
108
+ ) ,
109
+ host ,
110
+ ) ,
111
+ ) ;
112
+ throw new Error ( `TypeScript compiler emitted errors!` ) ;
113
+ }
114
+ } ) ,
115
+ )
116
+ . teardown ( ( { workingDir, sourceDir } ) => {
117
+ fs . removeSync ( workingDir ) ;
118
+ fs . removeSync ( sourceDir ) ;
119
+ } ) ,
120
+
121
+ // Always run against the same version of CDK source
122
+ new Benchmark ( `Compile aws-cdk-lib@${ cdkTagv2_21_1 } ` )
123
+ . setup ( async ( ) => {
124
+ const sourceDir = fs . mkdtempSync (
125
+ path . join ( os . tmpdir ( ) , 'jsii-cdk-bench-snapshot' ) ,
126
+ ) ;
127
+ await streamUntar ( cdkv2_21_1 , { cwd : sourceDir } ) ;
128
+ cp . execSync ( `${ npm } ci` , { cwd : sourceDir } ) ;
129
+
130
+ // Working directory for benchmark
131
+ const workingDir = fs . mkdtempSync (
132
+ path . join ( os . tmpdir ( ) , `jsii-cdk-bench@${ cdkTagv2_21_1 } ` ) ,
133
+ ) ;
134
+
135
+ return {
136
+ workingDir,
137
+ sourceDir,
138
+ } as const ;
139
+ } )
140
+ . beforeEach ( ( { workingDir, sourceDir } ) => {
141
+ fs . removeSync ( workingDir ) ;
142
+ fs . copySync ( sourceDir , workingDir ) ;
143
+ } )
144
+ . subject ( ( { workingDir } ) =>
145
+ inDirectory ( workingDir , ( ) => {
146
+ const { projectInfo } = loadProjectInfo ( workingDir ) ;
147
+ const compiler = new Compiler ( { projectInfo } ) ;
148
+
149
+ const result = compiler . emit ( ) ;
150
+ if (
151
+ result . diagnostics . some (
152
+ ( diag ) => diag . category === ts . DiagnosticCategory . Error ,
153
+ )
154
+ ) {
155
+ console . error (
156
+ ts . formatDiagnosticsWithColorAndContext (
157
+ result . diagnostics . filter (
158
+ ( diag ) => diag . category === ts . DiagnosticCategory . Error ,
159
+ ) ,
160
+ {
161
+ getCurrentDirectory : ( ) => workingDir ,
162
+ getCanonicalFileName : path . resolve ,
163
+ getNewLine : ( ) => ts . sys . newLine ,
164
+ } ,
165
+ ) ,
166
+ ) ;
167
+ throw new Error ( `jsii compiler emitted errors!` ) ;
168
+ }
169
+ } ) ,
170
+ )
171
+ . teardown ( ( { workingDir, sourceDir } ) => {
172
+ fs . removeSync ( workingDir ) ;
173
+ fs . removeSync ( sourceDir ) ;
174
+ } ) ,
175
+ ] ;
0 commit comments