@@ -11,23 +11,23 @@ trait TheBencher {
11
11
12
12
const ID : & ' static str ;
13
13
14
- fn run ( source : & str ) -> Self :: RunOutput ;
14
+ fn run ( path : & Path , source : & str ) -> Self :: RunOutput ;
15
15
16
- fn bench ( g : & mut BenchmarkGroup < ' _ , WallTime > , source : & str ) {
16
+ fn bench ( g : & mut BenchmarkGroup < ' _ , WallTime > , path : & Path , source : & str ) {
17
17
let cpus = num_cpus:: get_physical ( ) ;
18
18
let id = BenchmarkId :: new ( Self :: ID , "single-thread" ) ;
19
- g. bench_with_input ( id, & source, |b, source| b. iter ( || Self :: run ( source) ) ) ;
19
+ g. bench_with_input ( id, & source, |b, source| b. iter ( || Self :: run ( path , source) ) ) ;
20
20
21
21
let id = BenchmarkId :: new ( Self :: ID , "no-drop" ) ;
22
22
g. bench_with_input ( id, & source, |b, source| {
23
- b. iter_with_large_drop ( || Self :: run ( source) )
23
+ b. iter_with_large_drop ( || Self :: run ( path , source) )
24
24
} ) ;
25
25
26
26
let id = BenchmarkId :: new ( Self :: ID , "parallel" ) ;
27
27
g. bench_with_input ( id, & source, |b, source| {
28
28
b. iter ( || {
29
29
( 0 ..cpus) . into_par_iter ( ) . for_each ( |_| {
30
- Self :: run ( source) ;
30
+ Self :: run ( path , source) ;
31
31
} ) ;
32
32
} )
33
33
} ) ;
@@ -41,7 +41,7 @@ impl TheBencher for OxcBencher {
41
41
42
42
const ID : & ' static str = "oxc" ;
43
43
44
- fn run ( source_text : & str ) -> Self :: RunOutput {
44
+ fn run ( path : & Path , source_text : & str ) -> Self :: RunOutput {
45
45
use oxc:: {
46
46
allocator:: Allocator ,
47
47
codegen:: { Codegen , CodegenOptions } ,
@@ -51,7 +51,7 @@ impl TheBencher for OxcBencher {
51
51
} ;
52
52
53
53
let allocator = Allocator :: default ( ) ;
54
- let source_type = SourceType :: default ( ) ;
54
+ let source_type = SourceType :: from_path ( path ) . unwrap ( ) ;
55
55
{
56
56
let ret = Parser :: new ( & allocator, source_text, source_type) . parse ( ) ;
57
57
let trivias = ret. trivias ;
@@ -86,22 +86,30 @@ impl TheBencher for SwcBencher {
86
86
87
87
const ID : & ' static str = "swc" ;
88
88
89
- fn run ( source : & str ) -> Self :: RunOutput {
89
+ fn run ( path : & Path , source : & str ) -> Self :: RunOutput {
90
90
use std:: sync:: Arc ;
91
91
use swc:: { Compiler , PrintArgs , SwcComments } ;
92
92
use swc_common:: { chain, source_map:: SourceMap , sync:: Lrc , Mark , GLOBALS } ;
93
- use swc_ecma_parser:: { Parser , StringInput , Syntax } ;
93
+ use swc_ecma_parser:: { EsConfig , Parser , StringInput , Syntax , TsConfig } ;
94
94
use swc_ecma_transforms_react:: { react, Options } ;
95
95
use swc_ecma_transforms_typescript:: strip;
96
96
use swc_ecma_visit:: FoldWith ;
97
97
98
98
let cm = Lrc :: new ( SourceMap :: new ( swc_common:: FilePathMapping :: empty ( ) ) ) ;
99
99
let compiler = Compiler :: new ( Arc :: clone ( & cm) ) ;
100
100
let comments = SwcComments :: default ( ) ;
101
+ let syntax = match path. extension ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) {
102
+ "js" => Syntax :: Es ( EsConfig :: default ( ) ) ,
103
+ "tsx" => Syntax :: Typescript ( TsConfig {
104
+ tsx : true ,
105
+ ..TsConfig :: default ( )
106
+ } ) ,
107
+ _ => panic ! ( "need to define syntax for swc" ) ,
108
+ } ;
101
109
102
110
GLOBALS . set ( & Default :: default ( ) , || {
103
111
let program = Parser :: new (
104
- Syntax :: Es ( Default :: default ( ) ) ,
112
+ syntax ,
105
113
StringInput :: new ( source, Default :: default ( ) , Default :: default ( ) ) ,
106
114
Some ( & comments) ,
107
115
)
@@ -132,13 +140,15 @@ impl TheBencher for SwcBencher {
132
140
}
133
141
134
142
fn transformer_benchmark ( c : & mut Criterion ) {
135
- let filename = "typescript.js" ;
136
- let source = std:: fs:: read_to_string ( filename) . unwrap ( ) ;
137
-
138
- let mut g = c. benchmark_group ( filename) ;
139
- OxcBencher :: bench ( & mut g, & source) ;
140
- SwcBencher :: bench ( & mut g, & source) ;
141
- g. finish ( ) ;
143
+ let filenames = [ "typescript.js" , "cal.com.tsx" ] ;
144
+ for filename in filenames {
145
+ let path = Path :: new ( "files" ) . join ( filename) ;
146
+ let source = std:: fs:: read_to_string ( & path) . unwrap ( ) ;
147
+ let mut g = c. benchmark_group ( filename) ;
148
+ OxcBencher :: bench ( & mut g, & path, & source) ;
149
+ SwcBencher :: bench ( & mut g, & path, & source) ;
150
+ g. finish ( ) ;
151
+ }
142
152
}
143
153
144
154
criterion_group ! ( transformer, transformer_benchmark) ;
0 commit comments