1
1
// There is no public API exposed yet, the in progress API lives here.
2
2
import 'package:_fe_analyzer_shared/src/macros/api.dart' ;
3
+ import 'package:_fe_analyzer_shared/src/macros/code_optimizer.dart' ;
3
4
import 'package:_fe_analyzer_shared/src/macros/executor/introspection_impls.dart' ;
4
5
import 'package:_fe_analyzer_shared/src/macros/executor/remote_instance.dart' ;
5
6
import 'package:_fe_analyzer_shared/src/macros/executor.dart' ;
7
+ import 'package:_fe_analyzer_shared/src/scanner/scanner.dart' ;
6
8
import 'package:benchmark_harness/benchmark_harness.dart' ;
7
9
import 'package:dart_style/dart_style.dart' ;
8
10
9
- class BuildAugmentationLibraryBenchmark extends BenchmarkBase {
11
+ /// A benchmark which only calls `run` once inside `excersize` .
12
+ class RunOnceBenchmarkBase extends BenchmarkBase {
13
+ RunOnceBenchmarkBase (super .name);
14
+
15
+ @override
16
+ void exercise () {
17
+ run ();
18
+ }
19
+ }
20
+
21
+ class BuildAugmentationLibraryBenchmark extends RunOnceBenchmarkBase {
10
22
final MacroExecutor executor;
11
23
final List <MacroExecutionResult > results;
12
24
@@ -18,7 +30,7 @@ class BuildAugmentationLibraryBenchmark extends BenchmarkBase {
18
30
this .executor, this .results, this .identifierDeclarations)
19
31
: super ('AugmentationLibrary' );
20
32
21
- static void reportAndPrint (
33
+ static String reportAndPrint (
22
34
MacroExecutor executor,
23
35
List <MacroExecutionResult > results,
24
36
Map <Identifier , Declaration > identifierDeclarations,
@@ -31,8 +43,7 @@ class BuildAugmentationLibraryBenchmark extends BenchmarkBase {
31
43
final benchmark = BuildAugmentationLibraryBenchmark (
32
44
executor, results, identifierDeclarations);
33
45
benchmark.report ();
34
- final formatBenchmark = FormatLibraryBenchmark (benchmark.library)..report ();
35
- print ('${formatBenchmark .formattedResult }' );
46
+ return benchmark.library;
36
47
}
37
48
38
49
void run () {
@@ -48,7 +59,7 @@ class BuildAugmentationLibraryBenchmark extends BenchmarkBase {
48
59
kind: IdentifierKind .topLevelMember,
49
60
name: identifier.name,
50
61
staticScope: null ,
51
- uri: null );
62
+ uri: dartCore );
52
63
} else {
53
64
final declaration = identifierDeclarations[identifier];
54
65
String ? staticScope;
@@ -79,9 +90,11 @@ class BuildAugmentationLibraryBenchmark extends BenchmarkBase {
79
90
(annotation) =>
80
91
throw UnsupportedError ('Omitted types are not supported!' ));
81
92
}
93
+
94
+ static final dartCore = Uri .parse ('dart:core' );
82
95
}
83
96
84
- class FormatLibraryBenchmark extends BenchmarkBase {
97
+ class FormatLibraryBenchmark extends RunOnceBenchmarkBase {
85
98
final formatter = DartFormatter ();
86
99
final String library;
87
100
late String _formattedResult;
@@ -90,7 +103,7 @@ class FormatLibraryBenchmark extends BenchmarkBase {
90
103
.replaceAll ('/*augment*/' , 'augment' )
91
104
.replaceAll ('on FakeTypeForFormatting {' , '{' );
92
105
93
- FormatLibraryBenchmark (String library)
106
+ FormatLibraryBenchmark ._ (String library)
94
107
: library = _prepareLibrary (library),
95
108
super ('FormatLibrary' );
96
109
@@ -118,6 +131,51 @@ class FormatLibraryBenchmark extends BenchmarkBase {
118
131
void run () {
119
132
_formattedResult = formatter.format (library);
120
133
}
134
+
135
+ static String reportAndPrint (String library) {
136
+ final formatBenchmark = FormatLibraryBenchmark ._(library)..report ();
137
+ print ('${formatBenchmark .formattedResult }' );
138
+ return formatBenchmark.formattedResult;
139
+ }
140
+ }
141
+
142
+ class CodeOptimizerBenchmark extends RunOnceBenchmarkBase {
143
+ final String library;
144
+ final Set <String > libraryDeclarationNames;
145
+ final BenchmarkCodeOptimizer optimizer;
146
+ late String optimizedLibrary;
147
+
148
+ CodeOptimizerBenchmark (
149
+ this .library, this .libraryDeclarationNames, this .optimizer)
150
+ : super ('CodeOptimizer' );
151
+
152
+ void run () {
153
+ optimizedLibrary = Edit .applyList (
154
+ optimizer.optimize (library,
155
+ libraryDeclarationNames: libraryDeclarationNames,
156
+ scannerConfiguration: ScannerConfiguration (
157
+ enableExtensionMethods: true ,
158
+ enableNonNullable: true ,
159
+ enableTripleShift: true ,
160
+ forAugmentationLibrary: true )),
161
+ library);
162
+ }
163
+
164
+ void reportAndPrint () {
165
+ report ();
166
+ print (optimizedLibrary);
167
+ }
168
+ }
169
+
170
+ class BenchmarkCodeOptimizer extends CodeOptimizer {
171
+ final Map <Uri , Map <String , Identifier >> identifiers;
172
+
173
+ BenchmarkCodeOptimizer ({required this .identifiers});
174
+
175
+ @override
176
+ Set <String > getImportedNames (String uriStr) {
177
+ return (identifiers[Uri .parse (uriStr)] ?? {}).keys.toSet ();
178
+ }
121
179
}
122
180
123
181
abstract mixin class Fake {
0 commit comments