From 315ed7bdcbe4afde73fa85d87e167b573e91b22f Mon Sep 17 00:00:00 2001 From: Krille Fear Date: Fri, 5 Nov 2021 13:47:20 +0100 Subject: [PATCH] feat: Add more benchmarks for sync, timeline, init This also disables benchmarks if loglevel is higher than debug. --- lib/src/client.dart | 14 ++++++- lib/src/database/hive_database.dart | 48 ++++++++++++----------- lib/src/utils/run_benchmarked.dart | 5 ++- lib/src/utils/sync_update_item_count.dart | 41 +++++++++++++++++++ 4 files changed, 82 insertions(+), 26 deletions(-) create mode 100644 lib/src/utils/sync_update_item_count.dart diff --git a/lib/src/client.dart b/lib/src/client.dart index 2ac2b831..ec74f2cd 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -23,6 +23,7 @@ import 'dart:typed_data'; import 'package:http/http.dart' as http; import 'package:matrix/src/utils/run_in_root.dart'; +import 'package:matrix/src/utils/sync_update_item_count.dart'; import 'package:mime/mime.dart'; import 'package:olm/olm.dart' as olm; import 'package:collection/collection.dart' show IterableExtension; @@ -38,6 +39,7 @@ import 'utils/device_keys_list.dart'; import 'utils/event_update.dart'; import 'utils/http_timeout.dart'; import 'utils/matrix_file.dart'; +import 'utils/run_benchmarked.dart'; import 'utils/to_device_event.dart'; import 'utils/uia_request.dart'; import 'utils/multilock.dart'; @@ -920,8 +922,12 @@ class Client extends MatrixApi { throw Exception('User is already logged in! Call [logout()] first!'); } + final databaseBuilder = this.databaseBuilder; if (databaseBuilder != null) { - _database ??= await databaseBuilder?.call(this); + _database ??= await runBenchmarked( + 'Build database', + () async => await databaseBuilder(this), + ); } String? olmAccount; @@ -1154,7 +1160,11 @@ class Client extends MatrixApi { await database.storePrevBatch(syncResp.nextBatch); } }); - await _currentTransaction; + await runBenchmarked( + 'Process sync', + () async => await _currentTransaction, + syncResp.itemCount, + ); onSyncStatus.add(SyncStatusUpdate(SyncStatus.cleaningUp)); } else { await _handleSync(syncResp); diff --git a/lib/src/database/hive_database.dart b/lib/src/database/hive_database.dart index 4a96a3e4..8c53e47e 100644 --- a/lib/src/database/hive_database.dart +++ b/lib/src/database/hive_database.dart @@ -393,32 +393,34 @@ class FamedlySdkHiveDatabase extends DatabaseApi { Room room, { int start = 0, int? limit, - }) async { - // Get the synced event IDs from the store - final timelineKey = MultiKey(room.id, '').toString(); - final timelineEventIds = - (await _timelineFragmentsBox.get(timelineKey) as List? ?? []); + }) => + runBenchmarked>('Get event list', () async { + // Get the synced event IDs from the store + final timelineKey = MultiKey(room.id, '').toString(); + final timelineEventIds = + (await _timelineFragmentsBox.get(timelineKey) as List? ?? []); - // Get the local stored SENDING events from the store - late final List sendingEventIds; - if (start != 0) { - sendingEventIds = []; - } else { - final sendingTimelineKey = MultiKey(room.id, 'SENDING').toString(); - sendingEventIds = - (await _timelineFragmentsBox.get(sendingTimelineKey) as List? ?? []); - } + // Get the local stored SENDING events from the store + late final List sendingEventIds; + if (start != 0) { + sendingEventIds = []; + } else { + final sendingTimelineKey = MultiKey(room.id, 'SENDING').toString(); + sendingEventIds = + (await _timelineFragmentsBox.get(sendingTimelineKey) as List? ?? + []); + } - // Combine those two lists while respecting the start and limit parameters. - final end = min( - timelineEventIds.length, start + (limit ?? timelineEventIds.length)); - final eventIds = sendingEventIds + - (start < timelineEventIds.length - ? timelineEventIds.getRange(start, end).toList() - : []); + // Combine those two lists while respecting the start and limit parameters. + final end = min(timelineEventIds.length, + start + (limit ?? timelineEventIds.length)); + final eventIds = sendingEventIds + + (start < timelineEventIds.length + ? timelineEventIds.getRange(start, end).toList() + : []); - return await _getEventsByIds(eventIds.cast(), room); - } + return await _getEventsByIds(eventIds.cast(), room); + }); @override Future getFile(Uri mxcUri) async { diff --git a/lib/src/utils/run_benchmarked.dart b/lib/src/utils/run_benchmarked.dart index dd534a1d..af5a85bf 100644 --- a/lib/src/utils/run_benchmarked.dart +++ b/lib/src/utils/run_benchmarked.dart @@ -26,6 +26,9 @@ Future runBenchmarked( Future Function() func, [ int? itemCount, ]) async { + if (Logs().level.index < Level.debug.index) { + return func(); + } final start = DateTime.now(); final result = await func(); final milliseconds = @@ -35,6 +38,6 @@ Future runBenchmarked( message += ' ($itemCount items, ${itemCount > 0 ? milliseconds / itemCount : milliseconds} ms/item)'; } - Logs().v(message); + Logs().d(message); return result; } diff --git a/lib/src/utils/sync_update_item_count.dart b/lib/src/utils/sync_update_item_count.dart new file mode 100644 index 00000000..b980faed --- /dev/null +++ b/lib/src/utils/sync_update_item_count.dart @@ -0,0 +1,41 @@ +import 'package:matrix/matrix.dart'; + +extension SyncUpdateItemCount on SyncUpdate { + int get itemCount { + var count = 0; + count += accountData?.length ?? 0; + count += deviceLists?.changed?.length ?? 0; + count += deviceLists?.left?.length ?? 0; + count += toDevice?.length ?? 0; + count += presence?.length ?? 0; + count += _joinRoomsItemCount; + count += _inviteRoomsItemCount; + count += _leaveRoomsItemCount; + return count; + } + + int get _joinRoomsItemCount => + rooms?.join?.values.fold( + 0, + (prev, room) => + prev + + (room.accountData?.length ?? 0) + + (room.state?.length ?? 0) + + (room.timeline?.events?.length ?? 0)) ?? + 0; + + int get _inviteRoomsItemCount => + rooms?.invite?.values.fold( + 0, (prev, room) => prev + (room.inviteState?.length ?? 0)) ?? + 0; + + int get _leaveRoomsItemCount => + rooms?.leave?.values.fold( + 0, + (prev, room) => + prev + + (room.accountData?.length ?? 0) + + (room.state?.length ?? 0) + + (room.timeline?.events?.length ?? 0)) ?? + 0; +}