Skip to content

Commit f036fea

Browse files
committed
log: Introduce profilePrint; use it for timing initial fetch
1 parent 076edf0 commit f036fea

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

lib/log.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
import 'package:flutter/foundation.dart';
3+
24
/// Whether [debugLog] should do anything.
35
///
46
/// This has an effect only in a debug build.
@@ -31,6 +33,30 @@ bool debugLog(String message) {
3133
return true;
3234
}
3335

36+
/// Print a piece of profiling data.
37+
///
38+
/// This should be called only in profile mode:
39+
/// * In debug mode, any profiling results will be misleading.
40+
/// * In release mode, we should avoid doing the computation to even produce
41+
/// the [message] argument.
42+
///
43+
/// As a reminder of that, this function will throw in debug mode.
44+
///
45+
/// Example usage:
46+
/// ```dart
47+
/// final stopwatch = Stopwatch()..start();
48+
/// final data = await someSlowOperation();
49+
/// if (kProfileMode) {
50+
/// final t = stopwatch.elapsed;
51+
/// profilePrint("some-operation time: ${t.inMilliseconds}ms");
52+
/// }
53+
/// ```
54+
void profilePrint(String message) {
55+
assert(kProfileMode, 'Use profilePrint only within `if (kProfileMode)`.');
56+
if (kReleaseMode) return;
57+
print(message); // ignore: avoid_print
58+
}
59+
3460
// This should only be used for error reporting functions that allow the error
3561
// to be cancelled programmatically. The implementation is expected to handle
3662
// `null` for the `message` parameter and promptly dismiss the reported errors.

lib/model/store.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -996,8 +996,9 @@ class UpdateMachine {
996996
final stopwatch = Stopwatch()..start();
997997
final initialSnapshot = await _registerQueueWithRetry(connection,
998998
stopAndThrowIfNoAccount: stopAndThrowIfNoAccount);
999-
final t = (stopwatch..stop()).elapsed;
1000-
assert(debugLog("initial fetch time: ${t.inMilliseconds}ms"));
999+
if (kProfileMode) {
1000+
profilePrint("initial fetch time: ${stopwatch.elapsed.inMilliseconds}ms");
1001+
}
10011002

10021003
if (initialSnapshot.zulipVersion != account.zulipVersion
10031004
|| initialSnapshot.zulipMergeBase != account.zulipMergeBase

0 commit comments

Comments
 (0)