压力测试优化1

This commit is contained in:
hsc
2026-06-25 16:23:48 +08:00
parent 45df76b06d
commit 1b4d8ea0de
4 changed files with 71 additions and 32 deletions

View File

@@ -3,7 +3,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- 定义目标 --> <!-- 定义目标 -->
<targets> <targets async="true">
<!-- SQL 日志 --> <!-- SQL 日志 -->
<target name="sqlLog" xsi:type="File" <target name="sqlLog" xsi:type="File"
fileName="Logs/${shortdate}_sql.txt" fileName="Logs/${shortdate}_sql.txt"

View File

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

View File

@@ -1,17 +1,22 @@
using Logger; using Logger;
using System; using System;
using System.Collections.Concurrent;
using System.Windows.Media; using System.Windows.Media;
namespace UIShare.GlobalVariable namespace UIShare.GlobalVariable
{ {
/// <summary> /// <summary>
/// 作用域日志分发器:根据显式传入的 scope 参数把日志路由到对应 /// 作用域日志分发器:根据显式传入的 scope 参数把日志路由到对应
/// <see cref="ScopedContext.LogProgress"/>,实现每个台架/作用域拥有独立 LogArea。 /// <see cref="ScopedContext.LogBuffer"/>,实现每个台架/作用域拥有独立 LogArea。
/// 直接写入 ConcurrentQueue不走 Progress&lt;T&gt; / SynchronizationContext避免 UI 线程压力。
/// </summary> /// </summary>
public class ScopeLogDispatcher : IProgress<(string scope, string message, string color, int depth)> public class ScopeLogDispatcher : IProgress<(string scope, string message, string color, int depth)>
{ {
private readonly GlobalInfo _globalInfo; private readonly GlobalInfo _globalInfo;
/// <summary>Brush 缓存,避免每次都 new BrushConverter</summary>
private static readonly ConcurrentDictionary<string, Brush> _brushCache = new();
public ScopeLogDispatcher(GlobalInfo globalInfo) public ScopeLogDispatcher(GlobalInfo globalInfo)
{ {
_globalInfo = globalInfo ?? throw new ArgumentNullException(nameof(globalInfo)); _globalInfo = globalInfo ?? throw new ArgumentNullException(nameof(globalInfo));
@@ -23,19 +28,21 @@ namespace UIShare.GlobalVariable
if (string.IsNullOrEmpty(scope)) return; if (string.IsNullOrEmpty(scope)) return;
if (!_globalInfo.ContextDic.TryGetValue(scope, out var context)) return; if (!_globalInfo.ContextDic.TryGetValue(scope, out var context)) return;
if (context?.LogProgress == null) return;
Brush? brush = null; Brush brush = _brushCache.GetOrAdd(value.color, color =>
try
{ {
brush = (Brush)new BrushConverter().ConvertFromString(value.color); try
} {
catch return (Brush)new BrushConverter().ConvertFromString(color);
{ }
brush = Brushes.Black; catch
} {
return Brushes.Black;
}
});
context.LogProgress.Report((value.message, brush, value.depth)); // 直接入队到后台线程安全的 ConcurrentQueue不走 SynchronizationContext
context.LogBuffer.Enqueue((value.message, brush, value.depth));
} }
} }
} }

View File

@@ -1,6 +1,7 @@
using DeviceCommand.Base; using DeviceCommand.Base;
using MaterialDesignThemes.Wpf; using MaterialDesignThemes.Wpf;
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Diagnostics; using System.Diagnostics;
@@ -32,8 +33,12 @@ namespace UIShare.GlobalVariable
public List<IBaseInterface> DeviceList { get; set; } = new(); public List<IBaseInterface> DeviceList { get; set; } = new();
/// <summary>当前作用域的日志接收器LogAreaViewModel 订阅此处实现按作用域隔离日志。</summary> /// <summary>
public IProgress<(string Message, Brush Color, int Depth)>? LogProgress { get; set; } /// 日志缓冲队列后台线程ScopeLogDispatcher直接入队
/// UI 线程DispatcherTimer定时出队并刷新到 ObservableCollection。
/// 不走 Progress&lt;T&gt; / SynchronizationContext避免 Post 淹没 UI 消息队列。
/// </summary>
public ConcurrentQueue<(string Message, Brush Color, int Depth)> LogBuffer { get; } = new();
// 【新增测试属性】:每个实例被 new 出来时独一无二的随机身份 // 【新增测试属性】:每个实例被 new 出来时独一无二的随机身份
// 证 ID // 证 ID