-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathgenerate.dart
112 lines (93 loc) · 2.68 KB
/
generate.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
//
// Generates both the dart and dart2 version of this benchmark.
import 'dart:io';
import 'dart:math';
import 'package:path/path.dart' as path;
const String benchmarkName = 'InstantiateTypeArgs';
const List<int> instantiateCounts = [1, 5, 10, 100, 1000];
void generateBenchmarkClassesAndUtilities(IOSink output, {required bool nnbd}) {
output.writeln('''
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
//
// This benchmark suite measures the overhead of instantiating type arguments,
// with a particular aim of measuring the overhead of the caching mechanism.
''');
if (!nnbd) {
output.writeln('''
// @dart=2.9"
''');
}
output.write('''
import 'package:benchmark_harness/benchmark_harness.dart';
void main() {
''');
for (final count in instantiateCounts) {
output.write('''
const Instantiate$count().report();
''');
}
output.writeln('''
}
''');
for (final count in instantiateCounts) {
output.write('''
class Instantiate$count extends BenchmarkBase {
const Instantiate$count() : super('$benchmarkName.Instantiate$count');
// Normalize the cost across the benchmarks by number of instantiations.
@override
void report() => emitter.emit(name, measure() / $count);
@override
void run() {
''');
for (int i = 0; i < count; i++) {
output.write('''
D.instantiate<C$i>();
''');
}
output.writeln('''
}
}
''');
}
output.write('''
@pragma('vm:never-inline')
@pragma('wasm:never-inline')
@pragma('dart2js:never-inline')
void blackhole<T>() => null;
class D<T> {
@pragma('vm:never-inline')
@pragma('wasm:never-inline')
@pragma('dart2js:never-inline')
static void instantiate<S>() => blackhole<D<S>>();
}
''');
final maxCount = instantiateCounts.reduce(max);
for (int i = 0; i < maxCount; i++) {
output.write('''
class C$i {}
''');
}
}
void main() {
final dartFilePath = path.join(
path.dirname(Platform.script.path),
'dart',
'$benchmarkName.dart',
);
final dartSink = File(dartFilePath).openWrite();
generateBenchmarkClassesAndUtilities(dartSink, nnbd: true);
dartSink..flush();
final dart2FilePath = path.join(
path.dirname(Platform.script.path),
'dart2',
'$benchmarkName.dart',
);
final dart2Sink = File(dart2FilePath).openWrite();
generateBenchmarkClassesAndUtilities(dart2Sink, nnbd: false);
dart2Sink..flush();
}