-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathIsolateBaseOverhead.dart
66 lines (58 loc) · 1.98 KB
/
IsolateBaseOverhead.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
// 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.
import 'dart:async';
import 'dart:io';
import 'dart:isolate';
const int count = 10000;
// The benchmark will spawn a long chain of isolates, keeping all of them
// alive until the last one which measures Rss at that point (i.e. when all
// isolates are alive), thereby getting a good estimate of memory-overhead per
// isolate.
void main() async {
final onDone = ReceivePort();
final lastIsolatePort = ReceivePort();
final startRss = ProcessInfo.currentRss;
final startUs = DateTime.now().microsecondsSinceEpoch;
await Isolate.spawn(
worker,
WorkerInfo(count, lastIsolatePort.sendPort),
onExit: onDone.sendPort,
);
final result = await lastIsolatePort.first as List;
final lastIsolateRss = result[0] as int;
final lastIsolateUs = result[1] as int;
await onDone.first;
final doneUs = DateTime.now().microsecondsSinceEpoch;
final averageMemoryUsageInKB = (lastIsolateRss - startRss) / count / 1024;
final averageStartLatencyInUs = (lastIsolateUs - startUs) / count;
final averageFinishLatencyInUs = (doneUs - startUs) / count;
print('IsolateBaseOverhead.Rss(MemoryUse): $averageMemoryUsageInKB');
print(
'IsolateBaseOverhead.StartLatency(Latency): $averageStartLatencyInUs us.',
);
print(
'IsolateBaseOverhead.FinishLatency(Latency): $averageFinishLatencyInUs us.',
);
}
class WorkerInfo {
final int id;
final SendPort result;
WorkerInfo(this.id, this.result);
}
Future worker(WorkerInfo workerInfo) async {
if (workerInfo.id == 1) {
workerInfo.result.send([
ProcessInfo.currentRss,
DateTime.now().microsecondsSinceEpoch,
]);
return;
}
final onExit = ReceivePort();
await Isolate.spawn(
worker,
WorkerInfo(workerInfo.id - 1, workerInfo.result),
onExit: onExit.sendPort,
);
await onExit.first;
}