|
| 1 | +// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | +import 'dart:io'; |
| 7 | +import 'dart:isolate'; |
| 8 | +import 'dart:math'; |
| 9 | + |
| 10 | +import 'package:meta/meta.dart'; |
| 11 | + |
| 12 | +import 'package:compiler/src/dart2js.dart' as dart2js_main; |
| 13 | + |
| 14 | +class SpawnLatencyAndMemory { |
| 15 | + SpawnLatencyAndMemory(this.name); |
| 16 | + |
| 17 | + Future<ResultMessageLatencyAndMemory> run() async { |
| 18 | + final completerResult = Completer(); |
| 19 | + final receivePort = ReceivePort()..listen(completerResult.complete); |
| 20 | + final Completer<DateTime> isolateExitedCompleter = Completer<DateTime>(); |
| 21 | + final onExitReceivePort = ReceivePort() |
| 22 | + ..listen((_) { |
| 23 | + isolateExitedCompleter.complete(DateTime.now()); |
| 24 | + }); |
| 25 | + final DateTime beforeSpawn = DateTime.now(); |
| 26 | + await Isolate.spawn( |
| 27 | + isolateCompiler, |
| 28 | + StartMessageLatencyAndMemory( |
| 29 | + receivePort.sendPort, beforeSpawn, ProcessInfo.currentRss), |
| 30 | + onExit: onExitReceivePort.sendPort, |
| 31 | + onError: onExitReceivePort.sendPort); |
| 32 | + final DateTime afterSpawn = DateTime.now(); |
| 33 | + |
| 34 | + final ResultMessageLatencyAndMemory result = await completerResult.future; |
| 35 | + receivePort.close(); |
| 36 | + final DateTime isolateExited = await isolateExitedCompleter.future; |
| 37 | + result.timeToExitUs = isolateExited.difference(beforeSpawn).inMicroseconds; |
| 38 | + result.timeToIsolateSpawnUs = |
| 39 | + afterSpawn.difference(beforeSpawn).inMicroseconds; |
| 40 | + onExitReceivePort.close(); |
| 41 | + |
| 42 | + return result; |
| 43 | + } |
| 44 | + |
| 45 | + Future<AggregatedResultMessageLatencyAndMemory> measureFor( |
| 46 | + int minimumMillis) async { |
| 47 | + final minimumMicros = minimumMillis * 1000; |
| 48 | + final watch = Stopwatch()..start(); |
| 49 | + final Metric toAfterIsolateSpawnUs = LatencyMetric("${name}ToAfterSpawn"); |
| 50 | + final Metric toStartRunningCodeUs = LatencyMetric("${name}ToStartRunning"); |
| 51 | + final Metric toFinishRunningCodeUs = |
| 52 | + LatencyMetric("${name}ToFinishRunning"); |
| 53 | + final Metric toExitUs = LatencyMetric("${name}ToExit"); |
| 54 | + final Metric deltaRss = MemoryMetric("${name}Delta"); |
| 55 | + while (watch.elapsedMicroseconds < minimumMicros) { |
| 56 | + final ResultMessageLatencyAndMemory result = await run(); |
| 57 | + toAfterIsolateSpawnUs.add(result.timeToIsolateSpawnUs); |
| 58 | + toStartRunningCodeUs.add(result.timeToStartRunningCodeUs); |
| 59 | + toFinishRunningCodeUs.add(result.timeToFinishRunningCodeUs); |
| 60 | + toExitUs.add(result.timeToExitUs); |
| 61 | + deltaRss.add(result.deltaRss); |
| 62 | + } |
| 63 | + return AggregatedResultMessageLatencyAndMemory(toAfterIsolateSpawnUs, |
| 64 | + toStartRunningCodeUs, toFinishRunningCodeUs, toExitUs, deltaRss); |
| 65 | + } |
| 66 | + |
| 67 | + Future<AggregatedResultMessageLatencyAndMemory> measure() async { |
| 68 | + await measureFor(500); // warm-up |
| 69 | + return measureFor(4000); // actual measurement |
| 70 | + } |
| 71 | + |
| 72 | + Future<void> report() async { |
| 73 | + final AggregatedResultMessageLatencyAndMemory result = await measure(); |
| 74 | + print(result); |
| 75 | + } |
| 76 | + |
| 77 | + final String name; |
| 78 | + RawReceivePort receivePort; |
| 79 | +} |
| 80 | + |
| 81 | +class Metric { |
| 82 | + Metric({@required this.prefix, @required this.suffix}); |
| 83 | + |
| 84 | + void add(int value) { |
| 85 | + if (value > max) { |
| 86 | + max = value; |
| 87 | + } |
| 88 | + sum += value; |
| 89 | + sumOfSquares += value * value; |
| 90 | + count++; |
| 91 | + } |
| 92 | + |
| 93 | + double _average() => sum / count; |
| 94 | + double _rms() => sqrt(sumOfSquares / count); |
| 95 | + |
| 96 | + toString() => "$prefix): ${_average()}$suffix\n" |
| 97 | + "${prefix}Max): $max$suffix\n" |
| 98 | + "${prefix}RMS): ${_rms()}$suffix"; |
| 99 | + |
| 100 | + final String prefix; |
| 101 | + final String suffix; |
| 102 | + int max = 0; |
| 103 | + double sum = 0; |
| 104 | + double sumOfSquares = 0; |
| 105 | + int count = 0; |
| 106 | +} |
| 107 | + |
| 108 | +class LatencyMetric extends Metric { |
| 109 | + LatencyMetric(String name) : super(prefix: "${name}(Latency", suffix: " us."); |
| 110 | +} |
| 111 | + |
| 112 | +class MemoryMetric extends Metric { |
| 113 | + MemoryMetric(String name) : super(prefix: "${name}Rss(MemoryUse", suffix: ""); |
| 114 | + |
| 115 | + toString() => "$prefix): ${_average()}$suffix\n"; |
| 116 | +} |
| 117 | + |
| 118 | +class StartMessageLatencyAndMemory { |
| 119 | + StartMessageLatencyAndMemory(this.sendPort, this.spawned, this.rss); |
| 120 | + |
| 121 | + final SendPort sendPort; |
| 122 | + final DateTime spawned; |
| 123 | + final int rss; |
| 124 | +} |
| 125 | + |
| 126 | +class ResultMessageLatencyAndMemory { |
| 127 | + ResultMessageLatencyAndMemory( |
| 128 | + {this.timeToStartRunningCodeUs, |
| 129 | + this.timeToFinishRunningCodeUs, |
| 130 | + this.deltaRss}); |
| 131 | + |
| 132 | + final int timeToStartRunningCodeUs; |
| 133 | + final int timeToFinishRunningCodeUs; |
| 134 | + final int deltaRss; |
| 135 | + |
| 136 | + int timeToIsolateSpawnUs; |
| 137 | + int timeToExitUs; |
| 138 | +} |
| 139 | + |
| 140 | +class AggregatedResultMessageLatencyAndMemory { |
| 141 | + AggregatedResultMessageLatencyAndMemory( |
| 142 | + this.toAfterIsolateSpawnUs, |
| 143 | + this.toStartRunningCodeUs, |
| 144 | + this.toFinishRunningCodeUs, |
| 145 | + this.toExitUs, |
| 146 | + this.deltaRss, |
| 147 | + ); |
| 148 | + |
| 149 | + String toString() => """$toAfterIsolateSpawnUs |
| 150 | +$toStartRunningCodeUs |
| 151 | +$toFinishRunningCodeUs |
| 152 | +$toExitUs |
| 153 | +$deltaRss"""; |
| 154 | + |
| 155 | + final Metric toAfterIsolateSpawnUs; |
| 156 | + final Metric toStartRunningCodeUs; |
| 157 | + final Metric toFinishRunningCodeUs; |
| 158 | + final Metric toExitUs; |
| 159 | + final Metric deltaRss; |
| 160 | +} |
| 161 | + |
| 162 | +Future<void> isolateCompiler(StartMessageLatencyAndMemory start) async { |
| 163 | + final DateTime timeRunningCodeUs = DateTime.now(); |
| 164 | + await runZoned( |
| 165 | + () => dart2js_main.internalMain(<String>[ |
| 166 | + "benchmarks/IsolateSpawn/dart/helloworld.dart", |
| 167 | + '--libraries-spec=sdk/lib/libraries.json' |
| 168 | + ]), |
| 169 | + zoneSpecification: ZoneSpecification( |
| 170 | + print: (Zone self, ZoneDelegate parent, Zone zone, String line) {})); |
| 171 | + final DateTime timeFinishRunningCodeUs = DateTime.now(); |
| 172 | + start.sendPort.send(ResultMessageLatencyAndMemory( |
| 173 | + timeToStartRunningCodeUs: |
| 174 | + timeRunningCodeUs.difference(start.spawned).inMicroseconds, |
| 175 | + timeToFinishRunningCodeUs: |
| 176 | + timeFinishRunningCodeUs.difference(start.spawned).inMicroseconds, |
| 177 | + deltaRss: ProcessInfo.currentRss - start.rss)); |
| 178 | +} |
| 179 | + |
| 180 | +Future<void> main() async { |
| 181 | + await SpawnLatencyAndMemory("IsolateSpawn.Dart2JS").report(); |
| 182 | +} |
0 commit comments