-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathvm.dart
More file actions
138 lines (114 loc) · 3.98 KB
/
Copy pathvm.dart
File metadata and controls
138 lines (114 loc) · 3.98 KB
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
// Copyright (c) 2015, 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' show Future;
import 'dart:io' show Directory, File, Link, Platform, stdout;
import 'benchmarks/index.dart' show createBenchmark;
import 'data_index.dart'
show hostfileName, latestVMReportName, pubspecLockName, pubspecYamlName;
import 'generated/benchmark.pb.dart' as pb;
import 'report.dart'
show createPackages, createPlatform, encodeReport, findUpdatedResponse;
import 'suite.dart' show runSuite;
/// Runs a benchmark suite.
/// Writes a report to latest_vm.pb.json after every change,
/// to make progress available to the browser.
Future<void> runSuiteInVM(pb.Suite suite) async {
var env = await _loadEnv();
pb.Report? lastReport;
pb.Response? lastUpdate;
for (var report in runSuite(suite.requests, samplesPerBatch: 10)) {
report.env = env;
// show progress
var update = findUpdatedResponse(lastReport, report);
if (update != null) {
var summary = _summarize(update);
if (lastUpdate == null || update.request != lastUpdate.request) {
stdout.write('\n$summary');
} else {
_overwrite(summary);
}
}
lastReport = report;
lastUpdate = update;
}
// save the report to a file
var outFile = '${dataDir.path}/$latestVMReportName';
var tmpFile = File('$outFile.tmp');
await tmpFile.writeAsString(encodeReport(lastReport!));
await tmpFile.rename(outFile);
print('\nWrote result to $outFile');
}
String _summarize(pb.Response r) {
var b = createBenchmark(r.request);
return b.summarizeResponse(r);
}
final _escapeChar = String.fromCharCode(27);
final _clearLine = '\r$_escapeChar[2K';
/// Overwrite the last line printed to the terminal.
void _overwrite(String line) {
stdout.write('$_clearLine$line');
}
Future<pb.Env> _loadEnv() async {
await _ensureDataDir();
var platform = createPlatform()
..hostname = _hostname
..osType = _osType
..dartVersion = Platform.version;
var pubspec = await File(pubspecYaml.path).readAsString();
var lock = await File(pubspecLock.path).readAsString();
return pb.Env()
..script = _script
..platform = platform
..packages = createPackages(pubspec, lock);
}
/// Create files and symlinks in the data directory.
///
/// This is so they can be accessed in browser benchmarks.
Future<void> _ensureDataDir() async {
await hostnameFile.writeAsString(_hostname);
if (!await pubspecYaml.exists()) {
await pubspecYaml.create('${pubspecDir.path}/pubspec.yaml');
}
if (!await pubspecLock.exists()) {
await pubspecLock.create('${pubspecDir.path}/pubspec.lock');
}
}
String get _script {
// Only including the last part of the script name.
return Platform.script.pathSegments.last;
}
String get _hostname {
// Only including the first part of the hostname.
var h = Platform.localHostname;
var firstDot = h.indexOf('.');
if (firstDot == -1) return h;
return h.substring(0, firstDot);
}
pb.OSType get _osType {
if (Platform.isLinux) return pb.OSType.LINUX;
if (Platform.isMacOS) return pb.OSType.MAC;
if (Platform.isWindows) return pb.OSType.WINDOWS;
if (Platform.isAndroid) return pb.OSType.ANDROID;
// What is this?
throw 'unknown OS type';
}
final File hostnameFile = File('${dataDir.path}/$hostfileName');
final Link pubspecYaml = Link('${dataDir.path}/$pubspecYamlName');
final Link pubspecLock = Link('${dataDir.path}/$pubspecLockName');
final Directory dataDir = () {
var d = Directory('${pubspecDir.path}/web/data');
if (!d.existsSync()) {
throw "data dir doesn't exist at ${d.path}";
}
return d;
}();
/// The directory containing the pubspec.yaml file.
final Directory pubspecDir = () {
for (var d = Directory.current; d.parent != d; d = d.parent) {
if (File('${d.path}/pubspec.yaml').existsSync()) {
return d;
}
}
throw "can't find pubspec.yaml above ${Directory.current}";
}();