Skip to content

Commit 558875d

Browse files
bkonyicommit-bot@chromium.org
authored andcommitted
[ VM / dart:io ] Added service extension scaffolding for dart:io network profiling
Change-Id: I531f002da65a3ea1439a6ed93d9698323f48bd31 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/121888 Reviewed-by: Siva Annamalai <[email protected]> Commit-Queue: Ben Konyi <[email protected]>
1 parent c6c79e5 commit 558875d

8 files changed

Lines changed: 165 additions & 0 deletions

File tree

‎runtime/bin/dartutils.cc‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,15 @@ Dart_Handle DartUtils::SetupIOLibrary(const char* namespc_path,
634634
Dart_Handle set_script_name =
635635
Dart_SetField(platform_type, script_name, dart_script);
636636
RETURN_IF_ERROR(set_script_name);
637+
638+
Dart_Handle network_profiling_type =
639+
GetDartType(DartUtils::kIOLibURL, "_NetworkProfiling");
640+
RETURN_IF_ERROR(network_profiling_type);
641+
Dart_Handle result =
642+
Dart_Invoke(network_profiling_type,
643+
NewString("_registerServiceExtension"), 0, nullptr);
644+
RETURN_IF_ERROR(result);
645+
637646
return Dart_Null();
638647
}
639648

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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:convert';
7+
import 'dart:developer';
8+
import 'package:observatory/service_io.dart';
9+
import 'package:observatory/sample_profile.dart';
10+
import 'package:unittest/unittest.dart';
11+
import 'service_test_common.dart';
12+
import 'test_helper.dart';
13+
14+
void test() {
15+
// TODO(bkonyi): do actual network operations.
16+
print('hello world!');
17+
}
18+
19+
var tests = <IsolateTest>[
20+
(Isolate isolate) async {
21+
await isolate.load();
22+
23+
// Ensure all network profiling service extensions are registered.
24+
const kGetHttpProfileRPC = 'ext.dart.io.getHttpProfile';
25+
const kGetSocketProfileRPC = 'ext.dart.io.getSocketProfile';
26+
expect(isolate.extensionRPCs.length, greaterThanOrEqualTo(2));
27+
expect(isolate.extensionRPCs.contains(kGetHttpProfileRPC), isTrue);
28+
expect(isolate.extensionRPCs.contains(kGetSocketProfileRPC), isTrue);
29+
30+
// Test invocations (will throw on failure).
31+
var response = await isolate.invokeRpcNoUpgrade(kGetHttpProfileRPC, {});
32+
expect(response['type'], 'HttpProfile');
33+
response = await isolate.invokeRpcNoUpgrade(kGetSocketProfileRPC, {});
34+
expect(response['type'], 'SocketProfile');
35+
},
36+
];
37+
38+
main(args) async => runIsolateTests(args, tests, testeeConcurrent: test);

‎sdk/lib/io/io.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ part 'io_sink.dart';
213213
part 'io_service.dart';
214214
part 'link.dart';
215215
part 'namespace_impl.dart';
216+
part 'network_profiling.dart';
216217
part 'overrides.dart';
217218
part 'platform.dart';
218219
part 'platform_impl.dart';

