Skip to content

Commit 41d91a1

Browse files
authored
Add benchmarks for when there is mostly no codegen. (#4091)
1 parent d0ef60b commit 41d91a1

File tree

7 files changed

+91
-24
lines changed

7 files changed

+91
-24
lines changed

_benchmark/bin/_benchmark.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ final commandRunner =
2525
'build-repo-path',
2626
help: 'Path to build repo to benchmark.',
2727
)
28+
..argParser.addMultiOption(
29+
'dependency-override-path',
30+
help:
31+
'Set a dependency override in generated pubspec.yaml, for '
32+
'benchmarking local changes to builders. Example: '
33+
'--dependency-override=built_value_generator='
34+
'/path/to/local/package',
35+
)
2836
..argParser.addOption(
2937
'generator',
3038
help: 'Generator to benchmark.',
@@ -48,8 +56,8 @@ final commandRunner =
4856
allowed: Shape.values.map((e) => e.name).toList(),
4957
)
5058
..argParser.addFlag(
51-
'use-experimental-resolver',
52-
help: 'Whether to pass `--use-experimental-resolver` to build_runner.',
59+
'mostly-no-codegen',
60+
help: 'Whether to generate mostly source with no codegen.',
5361
);
5462

5563
Future<void> main(List<String> arguments) async {

_benchmark/lib/benchmarks/built_value_generator_benchmark.dart

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,20 @@ ${config.dependencyOverrides}
5656
libraryNumber,
5757
benchmarkSize: size,
5858
);
59-
workspace.write(
60-
'lib/$libraryName',
61-
source: '''
59+
if (config.config.mostlyNoCodegen && libraryNumber > 1) {
60+
workspace.write(
61+
'lib/$libraryName',
62+
source: '''
63+
// ignore_for_file: unused_import
64+
${[for (final importName in importNames) "import '$importName';"].join('\n')}
65+
66+
class Value {}
67+
''',
68+
);
69+
} else {
70+
workspace.write(
71+
'lib/$libraryName',
72+
source: '''
6273
// ignore_for_file: unused_import
6374
import 'package:built_value/built_value.dart';
6475
@@ -71,7 +82,8 @@ abstract class Value implements Built<Value, ValueBuilder> {
7182
factory Value(void Function(ValueBuilder) updates) = _\$Value;
7283
}
7384
''',
74-
);
85+
);
86+
}
7587
}
7688
}
7789
}

_benchmark/lib/benchmarks/freezed_generator_benchmark.dart

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,20 @@ ${config.dependencyOverrides}
6060
libraryNumber,
6161
benchmarkSize: size,
6262
);
63-
workspace.write(
64-
'lib/$libraryName',
65-
source: '''
63+
if (config.config.mostlyNoCodegen && libraryNumber > 1) {
64+
workspace.write(
65+
'lib/$libraryName',
66+
source: '''
67+
// ignore_for_file: unused_import
68+
${[for (final importName in importNames) "import '$importName';"].join('\n')}
69+
70+
class Value {}
71+
''',
72+
);
73+
} else {
74+
workspace.write(
75+
'lib/$libraryName',
76+
source: '''
6677
// ignore_for_file: unused_import
6778
import 'package:freezed_annotation/freezed_annotation.dart';
6879
@@ -75,7 +86,8 @@ class Value with _\$Value {
7586
const factory Value() = _Value;
7687
}
7788
''',
78-
);
89+
);
90+
}
7991
}
8092
}
8193
}

