-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve Diagnostics + Measure DB Performance (#281)
- Loading branch information
Showing
8 changed files
with
151 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,70 @@ | ||
import 'package:commet/debug/log.dart'; | ||
import 'package:commet/main.dart'; | ||
|
||
class TimerResult { | ||
final String name; | ||
final Duration time; | ||
|
||
const TimerResult(this.name, this.time); | ||
class CumulativeMeasurement { | ||
int numCalls = 0; | ||
Duration totalDuration = Duration.zero; | ||
String name = ""; | ||
} | ||
|
||
class Diagnostics { | ||
List<TimerResult> results = List.empty(growable: true); | ||
|
||
void addResult(String name, Duration time) { | ||
results.add(TimerResult(name, time)); | ||
class CumulativeDiagnostics { | ||
CumulativeDiagnostics(this.name); | ||
|
||
Log.i("Diagnostics: $name took ${time.inMilliseconds}ms"); | ||
} | ||
String name; | ||
Map<String, CumulativeMeasurement> measurements = {}; | ||
|
||
Future<T> timeAsync<T>(String name, Future<T> Function() func) async { | ||
T time<T>(String name, T Function() func) { | ||
if (!preferences.developerMode) return func(); | ||
|
||
Stopwatch s = Stopwatch(); | ||
|
||
s.start(); | ||
|
||
var result = await func(); | ||
var result = func(); | ||
|
||
s.stop(); | ||
|
||
addResult(name, s.elapsed); | ||
return result; | ||
} | ||
|
||
T time<T>(String name, T Function() func) { | ||
Future<T> timeAsync<T>(String name, Future<T> Function() func) async { | ||
if (!preferences.developerMode) return func(); | ||
Stopwatch s = Stopwatch(); | ||
|
||
Stopwatch s = Stopwatch(); | ||
s.start(); | ||
|
||
var result = func(); | ||
var result = await func(); | ||
|
||
s.stop(); | ||
|
||
addResult(name, s.elapsed); | ||
return result; | ||
} | ||
|
||
void addResult(String name, Duration elapsed) { | ||
var m = measurements[name] ?? CumulativeMeasurement(); | ||
m.numCalls += 1; | ||
m.totalDuration += elapsed; | ||
m.name = name; | ||
measurements[name] = m; | ||
} | ||
} | ||
|
||
class Diagnostics { | ||
static bool _isPostInit = false; | ||
|
||
static void setPostInit() { | ||
_isPostInit = true; | ||
} | ||
|
||
static CumulativeDiagnostics initialLoadDatabaseDiagnostics = | ||
CumulativeDiagnostics("Initial Load Database Diagnostics"); | ||
|
||
static CumulativeDiagnostics postLoadDatabaseDiagnostics = | ||
CumulativeDiagnostics("Post Load Database Diagnostics"); | ||
|
||
static CumulativeDiagnostics general = CumulativeDiagnostics("General"); | ||
|
||
static CumulativeDiagnostics get databaseDiagnostics => _isPostInit | ||
? postLoadDatabaseDiagnostics | ||
: initialLoadDatabaseDiagnostics; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
commet/lib/ui/pages/settings/categories/developer/cumulative_diagnostics_widget.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import 'package:commet/diagnostic/diagnostics.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
import 'package:tiamat/tiamat.dart' as tiamat; | ||
|
||
class CumulativeDiagnosticsWidget extends StatefulWidget { | ||
const CumulativeDiagnosticsWidget({required this.diagnostics, super.key}); | ||
|
||
final CumulativeDiagnostics diagnostics; | ||
|
||
@override | ||
State<CumulativeDiagnosticsWidget> createState() => | ||
_CumulativeDiagnosticsWidgetState(); | ||
} | ||
|
||
class _CumulativeDiagnosticsWidgetState | ||
extends State<CumulativeDiagnosticsWidget> { | ||
late List<CumulativeMeasurement> measurements; | ||
|
||
@override | ||
void initState() { | ||
measurements = widget.diagnostics.measurements.values.toList(); | ||
measurements.sort( | ||
(a, b) => b.totalDuration.compareTo(a.totalDuration), | ||
); | ||
|
||
super.initState(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return ExpansionTile( | ||
title: tiamat.Text.labelEmphasised(widget.diagnostics.name), | ||
initiallyExpanded: false, | ||
backgroundColor: Theme.of(context).colorScheme.surfaceContainerLow, | ||
collapsedBackgroundColor: | ||
Theme.of(context).colorScheme.surfaceContainerLow, | ||
children: measurements | ||
.mapIndexed((e, i) => Container( | ||
color: i % 2 == 0 | ||
? Theme.of(context).colorScheme.surfaceDim | ||
: Theme.of(context).colorScheme.surfaceContainerHigh, | ||
child: Padding( | ||
padding: const EdgeInsets.fromLTRB(20, 2, 20, 2), | ||
child: Row( | ||
mainAxisAlignment: MainAxisAlignment.spaceBetween, | ||
children: [ | ||
Flexible( | ||
child: Padding( | ||
padding: const EdgeInsets.fromLTRB(0, 2, 8, 2), | ||
child: tiamat.Text.labelLow( | ||
e.name, | ||
color: Theme.of(context).colorScheme.onSurface, | ||
), | ||
)), | ||
tiamat.Text.labelLow( | ||
[ | ||
"${e.numCalls} calls", | ||
"${(e.totalDuration.inMilliseconds / e.numCalls).round()}ms avg", | ||
"${e.totalDuration.inMilliseconds}ms total", | ||
].join(" - "), | ||
color: Theme.of(context).colorScheme.onSurfaceVariant, | ||
) | ||
], | ||
), | ||
), | ||
)) | ||
.toList()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters