@@ -22,6 +22,7 @@ import 'package:_fe_analyzer_shared/src/macros/bootstrap.dart';
2222import 'package:_fe_analyzer_shared/src/macros/executor.dart' ;
2323import 'package:_fe_analyzer_shared/src/macros/executor_shared/introspection_impls.dart' ;
2424import 'package:_fe_analyzer_shared/src/macros/executor_shared/remote_instance.dart' ;
25+ import 'package:_fe_analyzer_shared/src/macros/executor_shared/serialization.dart' ;
2526import 'package:_fe_analyzer_shared/src/macros/isolated_executor/isolated_executor.dart'
2627 as isolatedExecutor;
2728
@@ -30,6 +31,9 @@ void _log(String message) {
3031 print ('${_watch .elapsed }: $message ' );
3132}
3233
34+ var clientSerializationMode = SerializationMode .jsonClient;
35+ var serverSerializationMode = SerializationMode .jsonServer;
36+
3337// Run this script to print out the generated augmentation library for an example class.
3438void main () async {
3539 _log ('Preparing to run macros.' );
@@ -39,7 +43,7 @@ void main() async {
3943 print ('This script must be ran from the `macros` directory.' );
4044 exit (1 );
4145 }
42- var executor = await isolatedExecutor.start ();
46+ var executor = await isolatedExecutor.start (serverSerializationMode );
4347 var tmpDir = Directory .systemTemp.createTempSync ('data_class_macro_example' );
4448 try {
4549 var macroUri = thisFile.absolute.uri;
@@ -49,7 +53,7 @@ void main() async {
4953 macroUri.toString (): {
5054 macroName: ['' ],
5155 }
52- });
56+ }, clientSerializationMode );
5357
5458 var bootstrapFile = File (tmpDir.uri.resolve ('main.dart' ).toFilePath ())
5559 ..writeAsStringSync (bootstrapContent);
@@ -73,12 +77,16 @@ void main() async {
7377 _log ('Loading DataClass macro' );
7478 var clazzId = await executor.loadMacro (macroUri, macroName,
7579 precompiledKernelUri: kernelOutputFile.uri);
80+ _log ('Instantiating macro' );
7681 var instanceId =
7782 await executor.instantiateMacro (clazzId, '' , Arguments ([], {}));
7883
7984 _log ('Running DataClass macro 100 times...' );
8085 var results = < MacroExecutionResult > [];
81- for (var i = 1 ; i < 101 ; i++ ) {
86+ var macroExecutionStart = _watch.elapsed;
87+ late Duration firstRunEnd;
88+ late Duration first11RunsEnd;
89+ for (var i = 1 ; i <= 111 ; i++ ) {
8290 var _shouldLog = i == 1 || i == 10 || i == 100 ;
8391 if (_shouldLog) _log ('Running DataClass macro for the ${i }th time' );
8492 if (instanceId.shouldExecute (DeclarationKind .clazz, Phase .types)) {
@@ -103,17 +111,34 @@ void main() async {
103111 if (i == 1 ) results.add (result);
104112 }
105113 if (_shouldLog) _log ('Done running DataClass macro for the ${i }th time.' );
114+
115+ if (i == 1 ) {
116+ firstRunEnd = _watch.elapsed;
117+ } else if (i == 11 ) {
118+ first11RunsEnd = _watch.elapsed;
119+ }
106120 }
121+ var first111RunsEnd = _watch.elapsed;
107122
108123 _log ('Building augmentation library' );
109124 var library = executor.buildAugmentationLibrary (results, (identifier) {
110125 if (identifier == boolIdentifier ||
111126 identifier == objectIdentifier ||
112127 identifier == stringIdentifier ||
113128 identifier == intIdentifier) {
114- return Uri (scheme: 'dart' , path: 'core' );
129+ return ResolvedIdentifier (
130+ kind: IdentifierKind .topLevelMember,
131+ name: identifier.name,
132+ staticScope: null ,
133+ uri: null );
115134 } else {
116- return File ('example/data_class.dart' ).absolute.uri;
135+ return ResolvedIdentifier (
136+ kind: identifier.name == 'MyClass'
137+ ? IdentifierKind .topLevelMember
138+ : IdentifierKind .instanceMember,
139+ name: identifier.name,
140+ staticScope: null ,
141+ uri: Platform .script.resolve ('data_class.dart' ));
117142 }
118143 });
119144 executor.close ();
@@ -125,6 +150,11 @@ void main() async {
125150 .replaceAll ('/*augment*/' , 'augment' );
126151
127152 _log ('Macro augmentation library:\n\n $formatted ' );
153+ _log ('Time for the first run: ${macroExecutionStart - firstRunEnd }' );
154+ _log ('Average time for the next 10 runs: '
155+ '${(first11RunsEnd - firstRunEnd ).dividedBy (10 )}' );
156+ _log ('Average time for the next 100 runs: '
157+ '${(first111RunsEnd - first11RunsEnd ).dividedBy (100 )}' );
128158 } finally {
129159 tmpDir.deleteSync (recursive: true );
130160 }
@@ -136,7 +166,7 @@ final intIdentifier = IdentifierImpl(id: RemoteInstance.uniqueId, name: 'int');
136166final objectIdentifier =
137167 IdentifierImpl (id: RemoteInstance .uniqueId, name: 'Object' );
138168final stringIdentifier =
139- IdentifierImpl (id: RemoteInstance .uniqueId, name: 'bool ' );
169+ IdentifierImpl (id: RemoteInstance .uniqueId, name: 'String ' );
140170
141171final boolType = NamedTypeAnnotationImpl (
142172 id: RemoteInstance .uniqueId,
@@ -189,6 +219,7 @@ final myClassFields = [
189219 isExternal: false ,
190220 isFinal: true ,
191221 isLate: false ,
222+ isStatic: false ,
192223 type: stringType),
193224 FieldDeclarationImpl (
194225 definingClass: myClassIdentifier,
@@ -197,6 +228,7 @@ final myClassFields = [
197228 isExternal: false ,
198229 isFinal: true ,
199230 isLate: false ,
231+ isStatic: false ,
200232 type: boolType),
201233];
202234
@@ -210,6 +242,7 @@ final myClassMethods = [
210242 isGetter: false ,
211243 isOperator: true ,
212244 isSetter: false ,
245+ isStatic: false ,
213246 namedParameters: [],
214247 positionalParameters: [
215248 ParameterDeclarationImpl (
@@ -236,6 +269,7 @@ final myClassMethods = [
236269 isOperator: false ,
237270 isGetter: true ,
238271 isSetter: false ,
272+ isStatic: false ,
239273 namedParameters: [],
240274 positionalParameters: [],
241275 returnType: intType,
@@ -250,6 +284,7 @@ final myClassMethods = [
250284 isGetter: false ,
251285 isOperator: false ,
252286 isSetter: false ,
287+ isStatic: false ,
253288 namedParameters: [],
254289 positionalParameters: [],
255290 returnType: stringType,
@@ -289,3 +324,8 @@ class FakeTypeDeclarationResolver extends Fake
289324 implements TypeDeclarationResolver {}
290325
291326class FakeTypeResolver extends Fake implements TypeResolver {}
327+
328+ extension _ on Duration {
329+ Duration dividedBy (int amount) =>
330+ Duration (microseconds: (this .inMicroseconds / amount).round ());
331+ }
0 commit comments