[vm] tools/heapsnapshot has download util

Change-Id: I11e8a750a3a40d3fbc2733f4120c50b25194cc82
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/273183
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Jens Johansen <jensj@google.com>
This commit is contained in:
Jens Johansen 2022-12-01 13:25:47 +00:00 committed by Commit Queue
parent 05c0f5ca9a
commit 89b6ba91ba
2 changed files with 51 additions and 1 deletions

View file

@ -0,0 +1,38 @@
// 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:io' show File;
import 'dart:typed_data' show BytesBuilder;
import 'package:heapsnapshot/src/load.dart' show loadFromUri;
Future<void> main(List<String> args) async {
String filename = "snapshot.heapsnapshot";
String? uriString;
for (String arg in args) {
if (arg.startsWith("-o=")) {
filename = arg.substring("-o=".length);
} else {
uriString = arg;
}
}
if (uriString == null) {
throw "Give uri as input.";
}
print("Will try to fetch snapshot from $uriString and dump to $filename");
await download(uriString, filename);
print("Done");
}
Future<void> download(String uriString, String filename) async {
final chunks = await loadFromUri(Uri.parse(uriString));
final bytesBuilder = BytesBuilder();
for (final bd in chunks) {
bytesBuilder.add(bd.buffer.asUint8List(bd.offsetInBytes, bd.lengthInBytes));
print("Got ${(bytesBuilder.length / 1024 / 1024).toStringAsFixed(1)} MB");
}
final bytes = bytesBuilder.toBytes();
File(filename).writeAsBytesSync(bytes);
}

View file

@ -9,7 +9,19 @@ import 'package:vm_service/vm_service.dart';
import 'package:vm_service/vm_service_io.dart';
Future<List<ByteData>> loadFromUri(Uri uri) async {
final wsUri = uri.replace(scheme: 'ws', path: '/ws');
final Uri wsUri;
if (uri.isScheme("ws")) {
wsUri = uri;
} else {
if (uri.path.isEmpty || uri.path == "/") {
uri = uri.replace(path: "/ws");
} else if (uri.path.endsWith("/")) {
uri = uri.replace(path: "${uri.path}ws");
} else {
uri = uri.replace(path: "${uri.path}/ws");
}
wsUri = uri.replace(scheme: 'ws');
}
final service = await vmServiceConnectUri(wsUri.toString());
try {
final r = await _getHeapsnapshot(service);