feat: Calc benchmarks for hive operations on init

This commit is contained in:
Krille Fear 2021-10-06 10:33:22 +02:00
parent a7818bbd0f
commit 3603dae312
2 changed files with 165 additions and 116 deletions

View File

@ -29,6 +29,7 @@ import 'dart:typed_data';
import 'package:matrix/matrix.dart';
import 'package:matrix/src/utils/queued_to_device_event.dart';
import 'package:hive/hive.dart';
import 'package:matrix/src/utils/run_benchmarked.dart';
/// This is a basic database for the Matrix SDK using the hive store. You need
/// to make sure that you perform `Hive.init()` or `Hive.flutterInit()` before
@ -319,7 +320,9 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
}
@override
Future<Map<String, BasicEvent>> getAccountData() async {
Future<Map<String, BasicEvent>> getAccountData() =>
runBenchmarked<Map<String, BasicEvent>>('Get all account data from Hive',
() async {
final accountData = <String, BasicEvent>{};
for (final key in _accountDataBox.keys) {
final raw = await _accountDataBox.get(key);
@ -329,10 +332,11 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
);
}
return accountData;
}
}, _accountDataBox.keys.length);
@override
Future<Map<String, dynamic>?> getClient(String name) async {
Future<Map<String, dynamic>?> getClient(String name) =>
runBenchmarked('Get Client from Hive', () async {
final map = <String, dynamic>{};
for (final key in _clientBox.keys) {
if (key == 'version') continue;
@ -340,7 +344,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
}
if (map.isEmpty) return null;
return map;
}
});
@override
Future<Event?> getEventById(String eventId, Room room) async {
@ -482,7 +486,8 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
}
@override
Future<List<Room>> getRoomList(Client client) async {
Future<List<Room>> getRoomList(Client client) =>
runBenchmarked<List<Room>>('Get room list from hive', () async {
final rooms = <String, Room>{};
final importantRoomStates = client.importantStateEvents;
for (final key in _roomsBox.keys) {
@ -521,8 +526,8 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
// Get the "important" room states. All other states will be loaded once
// `getUnimportantRoomStates()` is called.
for (final type in importantRoomStates) {
final states =
await _roomStateBox.get(MultiKey(room.id, type).toString()) as Map?;
final states = await _roomStateBox
.get(MultiKey(room.id, type).toString()) as Map?;
if (states == null) continue;
final stateEvents = states.values
.map((raw) => Event.fromJson(convertToJson(raw), room))
@ -544,15 +549,17 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
final basicRoomEvent = BasicRoomEvent.fromJson(
convertToJson(raw),
);
rooms[roomId]!.roomAccountData[basicRoomEvent.type] = basicRoomEvent;
rooms[roomId]!.roomAccountData[basicRoomEvent.type] =
basicRoomEvent;
} else {
Logs().w('Found account data for unknown room $roomId. Delete now...');
Logs().w(
'Found account data for unknown room $roomId. Delete now...');
await _roomAccountDataBox.delete(key);
}
}
return rooms.values.toList();
}
}, _roomsBox.keys.length);
@override
Future<SSSSCache?> getSSSSCache(String type) async {
@ -595,7 +602,9 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
}
@override
Future<Map<String, DeviceKeysList>> getUserDeviceKeys(Client client) async {
Future<Map<String, DeviceKeysList>> getUserDeviceKeys(Client client) =>
runBenchmarked<Map<String, DeviceKeysList>>(
'Get all user device keys from Hive', () async {
final deviceKeysOutdated = _userDeviceKeysOutdatedBox.keys;
if (deviceKeysOutdated.isEmpty) {
return {};
@ -617,14 +626,14 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
'user_id': userId,
'outdated': await _userDeviceKeysOutdatedBox.get(userId),
},
await Future.wait(deviceKeysBoxKeys.map(
(key) async => convertToJson(await _userDeviceKeysBox.get(key)))),
await Future.wait(deviceKeysBoxKeys.map((key) async =>
convertToJson(await _userDeviceKeysBox.get(key)))),
await Future.wait(crossSigningKeysBoxKeys.map((key) async =>
convertToJson(await _userCrossSigningKeysBox.get(key)))),
client);
}
return res;
}
}, _userDeviceKeysBox.keys.length);
@override
Future<List<User>> getUsers(Room room) async {

View File

@ -0,0 +1,40 @@
/*
* Famedly Matrix SDK
* Copyright (C) 2019, 2020 Famedly GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import 'package:matrix/matrix.dart';
/// Calculates some benchmarks for this function. Give it a [name] and a [func]
/// to call and it will calculate the needed milliseconds. Give it an optional
/// [itemCount] to let it also calculate the needed milliseconds per item.
Future<T> runBenchmarked<T>(
String name,
Future<T> Function() func, [
int? itemCount,
]) async {
final start = DateTime.now();
final result = await func();
final milliseconds =
DateTime.now().millisecondsSinceEpoch - start.millisecondsSinceEpoch;
var message = 'Benchmark: $name -> $milliseconds ms';
if (itemCount != null) {
message +=
' ($itemCount items, ${itemCount > 0 ? milliseconds / itemCount : milliseconds} ms/item)';
}
Logs().v(message);
return result;
}