feat: Add more benchmarks for sync, timeline, init

This also disables benchmarks if loglevel is higher than debug.
This commit is contained in:
Krille Fear 2021-11-05 13:47:20 +01:00
parent 0e2542b172
commit 315ed7bdcb
4 changed files with 82 additions and 26 deletions

View File

@ -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<DatabaseApi>(
'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);

View File

@ -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<List<Event>>('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<String>(), room);
}
return await _getEventsByIds(eventIds.cast<String>(), room);
});
@override
Future<Uint8List?> getFile(Uri mxcUri) async {

View File

@ -26,6 +26,9 @@ Future<T> runBenchmarked<T>(
Future<T> 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<T> runBenchmarked<T>(
message +=
' ($itemCount items, ${itemCount > 0 ? milliseconds / itemCount : milliseconds} ms/item)';
}
Logs().v(message);
Logs().d(message);
return result;
}

View File

@ -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<int>(
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<int>(
0, (prev, room) => prev + (room.inviteState?.length ?? 0)) ??
0;
int get _leaveRoomsItemCount =>
rooms?.leave?.values.fold<int>(
0,
(prev, room) =>
prev +
(room.accountData?.length ?? 0) +
(room.state?.length ?? 0) +
(room.timeline?.events?.length ?? 0)) ??
0;
}