|
| 1 | +import 'package:_fe_analyzer_shared/src/macros/api.dart'; |
| 2 | +import 'package:_fe_analyzer_shared/src/macros/executor/introspection_impls.dart'; |
| 3 | +import 'package:_fe_analyzer_shared/src/macros/executor/remote_instance.dart'; |
| 4 | +import 'package:_fe_analyzer_shared/src/macros/executor.dart'; |
| 5 | +import 'package:benchmark_harness/benchmark_harness.dart'; |
| 6 | + |
| 7 | +import 'checks_extensions.dart'; |
| 8 | +import 'shared.dart'; |
| 9 | + |
| 10 | +Future<void> runBenchmarks(MacroExecutor executor, Uri macroUri) async { |
| 11 | + final introspector = SimpleDefinitionPhaseIntrospector(declarations: { |
| 12 | + myClass.identifier: myClass, |
| 13 | + objectClass.identifier: objectClass, |
| 14 | + boolClass.identifier: boolClass, |
| 15 | + intClass.identifier: intClass, |
| 16 | + stringClass.identifier: stringClass, |
| 17 | + }, identifiers: { |
| 18 | + Uri.parse('dart:core'): { |
| 19 | + 'bool': boolIdentifier, |
| 20 | + 'int': intIdentifier, |
| 21 | + 'dynamic': dynamicIdentifeir, |
| 22 | + 'String': stringIdentifier, |
| 23 | + 'Map': mapIdentifier, |
| 24 | + 'Object': objectIdentifier, |
| 25 | + } |
| 26 | + }, constructors: {}, enumValues: {}, fields: { |
| 27 | + myClass: myClassFields |
| 28 | + }, methods: {}); |
| 29 | + final identifierDeclarations = { |
| 30 | + ...introspector.declarations, |
| 31 | + for (final constructors in introspector.constructors.values) |
| 32 | + for (final constructor in constructors) |
| 33 | + constructor.identifier: constructor, |
| 34 | + for (final methods in introspector.methods.values) |
| 35 | + for (final method in methods) method.identifier: method, |
| 36 | + for (final fields in introspector.fields.values) |
| 37 | + for (final field in fields) field.identifier: field, |
| 38 | + }; |
| 39 | + final instantiateBenchmark = |
| 40 | + JsonSerializableInstantiateBenchmark(executor, macroUri); |
| 41 | + await instantiateBenchmark.report(); |
| 42 | + final instanceId = instantiateBenchmark.instanceIdentifier; |
| 43 | + final typesBenchmark = JsonSerializableTypesPhaseBenchmark( |
| 44 | + executor, macroUri, instanceId, introspector); |
| 45 | + await typesBenchmark.report(); |
| 46 | + BuildAugmentationLibraryBenchmark.reportAndPrint( |
| 47 | + executor, |
| 48 | + [if (typesBenchmark.result != null) typesBenchmark.result!], |
| 49 | + identifierDeclarations); |
| 50 | + final declarationsBenchmark = JsonSerializableDeclarationsPhaseBenchmark( |
| 51 | + executor, macroUri, instanceId, introspector); |
| 52 | + await declarationsBenchmark.report(); |
| 53 | + BuildAugmentationLibraryBenchmark.reportAndPrint( |
| 54 | + executor, |
| 55 | + [if (declarationsBenchmark.result != null) declarationsBenchmark.result!], |
| 56 | + identifierDeclarations); |
| 57 | + introspector.constructors[myClass] = myClassConstructors; |
| 58 | + final definitionsBenchmark = JsonSerializableDefinitionPhaseBenchmark( |
| 59 | + executor, macroUri, instanceId, introspector); |
| 60 | + await definitionsBenchmark.report(); |
| 61 | + BuildAugmentationLibraryBenchmark.reportAndPrint( |
| 62 | + executor, |
| 63 | + [if (definitionsBenchmark.result != null) definitionsBenchmark.result!], |
| 64 | + identifierDeclarations); |
| 65 | +} |
| 66 | + |
| 67 | +class JsonSerializableInstantiateBenchmark extends AsyncBenchmarkBase { |
| 68 | + final MacroExecutor executor; |
| 69 | + final Uri macroUri; |
| 70 | + late MacroInstanceIdentifier instanceIdentifier; |
| 71 | + |
| 72 | + JsonSerializableInstantiateBenchmark(this.executor, this.macroUri) |
| 73 | + : super('JsonSerializableInstantiate'); |
| 74 | + |
| 75 | + Future<void> run() async { |
| 76 | + instanceIdentifier = await executor.instantiateMacro( |
| 77 | + macroUri, 'JsonSerializable', '', Arguments([], {})); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +class JsonSerializableTypesPhaseBenchmark extends AsyncBenchmarkBase { |
| 82 | + final MacroExecutor executor; |
| 83 | + final Uri macroUri; |
| 84 | + final MacroInstanceIdentifier instanceIdentifier; |
| 85 | + final TypePhaseIntrospector introspector; |
| 86 | + MacroExecutionResult? result; |
| 87 | + |
| 88 | + JsonSerializableTypesPhaseBenchmark( |
| 89 | + this.executor, this.macroUri, this.instanceIdentifier, this.introspector) |
| 90 | + : super('JsonSerializableTypesPhase'); |
| 91 | + |
| 92 | + Future<void> run() async { |
| 93 | + if (instanceIdentifier.shouldExecute( |
| 94 | + DeclarationKind.classType, Phase.types)) { |
| 95 | + result = await executor.executeTypesPhase( |
| 96 | + instanceIdentifier, myClass, introspector); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +class JsonSerializableDeclarationsPhaseBenchmark extends AsyncBenchmarkBase { |
| 102 | + final MacroExecutor executor; |
| 103 | + final Uri macroUri; |
| 104 | + final MacroInstanceIdentifier instanceIdentifier; |
| 105 | + final DeclarationPhaseIntrospector introspector; |
| 106 | + |
| 107 | + MacroExecutionResult? result; |
| 108 | + |
| 109 | + JsonSerializableDeclarationsPhaseBenchmark( |
| 110 | + this.executor, this.macroUri, this.instanceIdentifier, this.introspector) |
| 111 | + : super('JsonSerializableDeclarationsPhase'); |
| 112 | + |
| 113 | + Future<void> run() async { |
| 114 | + result = null; |
| 115 | + if (instanceIdentifier.shouldExecute( |
| 116 | + DeclarationKind.classType, Phase.declarations)) { |
| 117 | + result = await executor.executeDeclarationsPhase( |
| 118 | + instanceIdentifier, myClass, introspector); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +class JsonSerializableDefinitionPhaseBenchmark extends AsyncBenchmarkBase { |
| 124 | + final MacroExecutor executor; |
| 125 | + final Uri macroUri; |
| 126 | + final MacroInstanceIdentifier instanceIdentifier; |
| 127 | + final DefinitionPhaseIntrospector introspector; |
| 128 | + |
| 129 | + MacroExecutionResult? result; |
| 130 | + |
| 131 | + JsonSerializableDefinitionPhaseBenchmark( |
| 132 | + this.executor, this.macroUri, this.instanceIdentifier, this.introspector) |
| 133 | + : super('JsonSerializableDefinitionPhase'); |
| 134 | + |
| 135 | + Future<void> run() async { |
| 136 | + result = null; |
| 137 | + if (instanceIdentifier.shouldExecute( |
| 138 | + DeclarationKind.classType, Phase.definitions)) { |
| 139 | + result = await executor.executeDefinitionsPhase( |
| 140 | + instanceIdentifier, myClass, introspector); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +final myClassIdentifier = |
| 146 | + IdentifierImpl(id: RemoteInstance.uniqueId, name: 'MyClass'); |
| 147 | +final myClass = ClassDeclarationImpl( |
| 148 | + id: RemoteInstance.uniqueId, |
| 149 | + identifier: myClassIdentifier, |
| 150 | + library: fooLibrary, |
| 151 | + metadata: [], |
| 152 | + interfaces: [], |
| 153 | + hasAbstract: false, |
| 154 | + hasBase: false, |
| 155 | + hasExternal: false, |
| 156 | + hasFinal: false, |
| 157 | + hasInterface: false, |
| 158 | + hasMixin: false, |
| 159 | + hasSealed: false, |
| 160 | + mixins: [], |
| 161 | + superclass: NamedTypeAnnotationImpl( |
| 162 | + id: RemoteInstance.uniqueId, |
| 163 | + isNullable: false, |
| 164 | + identifier: objectIdentifier, |
| 165 | + typeArguments: [], |
| 166 | + ), |
| 167 | + typeParameters: []); |
| 168 | + |
| 169 | +final myClassFields = [ |
| 170 | + FieldDeclarationImpl( |
| 171 | + definingType: myClassIdentifier, |
| 172 | + id: RemoteInstance.uniqueId, |
| 173 | + identifier: IdentifierImpl(id: RemoteInstance.uniqueId, name: 'myString'), |
| 174 | + library: fooLibrary, |
| 175 | + metadata: [], |
| 176 | + hasAbstract: false, |
| 177 | + hasExternal: false, |
| 178 | + hasFinal: true, |
| 179 | + hasLate: false, |
| 180 | + isStatic: false, |
| 181 | + type: stringType), |
| 182 | + FieldDeclarationImpl( |
| 183 | + definingType: myClassIdentifier, |
| 184 | + id: RemoteInstance.uniqueId, |
| 185 | + identifier: IdentifierImpl(id: RemoteInstance.uniqueId, name: 'myBool'), |
| 186 | + library: fooLibrary, |
| 187 | + metadata: [], |
| 188 | + hasAbstract: false, |
| 189 | + hasExternal: false, |
| 190 | + hasFinal: true, |
| 191 | + hasLate: false, |
| 192 | + isStatic: false, |
| 193 | + type: boolType), |
| 194 | +]; |
| 195 | + |
| 196 | +final myClassConstructors = [ |
| 197 | + ConstructorDeclarationImpl( |
| 198 | + id: RemoteInstance.uniqueId, |
| 199 | + identifier: IdentifierImpl(id: RemoteInstance.uniqueId, name: 'fromJson'), |
| 200 | + library: fooLibrary, |
| 201 | + metadata: [], |
| 202 | + hasBody: false, |
| 203 | + hasExternal: false, |
| 204 | + namedParameters: [], |
| 205 | + positionalParameters: [ |
| 206 | + ParameterDeclarationImpl( |
| 207 | + id: RemoteInstance.uniqueId, |
| 208 | + identifier: |
| 209 | + IdentifierImpl(id: RemoteInstance.uniqueId, name: 'json'), |
| 210 | + library: fooLibrary, |
| 211 | + metadata: [], |
| 212 | + isNamed: false, |
| 213 | + isRequired: true, |
| 214 | + type: NamedTypeAnnotationImpl( |
| 215 | + id: RemoteInstance.uniqueId, |
| 216 | + isNullable: false, |
| 217 | + identifier: mapIdentifier, |
| 218 | + typeArguments: [stringType, dynamicType])) |
| 219 | + ], |
| 220 | + returnType: myClassType, |
| 221 | + typeParameters: [], |
| 222 | + definingType: myClass.identifier, |
| 223 | + isFactory: true), |
| 224 | +]; |
0 commit comments