-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathIsolateSpawn.dart
181 lines (155 loc) · 5.02 KB
/
IsolateSpawn.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) 2019, 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.
import 'dart:async';
import 'dart:isolate';
import 'dart:math';
import '../../../pkg/vm/bin/gen_kernel.dart' as gen_kernel;
class SpawnLatency {
SpawnLatency(this.name);
Future<ResultMessageLatency> run() async {
final completerResult = Completer();
final receivePort = ReceivePort()..listen(completerResult.complete);
final isolateExitedCompleter = Completer<DateTime>();
final onExitReceivePort = ReceivePort()
..listen((_) {
isolateExitedCompleter.complete(DateTime.now());
});
final beforeSpawn = DateTime.now();
await Isolate.spawn(
isolateCompiler,
StartMessageLatency(receivePort.sendPort, beforeSpawn),
onExit: onExitReceivePort.sendPort,
onError: onExitReceivePort.sendPort,
);
final afterSpawn = DateTime.now();
final ResultMessageLatency result = await completerResult.future;
receivePort.close();
final DateTime isolateExited = await isolateExitedCompleter.future;
result.timeToExitUs = isolateExited.difference(beforeSpawn).inMicroseconds;
result.timeToIsolateSpawnUs = afterSpawn
.difference(beforeSpawn)
.inMicroseconds;
onExitReceivePort.close();
return result;
}
Future<AggregatedResultMessageLatency> measureFor(int minimumMillis) async {
final minimumMicros = minimumMillis * 1000;
final watch = Stopwatch()..start();
final Metric toAfterIsolateSpawnUs = LatencyMetric('${name}ToAfterSpawn');
final Metric toStartRunningCodeUs = LatencyMetric('${name}ToStartRunning');
final Metric toFinishRunningCodeUs = LatencyMetric(
'${name}ToFinishRunning',
);
final Metric toExitUs = LatencyMetric('${name}ToExit');
while (watch.elapsedMicroseconds < minimumMicros) {
final result = await run();
toAfterIsolateSpawnUs.add(result.timeToIsolateSpawnUs);
toStartRunningCodeUs.add(result.timeToStartRunningCodeUs);
toFinishRunningCodeUs.add(result.timeToFinishRunningCodeUs);
toExitUs.add(result.timeToExitUs);
}
return AggregatedResultMessageLatency(
toAfterIsolateSpawnUs,
toStartRunningCodeUs,
toFinishRunningCodeUs,
toExitUs,
);
}
Future<AggregatedResultMessageLatency> measure() async {
await measureFor(500); // warm-up
return measureFor(4000); // actual measurement
}
Future<void> report() async {
final result = await measure();
print(result);
}
final String name;
late RawReceivePort receivePort;
}
class Metric {
Metric({required this.prefix, required this.suffix});
void add(int value) {
if (value > max) {
max = value;
}
sum += value;
sumOfSquares += value * value;
count++;
}
double _average() => sum / count;
double _rms() => sqrt(sumOfSquares / count);
@override
String toString() =>
'$prefix): ${_average()}$suffix\n'
'${prefix}Max): $max$suffix\n'
'${prefix}RMS): ${_rms()}$suffix';
final String prefix;
final String suffix;
int max = 0;
double sum = 0;
double sumOfSquares = 0;
int count = 0;
}
class LatencyMetric extends Metric {
LatencyMetric(String name) : super(prefix: '$name(Latency', suffix: ' us.');
}
class StartMessageLatency {
StartMessageLatency(this.sendPort, this.spawned);
final SendPort sendPort;
final DateTime spawned;
}
class ResultMessageLatency {
ResultMessageLatency({
required this.timeToStartRunningCodeUs,
required this.timeToFinishRunningCodeUs,
});
final int timeToStartRunningCodeUs;
final int timeToFinishRunningCodeUs;
late int timeToIsolateSpawnUs;
late int timeToExitUs;
}
class AggregatedResultMessageLatency {
AggregatedResultMessageLatency(
this.toAfterIsolateSpawnUs,
this.toStartRunningCodeUs,
this.toFinishRunningCodeUs,
this.toExitUs,
);
@override
String toString() =>
'''$toAfterIsolateSpawnUs
$toStartRunningCodeUs
$toFinishRunningCodeUs
$toExitUs''';
final Metric toAfterIsolateSpawnUs;
final Metric toStartRunningCodeUs;
final Metric toFinishRunningCodeUs;
final Metric toExitUs;
}
Future<void> isolateCompiler(StartMessageLatency start) async {
final timeRunningCodeUs = DateTime.now();
await runZoned(
() => gen_kernel.compile(<String>[
'benchmarks/IsolateSpawn/dart/helloworld.dart',
'benchmarks/IsolateSpawn/dart/helloworld.dart.dill',
]),
zoneSpecification: ZoneSpecification(
print: (Zone self, ZoneDelegate parent, Zone zone, String line) {},
),
);
final timeFinishRunningCodeUs = DateTime.now();
start.sendPort.send(
ResultMessageLatency(
timeToStartRunningCodeUs: timeRunningCodeUs
.difference(start.spawned)
.inMicroseconds,
timeToFinishRunningCodeUs: timeFinishRunningCodeUs
.difference(start.spawned)
.inMicroseconds,
),
);
}
Future<void> main() async {
await SpawnLatency('IsolateSpawn.Dart2JS').report();
}