|
| 1 | +import 'package:_fe_analyzer_shared/src/macros/api.dart'; |
| 2 | +import 'package:_fe_analyzer_shared/src/macros/executor.dart'; |
| 3 | +import 'package:_fe_analyzer_shared/src/macros/executor/introspection_impls.dart'; |
| 4 | +import 'package:_fe_analyzer_shared/src/macros/executor/remote_instance.dart'; |
| 5 | +import 'package:benchmark_harness/benchmark_harness.dart'; |
| 6 | + |
| 7 | +import 'shared.dart'; |
| 8 | + |
| 9 | +Future<void> runBenchmarks(MacroExecutor executor, Uri macroUri) async { |
| 10 | + final identifierResolver = SimpleIdentifierResolver({ |
| 11 | + Uri.parse('dart:core'): { |
| 12 | + 'int': intIdentifier, |
| 13 | + 'String': stringIdentifier, |
| 14 | + }, |
| 15 | + Uri.parse('package:flutter/flutter.dart'): { |
| 16 | + 'BuildContext': buildContextIdentifier, |
| 17 | + 'Widget': widgetIdentifier, |
| 18 | + } |
| 19 | + }); |
| 20 | + final identifierDeclarations = <Identifier, Declaration>{}; |
| 21 | + final instantiateBenchmark = |
| 22 | + FunctionalWidgetInstantiateBenchmark(executor, macroUri); |
| 23 | + await instantiateBenchmark.report(); |
| 24 | + final instanceId = instantiateBenchmark.instanceIdentifier; |
| 25 | + final typesBenchmark = FunctionalWidgetTypesPhaseBenchmark( |
| 26 | + executor, macroUri, identifierResolver, instanceId); |
| 27 | + await typesBenchmark.report(); |
| 28 | + BuildAugmentationLibraryBenchmark.reportAndPrint( |
| 29 | + executor, |
| 30 | + [if (typesBenchmark.result != null) typesBenchmark.result!], |
| 31 | + identifierDeclarations); |
| 32 | +} |
| 33 | + |
| 34 | +class FunctionalWidgetInstantiateBenchmark extends AsyncBenchmarkBase { |
| 35 | + final MacroExecutor executor; |
| 36 | + final Uri macroUri; |
| 37 | + late MacroInstanceIdentifier instanceIdentifier; |
| 38 | + |
| 39 | + FunctionalWidgetInstantiateBenchmark(this.executor, this.macroUri) |
| 40 | + : super('FunctionalWidgetInstantiate'); |
| 41 | + |
| 42 | + Future<void> run() async { |
| 43 | + instanceIdentifier = await executor.instantiateMacro( |
| 44 | + macroUri, 'FunctionalWidget', '', Arguments([], {})); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +class FunctionalWidgetTypesPhaseBenchmark extends AsyncBenchmarkBase { |
| 49 | + final MacroExecutor executor; |
| 50 | + final Uri macroUri; |
| 51 | + final IdentifierResolver identifierResolver; |
| 52 | + final MacroInstanceIdentifier instanceIdentifier; |
| 53 | + MacroExecutionResult? result; |
| 54 | + |
| 55 | + FunctionalWidgetTypesPhaseBenchmark(this.executor, this.macroUri, |
| 56 | + this.identifierResolver, this.instanceIdentifier) |
| 57 | + : super('FunctionalWidgetTypesPhase'); |
| 58 | + |
| 59 | + Future<void> run() async { |
| 60 | + if (instanceIdentifier.shouldExecute( |
| 61 | + DeclarationKind.function, Phase.types)) { |
| 62 | + result = await executor.executeTypesPhase( |
| 63 | + instanceIdentifier, myFunction, identifierResolver); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +final buildContextIdentifier = |
| 69 | + IdentifierImpl(id: RemoteInstance.uniqueId, name: 'BuildContext'); |
| 70 | +final buildContextType = NamedTypeAnnotationImpl( |
| 71 | + id: RemoteInstance.uniqueId, |
| 72 | + isNullable: false, |
| 73 | + identifier: buildContextIdentifier, |
| 74 | + typeArguments: []); |
| 75 | +final widgetIdentifier = |
| 76 | + IdentifierImpl(id: RemoteInstance.uniqueId, name: 'Widget'); |
| 77 | +final widgetType = NamedTypeAnnotationImpl( |
| 78 | + id: RemoteInstance.uniqueId, |
| 79 | + isNullable: false, |
| 80 | + identifier: widgetIdentifier, |
| 81 | + typeArguments: []); |
| 82 | +final myFunction = FunctionDeclarationImpl( |
| 83 | + id: RemoteInstance.uniqueId, |
| 84 | + identifier: IdentifierImpl(id: RemoteInstance.uniqueId, name: '_myWidget'), |
| 85 | + library: fooLibrary, |
| 86 | + isAbstract: false, |
| 87 | + isExternal: false, |
| 88 | + isGetter: false, |
| 89 | + isOperator: false, |
| 90 | + isSetter: false, |
| 91 | + namedParameters: [ |
| 92 | + ParameterDeclarationImpl( |
| 93 | + id: RemoteInstance.uniqueId, |
| 94 | + identifier: |
| 95 | + IdentifierImpl(id: RemoteInstance.uniqueId, name: 'title'), |
| 96 | + isNamed: true, |
| 97 | + isRequired: true, |
| 98 | + library: fooLibrary, |
| 99 | + type: stringType), |
| 100 | + ], |
| 101 | + positionalParameters: [ |
| 102 | + ParameterDeclarationImpl( |
| 103 | + id: RemoteInstance.uniqueId, |
| 104 | + identifier: |
| 105 | + IdentifierImpl(id: RemoteInstance.uniqueId, name: 'context'), |
| 106 | + isNamed: false, |
| 107 | + isRequired: true, |
| 108 | + library: fooLibrary, |
| 109 | + type: buildContextType), |
| 110 | + ParameterDeclarationImpl( |
| 111 | + id: RemoteInstance.uniqueId, |
| 112 | + identifier: |
| 113 | + IdentifierImpl(id: RemoteInstance.uniqueId, name: 'count'), |
| 114 | + isNamed: false, |
| 115 | + isRequired: true, |
| 116 | + library: fooLibrary, |
| 117 | + type: intType), |
| 118 | + ], |
| 119 | + returnType: widgetType, |
| 120 | + typeParameters: []); |
0 commit comments