更新数据库
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:im_app/app/di/db_provider.dart';
|
||||
import 'package:im_app/data/local/drift/app_database.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
part 'chat_db_test_view_model.g.dart';
|
||||
|
||||
class TestResult {
|
||||
final String title;
|
||||
final String subtitle;
|
||||
final String duration;
|
||||
|
||||
TestResult({
|
||||
required this.title,
|
||||
required this.subtitle,
|
||||
required this.duration,
|
||||
});
|
||||
}
|
||||
|
||||
class ChatDbTestState {
|
||||
final bool testStarted;
|
||||
final List<TestResult> testResults;
|
||||
final String currentState;
|
||||
|
||||
const ChatDbTestState({
|
||||
this.testStarted = false,
|
||||
this.testResults = const [],
|
||||
this.currentState = '',
|
||||
});
|
||||
|
||||
ChatDbTestState copyWith({
|
||||
bool? testStarted,
|
||||
List<TestResult>? testResults,
|
||||
String? currentState,
|
||||
}) => ChatDbTestState(
|
||||
testStarted: testStarted ?? this.testStarted,
|
||||
testResults: testResults ?? this.testResults,
|
||||
currentState: currentState ?? this.currentState,
|
||||
);
|
||||
}
|
||||
|
||||
@riverpod
|
||||
class ChatDbTestViewModel extends _$ChatDbTestViewModel {
|
||||
|
||||
@override
|
||||
ChatDbTestState build() {
|
||||
// 这里就是 onInit
|
||||
final List<TestResult> testResults = List.generate(
|
||||
1000,
|
||||
(i) => TestResult(
|
||||
title: '用户 ${Random().nextInt(9999)}',
|
||||
subtitle: 'uid: ${Random().nextInt(999999)}',
|
||||
duration: '${Random().nextInt(500)}ms',
|
||||
),
|
||||
);
|
||||
return ChatDbTestState(testResults: testResults);
|
||||
}
|
||||
|
||||
// ── 导航(Demo 按钮,正式开发后随 UI 一并替换) ──────────────────────────
|
||||
|
||||
/// 开始测试
|
||||
void startDBTest(BuildContext context) {
|
||||
state = state.copyWith(testStarted: true, currentState: '开始测试');
|
||||
_testDBInsert();
|
||||
}
|
||||
|
||||
/// 结束测试
|
||||
void stopDBTest(BuildContext context) {
|
||||
state = state.copyWith(testStarted: false, currentState: '结束测试');
|
||||
}
|
||||
|
||||
Future<void> _testDBInsert() async {
|
||||
final db = ref.read(storageSdkProvider);
|
||||
const count = 10000;
|
||||
const chunkSize = 50;
|
||||
final stopwatch = Stopwatch()..start();
|
||||
|
||||
debugPrint('开始测试: $count 条,每批 $chunkSize 条');
|
||||
|
||||
int completed = 0;
|
||||
|
||||
for (var i = 0; i < count; i += chunkSize) {
|
||||
final chunk = List.generate(
|
||||
chunkSize.clamp(0, count - i),
|
||||
(j) => UsersCompanion.insert(
|
||||
uid: Value(i + j),
|
||||
nickname: Value('User ${i + j}'),
|
||||
),
|
||||
);
|
||||
|
||||
await db.batchInsertOrReplace<User>(chunk);
|
||||
completed += chunk.length;
|
||||
|
||||
// 让出主线程
|
||||
await Future.delayed(Duration.zero);
|
||||
|
||||
debugPrint('已完成: $completed / $count (${stopwatch.elapsedMilliseconds}ms)');
|
||||
|
||||
// 更新 UI 状态
|
||||
if (ref.mounted) {
|
||||
state = state.copyWith(
|
||||
currentState: '已插入 $completed / $count 条',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
debugPrint('全部完成: ${stopwatch.elapsedMilliseconds}ms');
|
||||
if (ref.mounted) {
|
||||
state = state.copyWith(
|
||||
testStarted: false,
|
||||
currentState: '完成!共 $count 条,耗时 ${stopwatch.elapsedMilliseconds}ms',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -55,6 +55,11 @@ class ChatViewModel extends _$ChatViewModel {
|
||||
context.go(AppRouteName.settings.path);
|
||||
}
|
||||
|
||||
/// 测试数据库性能
|
||||
void goToDatabaseTest(BuildContext context) {
|
||||
context.push(AppRouteName.chatDBTest.path);
|
||||
}
|
||||
|
||||
// ── 业务 ─────────────────────────────────────────────────────────────────
|
||||
|
||||
/// 退出登录
|
||||
|
||||
Reference in New Issue
Block a user