_benchmark/lib/benchmarks/json_serializable_generator_benchmark.dart

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,20 @@ ${config.dependencyOverrides}
5757
libraryNumber,
5858
benchmarkSize: size,
5959
);
60-
workspace.write(
61-
'lib/$libraryName',
62-
source: '''
60+
if (config.config.mostlyNoCodegen && libraryNumber > 1) {
61+
workspace.write(
62+
'lib/$libraryName',
63+
source: '''
64+
// ignore_for_file: unused_import
65+
${[for (final importName in importNames) "import '$importName';"].join('\n')}
66+
67+
class Value {}
68+
''',
69+
);
70+
} else {
71+
workspace.write(
72+
'lib/$libraryName',
73+
source: '''
6374
// ignore_for_file: unused_import
6475
import 'package:json_annotation/json_annotation.dart';
6576
@@ -75,7 +86,8 @@ class Value {
7586
Map<String, dynamic> toJson() => _\$ValueToJson(this);
7687
}
7788
''',
78-
);
89+
);
90+
}
7991
}
8092
}
8193
}

_benchmark/lib/benchmarks/mockito_generator_benchmark.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ class MockitoGeneratorBenchmark implements Benchmark {
1717

1818
@override
1919
void create(RunConfig config) {
20+
if (config.config.mostlyNoCodegen) {
21+
throw UnsupportedError(
22+
'Mockito benchmark does not support --mostly-no-codegen.',
23+
);
24+
}
2025
final workspace = config.workspace;
2126
final size = config.size;
2227

_benchmark/lib/config.dart

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,32 @@ import 'workspace.dart';
1313
/// Benchmark tool config.
1414
class Config {
1515
final String? buildRepoPath;
16+
final Map<String, String> dependencyOverridePaths;
1617
final Generator generator;
1718
final List<Shape> shapes;
1819
final Directory rootDirectory;
1920
final List<int> sizes;
20-
final bool useExperimentalResolver;
2121
final bool allowFailures;
22+
bool mostlyNoCodegen;
2223

2324
Config({
2425
required this.allowFailures,
2526
required this.buildRepoPath,
27+
required this.dependencyOverridePaths,
2628
required this.generator,
2729
required this.rootDirectory,
2830
required this.sizes,
2931
required this.shapes,
30-
required this.useExperimentalResolver,
32+
required this.mostlyNoCodegen,
3133
});
3234

3335
factory Config.fromArgResults(ArgResults argResults) => Config(
3436
allowFailures: argResults['allow-failures'] as bool,
3537
buildRepoPath: argResults['build-repo-path'] as String?,
38+
dependencyOverridePaths: {
39+
for (var s in argResults['dependency-override-path'] as List<String>)
40+
s.split('=')[0]: s.split('=')[1],
41+
},
3642
generator: Generator.values.singleWhere(
3743
(e) => e.packageName == argResults['generator'],
3844
),
@@ -45,7 +51,7 @@ class Config {
4551
argResults['shape'] == null
4652
? Shape.values
4753
: [Shape.values.singleWhere((e) => e.name == argResults['shape'])],
48-
useExperimentalResolver: argResults['use-experimental-resolver'] as bool,
54+
mostlyNoCodegen: argResults['mostly-no-codegen'] as bool,
4955
);
5056
}
5157

@@ -69,11 +75,11 @@ class RunConfig {
6975
});
7076

7177
String get dependencyOverrides {
72-
final buildRepoPath = config.buildRepoPath;
73-
if (buildRepoPath == null) return '';
78+
final overrides = StringBuffer();
7479

75-
return '''
76-
dependency_overrides:
80+
final buildRepoPath = config.buildRepoPath;
81+
if (buildRepoPath != null) {
82+
overrides.write('''
7783
build:
7884
path: $buildRepoPath/build
7985
build_config:
@@ -90,6 +96,21 @@ dependency_overrides:
9096
path: $buildRepoPath/build_test
9197
build_web_compilers:
9298
path: $buildRepoPath/build_web_compilers
99+
''');
100+
}
101+
102+
for (final entry in config.dependencyOverridePaths.entries) {
103+
overrides.write('''
104+
${entry.key}:
105+
path: ${entry.value}
106+
''');
107+
}
108+
109+
return overrides.isEmpty
110+
? ''
111+
: '''
112+
dependency_overrides:
113+
$overrides
93114
''';
94115
}
95116
}

_benchmark/lib/workspace.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ class Workspace {
6060
'build_runner',
6161
'build',
6262
'-d',
63-
if (config.useExperimentalResolver) '--use-experimental-resolver',
6463
], workingDirectory: directory.path);
6564
var exitCode = await process.exitCode;
6665
if (exitCode == 0) {
@@ -81,7 +80,6 @@ class Workspace {
8180
'build_runner',
8281
'build',
8382
'-d',
84-
if (config.useExperimentalResolver) '--use-experimental-resolver',
8583
], workingDirectory: directory.path);
8684
exitCode = await process.exitCode;
8785
if (exitCode == 0) {
@@ -101,7 +99,6 @@ class Workspace {
10199
'build_runner',
102100
'build',
103101
'-d',
104-
if (config.useExperimentalResolver) '--use-experimental-resolver',
105102
], workingDirectory: directory.path);
106103
exitCode = await process.exitCode;
107104
if (exitCode == 0) {

0 commit comments

Comments
 (0)