@@ -12,7 +12,6 @@ import '_background_isolate_binary_messenger_io.dart'
1212
1313import 'binary_messenger.dart' ;
1414import 'binding.dart' ;
15- import 'debug.dart' show debugProfilePlatformChannels;
1615import 'message_codec.dart' ;
1716import 'message_codecs.dart' ;
1817
@@ -23,9 +22,21 @@ export 'binary_messenger.dart' show BinaryMessenger;
2322export 'binding.dart' show RootIsolateToken;
2423export 'message_codec.dart' show MessageCodec, MethodCall, MethodCodec;
2524
26- bool _debugProfilePlatformChannelsIsRunning = false ;
27- const Duration _debugProfilePlatformChannelsRate = Duration (seconds: 1 );
28- final Expando <BinaryMessenger > _debugBinaryMessengers = Expando <BinaryMessenger >();
25+ /// Profile and print statistics on Platform Channel usage.
26+ ///
27+ /// When this is true statistics about the usage of Platform Channels will be
28+ /// printed out periodically to the console and Timeline events will show the
29+ /// time between sending and receiving a message (encoding and decoding time
30+ /// excluded).
31+ ///
32+ /// The statistics include the total bytes transmitted and the average number of
33+ /// bytes per invocation in the last quantum. "Up" means in the direction of
34+ /// Flutter to the host platform, "down" is the host platform to flutter.
35+ const bool kProfilePlatformChannels = false ;
36+
37+ bool _profilePlatformChannelsIsRunning = false ;
38+ const Duration _profilePlatformChannelsRate = Duration (seconds: 1 );
39+ final Expando <BinaryMessenger > _profiledBinaryMessengers = Expando <BinaryMessenger >();
2940
3041class _ProfiledBinaryMessenger implements BinaryMessenger {
3142 const _ProfiledBinaryMessenger (this .proxy, this .channelTypeName, this .codecTypeName);
@@ -40,17 +51,12 @@ class _ProfiledBinaryMessenger implements BinaryMessenger {
4051
4152 Future <ByteData ?>? sendWithPostfix (String channel, String postfix, ByteData ? message) async {
4253 _debugRecordUpStream (channelTypeName, '$channel $postfix ' , codecTypeName, message);
43- TimelineTask ? debugTimelineTask;
44- if (! kReleaseMode) {
45- debugTimelineTask = TimelineTask ()..start ('Platform Channel send $channel $postfix ' );
46- }
54+ final TimelineTask timelineTask = TimelineTask ()..start ('Platform Channel send $channel $postfix ' );
4755 final ByteData ? result;
4856 try {
4957 result = await proxy.send (channel, message);
5058 } finally {
51- if (! kReleaseMode) {
52- debugTimelineTask! .finish ();
53- }
59+ timelineTask.finish ();
5460 }
5561 _debugRecordDownStream (channelTypeName, '$channel $postfix ' , codecTypeName, result);
5662 return result;
@@ -93,17 +99,17 @@ class _PlatformChannelStats {
9399 double get averageDownPayload => _downBytes / _downCount;
94100}
95101
96- final Map <String , _PlatformChannelStats > _debugProfilePlatformChannelsStats = < String , _PlatformChannelStats > {};
102+ final Map <String , _PlatformChannelStats > _profilePlatformChannelsStats = < String , _PlatformChannelStats > {};
97103
98104Future <void > _debugLaunchProfilePlatformChannels () async {
99- if (! _debugProfilePlatformChannelsIsRunning ) {
100- _debugProfilePlatformChannelsIsRunning = true ;
101- await Future <dynamic >.delayed (_debugProfilePlatformChannelsRate );
102- _debugProfilePlatformChannelsIsRunning = false ;
105+ if (! _profilePlatformChannelsIsRunning ) {
106+ _profilePlatformChannelsIsRunning = true ;
107+ await Future <dynamic >.delayed (_profilePlatformChannelsRate );
108+ _profilePlatformChannelsIsRunning = false ;
103109 final StringBuffer log = StringBuffer ();
104110 log.writeln ('Platform Channel Stats:' );
105111 final List <_PlatformChannelStats > allStats =
106- _debugProfilePlatformChannelsStats .values.toList ();
112+ _profilePlatformChannelsStats .values.toList ();
107113 // Sort highest combined bandwidth first.
108114 allStats.sort ((_PlatformChannelStats x, _PlatformChannelStats y) =>
109115 (y.upBytes + y.downBytes) - (x.upBytes + x.downBytes));
@@ -112,14 +118,14 @@ Future<void> _debugLaunchProfilePlatformChannels() async {
112118 ' (name:"${stats .channel }" type:"${stats .type }" codec:"${stats .codec }" upBytes:${stats .upBytes } upBytes_avg:${stats .averageUpPayload .toStringAsFixed (1 )} downBytes:${stats .downBytes } downBytes_avg:${stats .averageDownPayload .toStringAsFixed (1 )})' );
113119 }
114120 debugPrint (log.toString ());
115- _debugProfilePlatformChannelsStats .clear ();
121+ _profilePlatformChannelsStats .clear ();
116122 }
117123}
118124
119125void _debugRecordUpStream (String channelTypeName, String name,
120126 String codecTypeName, ByteData ? bytes) {
121127 final _PlatformChannelStats stats =
122- _debugProfilePlatformChannelsStats [name] ?? =
128+ _profilePlatformChannelsStats [name] ?? =
123129 _PlatformChannelStats (name, codecTypeName, channelTypeName);
124130 stats.addUpStream (bytes? .lengthInBytes ?? 0 );
125131 _debugLaunchProfilePlatformChannels ();
@@ -128,7 +134,7 @@ void _debugRecordUpStream(String channelTypeName, String name,
128134void _debugRecordDownStream (String channelTypeName, String name,
129135 String codecTypeName, ByteData ? bytes) {
130136 final _PlatformChannelStats stats =
131- _debugProfilePlatformChannelsStats [name] ?? =
137+ _profilePlatformChannelsStats [name] ?? =
132138 _PlatformChannelStats (name, codecTypeName, channelTypeName);
133139 stats.addDownStream (bytes? .lengthInBytes ?? 0 );
134140 _debugLaunchProfilePlatformChannels ();
@@ -184,8 +190,8 @@ class BasicMessageChannel<T> {
184190 /// [BackgroundIsolateBinaryMessenger.ensureInitialized] .
185191 BinaryMessenger get binaryMessenger {
186192 final BinaryMessenger result = _binaryMessenger ?? _findBinaryMessenger ();
187- return ! kReleaseMode && debugProfilePlatformChannels
188- ? _debugBinaryMessengers [this ] ?? = _ProfiledBinaryMessenger (
193+ return kProfilePlatformChannels
194+ ? _profiledBinaryMessengers [this ] ?? = _ProfiledBinaryMessenger (
189195 // ignore: no_runtimetype_tostring
190196 result, runtimeType.toString (), codec.runtimeType.toString ())
191197 : result;
@@ -273,8 +279,8 @@ class MethodChannel {
273279 /// [BackgroundIsolateBinaryMessenger.ensureInitialized] .
274280 BinaryMessenger get binaryMessenger {
275281 final BinaryMessenger result = _binaryMessenger ?? _findBinaryMessenger ();
276- return ! kReleaseMode && debugProfilePlatformChannels
277- ? _debugBinaryMessengers [this ] ?? = _ProfiledBinaryMessenger (
282+ return kProfilePlatformChannels
283+ ? _profiledBinaryMessengers [this ] ?? = _ProfiledBinaryMessenger (
278284 // ignore: no_runtimetype_tostring
279285 result, runtimeType.toString (), codec.runtimeType.toString ())
280286 : result;
@@ -304,7 +310,7 @@ class MethodChannel {
304310 Future <T ?> _invokeMethod <T >(String method, { required bool missingOk, dynamic arguments }) async {
305311 final ByteData input = codec.encodeMethodCall (MethodCall (method, arguments));
306312 final ByteData ? result =
307- ! kReleaseMode && debugProfilePlatformChannels ?
313+ kProfilePlatformChannels ?
308314 await (binaryMessenger as _ProfiledBinaryMessenger ).sendWithPostfix (name, '#$method ' , input) :
309315 await binaryMessenger.send (name, input);
310316 if (result == null ) {
0 commit comments