压力测试优化1
This commit is contained in:
@@ -3,18 +3,17 @@ using Logger;
|
||||
using Prism.Ioc;
|
||||
using Prism.Mvvm;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Reflection;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Threading;
|
||||
using UIShare.ViewModelBase;
|
||||
|
||||
namespace TestingModule.ViewModels
|
||||
{
|
||||
public class LogAreaViewModel : NavigateViewModelBase, IDisposable
|
||||
{
|
||||
|
||||
|
||||
// 日志集合
|
||||
private ObservableCollection<LogItem> _logs = new();
|
||||
|
||||
@@ -23,11 +22,14 @@ namespace TestingModule.ViewModels
|
||||
get => _logs;
|
||||
set => SetProperty(ref _logs, value);
|
||||
}
|
||||
|
||||
public ICommand ClearLogCommand { get; set; }
|
||||
|
||||
private readonly ScopedContext _scopedContext;
|
||||
private readonly GlobalInfo _globalInfo;
|
||||
private IProgress<(string Message, Brush Color, int Depth)>? _logProgress;
|
||||
|
||||
/// <summary>UI 线程定时器,每 100ms 批量从 LogBuffer 出队并刷新</summary>
|
||||
private readonly DispatcherTimer _flushTimer;
|
||||
|
||||
public LogAreaViewModel(IContainerProvider containerProvider) : base(containerProvider)
|
||||
{
|
||||
@@ -35,17 +37,39 @@ namespace TestingModule.ViewModels
|
||||
_scopedContext = containerProvider.Resolve<ScopedContext>();
|
||||
_globalInfo = containerProvider.Resolve<GlobalInfo>();
|
||||
|
||||
// 创建本作用域专属的日志接收器并注册到 ScopedContext
|
||||
_logProgress = new Progress<(string Message, Brush Color, int Depth)>(log =>
|
||||
// UI 线程定时器:每 100ms 从 ScopedContext.LogBuffer 批量出队并 Add
|
||||
_flushTimer = new DispatcherTimer(DispatcherPriority.Normal, System.Windows.Application.Current.Dispatcher)
|
||||
{
|
||||
if (Logs == null) return;
|
||||
Logs.Add(new LogItem(log.Message, log.Color, log.Depth));
|
||||
});
|
||||
_scopedContext.LogProgress = _logProgress;
|
||||
Interval = TimeSpan.FromMilliseconds(100)
|
||||
};
|
||||
_flushTimer.Tick += FlushLogBuffer;
|
||||
_flushTimer.Start();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 定时批量刷新日志:一次性出队并 Add,WPF 对连续 Add 做合并渲染
|
||||
/// </summary>
|
||||
private void FlushLogBuffer(object? sender, EventArgs e)
|
||||
{
|
||||
if (_scopedContext.LogBuffer.IsEmpty || Logs == null) return;
|
||||
|
||||
var batch = new List<(string Message, Brush Color, int Depth)>();
|
||||
while (_scopedContext.LogBuffer.TryDequeue(out var item))
|
||||
{
|
||||
batch.Add(item);
|
||||
}
|
||||
|
||||
// 批量 Add,WPF 会对连续 Add 做合并渲染,只触发一次 layout/render pass
|
||||
foreach (var item in batch)
|
||||
{
|
||||
Logs.Add(new LogItem(item.Message, item.Color, item.Depth));
|
||||
}
|
||||
}
|
||||
|
||||
private void ClearLog()
|
||||
{
|
||||
// 清空缓冲区,防止定时器下次还把旧数据刷出来
|
||||
while (_scopedContext.LogBuffer.TryDequeue(out _)) { }
|
||||
Logs?.Clear();
|
||||
}
|
||||
|
||||
@@ -56,12 +80,12 @@ namespace TestingModule.ViewModels
|
||||
{
|
||||
try
|
||||
{
|
||||
// 注销本作用域的日志接收器,避免 Dispose 后仍收到旧消息
|
||||
if (_scopedContext != null && _scopedContext.LogProgress == _logProgress)
|
||||
{
|
||||
_scopedContext.LogProgress = null;
|
||||
}
|
||||
_logProgress = null;
|
||||
// 停止定时器
|
||||
_flushTimer.Stop();
|
||||
_flushTimer.Tick -= FlushLogBuffer;
|
||||
|
||||
// 最后一次刷新缓冲区中的剩余日志
|
||||
FlushLogBuffer(null, EventArgs.Empty);
|
||||
|
||||
if (Logs != null)
|
||||
{
|
||||
@@ -71,7 +95,9 @@ namespace TestingModule.ViewModels
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LoggerHelper.ErrorWithNotify(_scopedContext != null ? _globalInfo.CurrentScope : "", $"释放日志组件(LogAreaViewModel)资源失败: {ex.Message}");
|
||||
Logger.LoggerHelper.ErrorWithNotify(
|
||||
_scopedContext != null ? _globalInfo.CurrentScope : "",
|
||||
$"释放日志组件(LogAreaViewModel)资源失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -81,7 +107,8 @@ namespace TestingModule.ViewModels
|
||||
{
|
||||
public string Message { get; set; }
|
||||
public Brush Color { get; set; } = Brushes.Black;
|
||||
public int Depth { get; set; }
|
||||
public int Depth { get; set; }
|
||||
|
||||
public LogItem(string message, Brush color, int depth = 0)
|
||||
{
|
||||
Message = new string(' ', depth * 20) + message;
|
||||
|
||||
Reference in New Issue
Block a user