数据和测试案例按照架构来处理

This commit is contained in:
Happi (哈比)
2026-03-09 14:59:29 +08:00
parent 7b78da86e7
commit fe54f79b21
6 changed files with 147 additions and 32 deletions

View File

@@ -1,8 +1,8 @@
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:im_app/app/di/user_provider.dart';
import 'package:im_app/domain/entities/user.dart';
import 'package:im_app/domain/presentation/di/user_provider.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'chat_db_test_view_model.g.dart';
@@ -101,42 +101,38 @@ class ChatDbTestViewModel extends _$ChatDbTestViewModel {
}
Future<void> _testDBInsert() async {
final useCase = ref.read(insertUsersUseCaseProvider);
final repo = ref.read(userRepositoryProvider);
const count = 10000;
const chunkSize = 200;
final stopwatch = Stopwatch()..start();
final baseUid = DateTime.now().microsecondsSinceEpoch;
debugPrint('开始测试: $count 条,每批 $chunkSize');
debugPrint('开始测试: $count');
final workingList = List<User>.from(state.users);
int completed = 0;
while (completed < count) {
if (!_isTesting) break;
final allUsers = List.generate(
count,
(i) => User(uid: baseUid + i, nickname: 'User ${_random.nextInt(9999)}'),
);
final chunk = List.generate(
min(chunkSize, count - completed),
(_) => User(
uid: _random.nextInt(999999),
nickname: 'User ${_random.nextInt(9999)}',
),
);
await useCase.execute(
allUsers,
onProgress: (completed, total, chunk) {
workingList.addAll(chunk);
await repo.saveUsers(chunk);
completed += chunk.length;
workingList.addAll(chunk);
debugPrint(
'已完成: $completed / $count (${stopwatch.elapsedMilliseconds}ms)',
);
if (ref.mounted) {
state = state.copyWith(
users: List.unmodifiable(workingList),
currentState: '已插入 $completed / $count',
debugPrint(
'已完成: $completed / $total (${stopwatch.elapsedMilliseconds}ms)',
);
}
}
if (ref.mounted && _isTesting) {
state = state.copyWith(
users: List.unmodifiable(workingList),
currentState: '已插入 $completed / $total',
);
}
},
);
_isTesting = false;
final elapsed = stopwatch.elapsedMilliseconds;