数据和测试案例按照架构来处理
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user