@@ -12,7 +12,6 @@ import '_background_isolate_binary_messenger_io.dart'
12
12
13
13
import 'binary_messenger.dart' ;
14
14
import 'binding.dart' ;
15
- import 'debug.dart' show debugProfilePlatformChannels;
16
15
import 'message_codec.dart' ;
17
16
import 'message_codecs.dart' ;
18
17
@@ -23,9 +22,21 @@ export 'binary_messenger.dart' show BinaryMessenger;
23
22
export 'binding.dart' show RootIsolateToken;
24
23
export 'message_codec.dart' show MessageCodec, MethodCall, MethodCodec;
25
24
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 >();
29
40
30
41
class _ProfiledBinaryMessenger implements BinaryMessenger {
31
42
const _ProfiledBinaryMessenger (this .proxy, this .channelTypeName, this .codecTypeName);
@@ -40,17 +51,12 @@ class _ProfiledBinaryMessenger implements BinaryMessenger {
40
51
41
52
Future <ByteData ?>? sendWithPostfix (String channel, String postfix, ByteData ? message) async {
42
53
_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 ' );
47
55
final ByteData ? result;
48
56
try {
49
57
result = await proxy.send (channel, message);
50
58
} finally {
51
- if (! kReleaseMode) {
52
- debugTimelineTask! .finish ();
53
- }
59
+ timelineTask.finish ();
54
60
}
55
61
_debugRecordDownStream (channelTypeName, '$channel $postfix ' , codecTypeName, result);
56
62
return result;
@@ -93,17 +99,17 @@ class _PlatformChannelStats {
93
99
double get averageDownPayload => _downBytes / _downCount;
94
100
}
95
101
96
- final Map <String , _PlatformChannelStats > _debugProfilePlatformChannelsStats = < String , _PlatformChannelStats > {};
102
+ final Map <String , _PlatformChannelStats > _profilePlatformChannelsStats = < String , _PlatformChannelStats > {};
97
103
98
104
Future <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 ;
103
109
final StringBuffer log = StringBuffer ();
104
110
log.writeln ('Platform Channel Stats:' );
105
111
final List <_PlatformChannelStats > allStats =
106
- _debugProfilePlatformChannelsStats .values.toList ();
112
+ _profilePlatformChannelsStats .values.toList ();
107
113
// Sort highest combined bandwidth first.
108
114
allStats.sort ((_PlatformChannelStats x, _PlatformChannelStats y) =>
109
115
(y.upBytes + y.downBytes) - (x.upBytes + x.downBytes));
@@ -112,14 +118,14 @@ Future<void> _debugLaunchProfilePlatformChannels() async {
112
118
' (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 )})' );
113
119
}
114
120
debugPrint (log.toString ());
115
- _debugProfilePlatformChannelsStats .clear ();
121
+ _profilePlatformChannelsStats .clear ();
116
122
}
117
123
}
118
124
119
125
void _debugRecordUpStream (String channelTypeName, String name,
120
126
String codecTypeName, ByteData ? bytes) {
121
127
final _PlatformChannelStats stats =
122
- _debugProfilePlatformChannelsStats [name] ?? =
128
+ _profilePlatformChannelsStats [name] ?? =
123
129
_PlatformChannelStats (name, codecTypeName, channelTypeName);
124
130
stats.addUpStream (bytes? .lengthInBytes ?? 0 );
125
131
_debugLaunchProfilePlatformChannels ();
@@ -128,7 +134,7 @@ void _debugRecordUpStream(String channelTypeName, String name,
128
134
void _debugRecordDownStream (String channelTypeName, String name,
129
135
String codecTypeName, ByteData ? bytes) {
130
136
final _PlatformChannelStats stats =
131
- _debugProfilePlatformChannelsStats [name] ?? =
137
+ _profilePlatformChannelsStats [name] ?? =
132
138
_PlatformChannelStats (name, codecTypeName, channelTypeName);
133
139
stats.addDownStream (bytes? .lengthInBytes ?? 0 );
134
140
_debugLaunchProfilePlatformChannels ();
@@ -184,8 +190,8 @@ class BasicMessageChannel<T> {
184
190
/// [BackgroundIsolateBinaryMessenger.ensureInitialized] .
185
191
BinaryMessenger get binaryMessenger {
186
192
final BinaryMessenger result = _binaryMessenger ?? _findBinaryMessenger ();
187
- return ! kReleaseMode && debugProfilePlatformChannels
188
- ? _debugBinaryMessengers [this ] ?? = _ProfiledBinaryMessenger (
193
+ return kProfilePlatformChannels
194
+ ? _profiledBinaryMessengers [this ] ?? = _ProfiledBinaryMessenger (
189
195
// ignore: no_runtimetype_tostring
190
196
result, runtimeType.toString (), codec.runtimeType.toString ())
191
197
: result;
@@ -273,8 +279,8 @@ class MethodChannel {
273
279
/// [BackgroundIsolateBinaryMessenger.ensureInitialized] .
274
280
BinaryMessenger get binaryMessenger {
275
281
final BinaryMessenger result = _binaryMessenger ?? _findBinaryMessenger ();
276
- return ! kReleaseMode && debugProfilePlatformChannels
277
- ? _debugBinaryMessengers [this ] ?? = _ProfiledBinaryMessenger (
282
+ return kProfilePlatformChannels
283
+ ? _profiledBinaryMessengers [this ] ?? = _ProfiledBinaryMessenger (
278
284
// ignore: no_runtimetype_tostring
279
285
result, runtimeType.toString (), codec.runtimeType.toString ())
280
286
: result;
@@ -304,7 +310,7 @@ class MethodChannel {
304
310
Future <T ?> _invokeMethod <T >(String method, { required bool missingOk, dynamic arguments }) async {
305
311
final ByteData input = codec.encodeMethodCall (MethodCall (method, arguments));
306
312
final ByteData ? result =
307
- ! kReleaseMode && debugProfilePlatformChannels ?
313
+ kProfilePlatformChannels ?
308
314
await (binaryMessenger as _ProfiledBinaryMessenger ).sendWithPostfix (name, '#$method ' , input) :
309
315
await binaryMessenger.send (name, input);
310
316
if (result == null ) {
0 commit comments