Merge branch 'krille/benchmarks' into 'main'
feat: Add more benchmarks for sync, timeline, init See merge request famedly/company/frontend/famedlysdk!876
This commit is contained in:
commit
e61faec722
|
|
@ -23,6 +23,7 @@ import 'dart:typed_data';
|
||||||
|
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
import 'package:matrix/src/utils/run_in_root.dart';
|
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:mime/mime.dart';
|
||||||
import 'package:olm/olm.dart' as olm;
|
import 'package:olm/olm.dart' as olm;
|
||||||
import 'package:collection/collection.dart' show IterableExtension;
|
import 'package:collection/collection.dart' show IterableExtension;
|
||||||
|
|
@ -38,6 +39,7 @@ import 'utils/device_keys_list.dart';
|
||||||
import 'utils/event_update.dart';
|
import 'utils/event_update.dart';
|
||||||
import 'utils/http_timeout.dart';
|
import 'utils/http_timeout.dart';
|
||||||
import 'utils/matrix_file.dart';
|
import 'utils/matrix_file.dart';
|
||||||
|
import 'utils/run_benchmarked.dart';
|
||||||
import 'utils/to_device_event.dart';
|
import 'utils/to_device_event.dart';
|
||||||
import 'utils/uia_request.dart';
|
import 'utils/uia_request.dart';
|
||||||
import 'utils/multilock.dart';
|
import 'utils/multilock.dart';
|
||||||
|
|
@ -920,8 +922,12 @@ class Client extends MatrixApi {
|
||||||
throw Exception('User is already logged in! Call [logout()] first!');
|
throw Exception('User is already logged in! Call [logout()] first!');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final databaseBuilder = this.databaseBuilder;
|
||||||
if (databaseBuilder != null) {
|
if (databaseBuilder != null) {
|
||||||
_database ??= await databaseBuilder?.call(this);
|
_database ??= await runBenchmarked<DatabaseApi>(
|
||||||
|
'Build database',
|
||||||
|
() async => await databaseBuilder(this),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
String? olmAccount;
|
String? olmAccount;
|
||||||
|
|
@ -1154,7 +1160,11 @@ class Client extends MatrixApi {
|
||||||
await database.storePrevBatch(syncResp.nextBatch);
|
await database.storePrevBatch(syncResp.nextBatch);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
await _currentTransaction;
|
await runBenchmarked(
|
||||||
|
'Process sync',
|
||||||
|
() async => await _currentTransaction,
|
||||||
|
syncResp.itemCount,
|
||||||
|
);
|
||||||
onSyncStatus.add(SyncStatusUpdate(SyncStatus.cleaningUp));
|
onSyncStatus.add(SyncStatusUpdate(SyncStatus.cleaningUp));
|
||||||
} else {
|
} else {
|
||||||
await _handleSync(syncResp);
|
await _handleSync(syncResp);
|
||||||
|
|
|
||||||
|
|
@ -393,7 +393,8 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
|
||||||
Room room, {
|
Room room, {
|
||||||
int start = 0,
|
int start = 0,
|
||||||
int? limit,
|
int? limit,
|
||||||
}) async {
|
}) =>
|
||||||
|
runBenchmarked<List<Event>>('Get event list', () async {
|
||||||
// Get the synced event IDs from the store
|
// Get the synced event IDs from the store
|
||||||
final timelineKey = MultiKey(room.id, '').toString();
|
final timelineKey = MultiKey(room.id, '').toString();
|
||||||
final timelineEventIds =
|
final timelineEventIds =
|
||||||
|
|
@ -406,19 +407,20 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
|
||||||
} else {
|
} else {
|
||||||
final sendingTimelineKey = MultiKey(room.id, 'SENDING').toString();
|
final sendingTimelineKey = MultiKey(room.id, 'SENDING').toString();
|
||||||
sendingEventIds =
|
sendingEventIds =
|
||||||
(await _timelineFragmentsBox.get(sendingTimelineKey) as List? ?? []);
|
(await _timelineFragmentsBox.get(sendingTimelineKey) as List? ??
|
||||||
|
[]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Combine those two lists while respecting the start and limit parameters.
|
// Combine those two lists while respecting the start and limit parameters.
|
||||||
final end = min(
|
final end = min(timelineEventIds.length,
|
||||||
timelineEventIds.length, start + (limit ?? timelineEventIds.length));
|
start + (limit ?? timelineEventIds.length));
|
||||||
final eventIds = sendingEventIds +
|
final eventIds = sendingEventIds +
|
||||||
(start < timelineEventIds.length
|
(start < timelineEventIds.length
|
||||||
? timelineEventIds.getRange(start, end).toList()
|
? timelineEventIds.getRange(start, end).toList()
|
||||||
: []);
|
: []);
|
||||||
|
|
||||||
return await _getEventsByIds(eventIds.cast<String>(), room);
|
return await _getEventsByIds(eventIds.cast<String>(), room);
|
||||||
}
|
});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<Uint8List?> getFile(Uri mxcUri) async {
|
Future<Uint8List?> getFile(Uri mxcUri) async {
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,9 @@ Future<T> runBenchmarked<T>(
|
||||||
Future<T> Function() func, [
|
Future<T> Function() func, [
|
||||||
int? itemCount,
|
int? itemCount,
|
||||||
]) async {
|
]) async {
|
||||||
|
if (Logs().level.index < Level.debug.index) {
|
||||||
|
return func();
|
||||||
|
}
|
||||||
final start = DateTime.now();
|
final start = DateTime.now();
|
||||||
final result = await func();
|
final result = await func();
|
||||||
final milliseconds =
|
final milliseconds =
|
||||||
|
|
@ -35,6 +38,6 @@ Future<T> runBenchmarked<T>(
|
||||||
message +=
|
message +=
|
||||||
' ($itemCount items, ${itemCount > 0 ? milliseconds / itemCount : milliseconds} ms/item)';
|
' ($itemCount items, ${itemCount > 0 ? milliseconds / itemCount : milliseconds} ms/item)';
|
||||||
}
|
}
|
||||||
Logs().v(message);
|
Logs().d(message);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue