Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Diagnostics + Measure DB Performance #281

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion commet/lib/client/matrix/database/matrix_database_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:isolate';
import 'dart:ui';

import 'package:commet/config/app_config.dart';
import 'package:commet/diagnostic/diagnostics.dart';
import 'package:drift/drift.dart';
import 'package:drift/isolate.dart';
import 'package:flutter/services.dart';
Expand All @@ -19,7 +20,13 @@ Future<DatabaseApi> getMatrixDatabaseImplementation(String clientName) async {
var isolate = DriftIsolate.fromConnectPort(
port ?? (await _createIsolate(clientName)).connectPort);

return MatrixSdkDriftDatabase.init(await isolate.connect());
return MatrixSdkDriftDatabase.init(await isolate.connect(), clientName,
benchmark: benchmarkFunc);
}

Future<T> benchmarkFunc<T>(String name, Future<T> Function() func,
[int? itemCount]) {
return Diagnostics.databaseDiagnostics.timeAsync(name, func);
}

Future<DriftIsolate> _createIsolate(String clientName) async {
Expand Down
9 changes: 5 additions & 4 deletions commet/lib/client/matrix/matrix_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'package:commet/client/profile.dart';
import 'package:commet/client/room_preview.dart';
import 'package:commet/config/build_config.dart';
import 'package:commet/debug/log.dart';
import 'package:commet/diagnostic/diagnostics.dart';
import 'package:commet/main.dart';
import 'package:commet/ui/navigation/adaptive_dialog.dart';
import 'package:commet/ui/pages/matrix/authentication/matrix_uia_request.dart';
Expand Down Expand Up @@ -142,7 +143,7 @@ class MatrixClient extends Client {

static Future<void> loadFromDB(ClientManager manager,
{bool isBackgroundService = false}) async {
await diagnostics.timeAsync("loadFromDB", () async {
await Diagnostics.general.timeAsync("loadFromDB", () async {
var clients = preferences.getRegisteredMatrixClients();

List<Future> futures = List.empty(growable: true);
Expand All @@ -153,8 +154,8 @@ class MatrixClient extends Client {
for (var clientName in clients) {
var client = MatrixClient(identifier: clientName);
manager.addClient(client);
futures.add(diagnostics.timeAsync("Initializing client $clientName",
() async {
futures.add(Diagnostics.general
.timeAsync("Initializing client $clientName", () async {
try {
await client.init(true, isBackgroundService: isBackgroundService);
} catch (error, trace) {
Expand Down Expand Up @@ -196,7 +197,7 @@ class MatrixClient extends Client {
Future<void> init(bool loadingFromCache,
{bool isBackgroundService = false}) async {
if (!_matrixClient.isLogged()) {
await diagnostics.timeAsync("Matrix client init", () async {
await Diagnostics.general.timeAsync("Matrix client init", () async {
await _matrixClient.init(
waitForFirstSync: !loadingFromCache,
waitUntilLoadCompletedLoaded: true,
Expand Down
61 changes: 42 additions & 19 deletions commet/lib/diagnostic/diagnostics.dart
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;
}
2 changes: 1 addition & 1 deletion commet/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ FileCache? fileCache;
Preferences preferences = Preferences();
ShortcutsManager shortcutsManager = ShortcutsManager();
BackgroundTaskManager backgroundTaskManager = BackgroundTaskManager();
Diagnostics diagnostics = Diagnostics();
ClientManager? clientManager;

bool isHeadless = false;
Expand Down Expand Up @@ -153,6 +152,7 @@ Future<void> initNecessary() async {
]);

clientManager = await ClientManager.init();
Diagnostics.setPostInit();

shortcutsManager.init();
NotificationManager.init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';

import 'package:commet/client/client_manager.dart';
import 'package:commet/client/stale_info.dart';
import 'package:commet/main.dart';
import 'package:commet/ui/molecules/user_panel.dart';
import 'package:commet/ui/pages/login/login_page.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -121,6 +122,7 @@ class _AccountManagementSettingsTabState
displayName: clients[index].self!.displayName,
avatar: clients[index].self!.avatar,
detail: clients[index].self!.identifier,
internalId: clients[index].identifier,
onLogoutClicked: () =>
clientmanager.logoutClient(clients[index])),
);
Expand All @@ -132,16 +134,27 @@ class _AccountManagementSettingsTabState
{required String displayName,
ImageProvider? avatar,
String? detail,
String? internalId,
Function? onLogoutClicked}) {
String detailString = detail ?? "";

if (preferences.developerMode && internalId != null) {
detailString = "$detail - ($internalId)";
}

return Padding(
padding: const EdgeInsets.fromLTRB(8.0, 4, 12, 4),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
UserPanelView(
displayName: displayName,
avatar: avatar,
detail: detail,
Column(
children: [
UserPanelView(
displayName: displayName,
avatar: avatar,
detail: detailString,
),
],
),
tiamat.Button.danger(
text: promptLogoutSingleAccount,
Expand Down
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import 'dart:io';
import 'package:commet/client/components/push_notification/notification_content.dart';
import 'package:commet/client/components/push_notification/notification_manager.dart';
import 'package:commet/config/app_config.dart';
import 'package:commet/diagnostic/diagnostics.dart';
import 'package:commet/main.dart';
import 'package:commet/ui/navigation/navigation_utils.dart';
import 'package:commet/ui/pages/developer/benchmarks/timeline_viewer_benchmark.dart';
import 'package:commet/ui/pages/settings/categories/developer/cumulative_diagnostics_widget.dart';
import 'package:commet/utils/background_tasks/background_task_manager.dart';
import 'package:commet/utils/background_tasks/mock_tasks.dart';
import 'package:file_picker/file_picker.dart';
Expand Down Expand Up @@ -50,18 +52,11 @@ class _DeveloperSettingsPageState extends State<DeveloperSettingsPage> {
backgroundColor: Theme.of(context).colorScheme.surfaceContainerLow,
collapsedBackgroundColor:
Theme.of(context).colorScheme.surfaceContainerLow,
children: diagnostics.results
.map((e) => Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Flexible(child: tiamat.Text.labelEmphasised(e.name)),
tiamat.Text.label("${e.time.inMilliseconds}ms")
],
),
))
.toList());
children: [
Diagnostics.general,
Diagnostics.initialLoadDatabaseDiagnostics,
Diagnostics.postLoadDatabaseDiagnostics,
].map((e) => CumulativeDiagnosticsWidget(diagnostics: e)).toList());
}

Widget rendering() {
Expand Down
2 changes: 1 addition & 1 deletion commet/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ packages:
description:
path: "."
ref: HEAD
resolved-ref: "6de337a57d191780be02079ba476024acdff0052"
resolved-ref: "8b60e925966b6e0ca29baa3ebc9c560ce9f828b1"
url: "https://github.com/commetchat/matrix-dart-sdk-drift-db.git"
source: git
version: "0.0.1"
Expand Down
Loading