‎sdk/lib/io/io_sources.gni‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ io_sdk_sources = [
2121
"io_service.dart",
2222
"link.dart",
2323
"namespace_impl.dart",
24+
"network_profiling.dart",
2425
"overrides.dart",
2526
"platform.dart",
2627
"platform_impl.dart",

‎sdk/lib/io/network_profiling.dart‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
part of dart.io;
6+
7+
@pragma('vm:entry-point')
8+
abstract class _NetworkProfiling {
9+
static const _kGetHttpProfileRPC = 'ext.dart.io.getHttpProfile';
10+
static const _kGetSocketProfileRPC = 'ext.dart.io.getSocketProfile';
11+
12+
@pragma('vm:entry-point')
13+
static void _registerServiceExtension() {
14+
registerExtension(_kGetHttpProfileRPC, _serviceExtensionHandler);
15+
registerExtension(_kGetSocketProfileRPC, _serviceExtensionHandler);
16+
}
17+
18+
static Future<ServiceExtensionResponse> _serviceExtensionHandler(
19+
String method, Map<String, String> parameters) {
20+
String responseJson;
21+
switch (method) {
22+
case _kGetHttpProfileRPC:
23+
responseJson = _HttpProfile.toJSON();
24+
break;
25+
case _kGetSocketProfileRPC:
26+
responseJson = _SocketProfile.toJSON();
27+
break;
28+
default:
29+
return Future.value(ServiceExtensionResponse.error(
30+
ServiceExtensionResponse.extensionError,
31+
'Method $method does not exist'));
32+
}
33+
return Future.value(ServiceExtensionResponse.result(responseJson));
34+
}
35+
}
36+
37+
abstract class _HttpProfile {
38+
static const _kType = 'HttpProfile';
39+
// TODO(bkonyi): implement.
40+
static String toJSON() {
41+
final response = <String, dynamic>{
42+
'type': _kType,
43+
};
44+
return json.encode(response);
45+
}
46+
}
47+
48+
abstract class _SocketProfile {
49+
static const _kType = 'SocketProfile';
50+
// TODO(bkonyi): implement.
51+
static String toJSON() {
52+
final response = <String, dynamic>{
53+
'type': _kType,
54+
};
55+
return json.encode(response);
56+
}
57+
}

‎sdk_nnbd/lib/io/io.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ part 'io_sink.dart';
215215
part 'io_service.dart';
216216
part 'link.dart';
217217
part 'namespace_impl.dart';
218+
part 'network_profiling.dart';
218219
part 'overrides.dart';
219220
part 'platform.dart';
220221
part 'platform_impl.dart';

‎sdk_nnbd/lib/io/io_sources.gni‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ io_sdk_sources = [
2121
"io_service.dart",
2222
"link.dart",
2323
"namespace_impl.dart",
24+
"network_profiling.dart",
2425
"overrides.dart",
2526
"platform.dart",
2627
"platform_impl.dart",
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
part of dart.io;
6+
7+
@pragma('vm:entry-point')
8+
abstract class _NetworkProfiling {
9+
static const _kGetHttpProfileRPC = 'ext.dart.io.getHttpProfile';
10+
static const _kGetSocketProfileRPC = 'ext.dart.io.getSocketProfile';
11+
12+
@pragma('vm:entry-point')
13+
static void _registerServiceExtension() {
14+
registerExtension(_kGetHttpProfileRPC, _serviceExtensionHandler);
15+
registerExtension(_kGetSocketProfileRPC, _serviceExtensionHandler);
16+
}
17+
18+
static Future<ServiceExtensionResponse> _serviceExtensionHandler(
19+
String method, Map<String, String> parameters) {
20+
String responseJson;
21+
switch (method) {
22+
case _kGetHttpProfileRPC:
23+
responseJson = _HttpProfile.toJSON();
24+
break;
25+
case _kGetSocketProfileRPC:
26+
responseJson = _SocketProfile.toJSON();
27+
break;
28+
default:
29+
return Future.value(ServiceExtensionResponse.error(
30+
ServiceExtensionResponse.extensionError,
31+
'Method $method does not exist'));
32+
}
33+
return Future.value(ServiceExtensionResponse.result(responseJson));
34+
}
35+
}
36+
37+
abstract class _HttpProfile {
38+
static const _kType = 'HttpProfile';
39+
// TODO(bkonyi): implement.
40+
static String toJSON() {
41+
final response = <String, dynamic>{
42+
'type': _kType,
43+
};
44+
return json.encode(response);
45+
}
46+
}
47+
48+
abstract class _SocketProfile {
49+
static const _kType = 'SocketProfile';
50+
// TODO(bkonyi): implement.
51+
static String toJSON() {
52+
final response = <String, dynamic>{
53+
'type': _kType,
54+
};
55+
return json.encode(response);
56+
}
57+
}

0 commit comments

Comments
 (0)