压力测试优化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

@@ -1,17 +1,22 @@
using Logger;
using System;
using System.Collections.Concurrent;
using System.Windows.Media;
namespace UIShare.GlobalVariable
{
/// <summary>
/// 作用域日志分发器:根据显式传入的 scope 参数把日志路由到对应
/// <see cref="ScopedContext.LogProgress"/>,实现每个台架/作用域拥有独立 LogArea。
/// <see cref="ScopedContext.LogBuffer"/>,实现每个台架/作用域拥有独立 LogArea。
/// 直接写入 ConcurrentQueue不走 Progress&lt;T&gt; / SynchronizationContext避免 UI 线程压力。
/// </summary>
public class ScopeLogDispatcher : IProgress<(string scope, string message, string color, int depth)>
{
private readonly GlobalInfo _globalInfo;
/// <summary>Brush 缓存,避免每次都 new BrushConverter</summary>
private static readonly ConcurrentDictionary<string, Brush> _brushCache = new();
public ScopeLogDispatcher(GlobalInfo globalInfo)
{
_globalInfo = globalInfo ?? throw new ArgumentNullException(nameof(globalInfo));
@@ -23,19 +28,21 @@ namespace UIShare.GlobalVariable
if (string.IsNullOrEmpty(scope)) return;
if (!_globalInfo.ContextDic.TryGetValue(scope, out var context)) return;
if (context?.LogProgress == null) return;
Brush? brush = null;
try
Brush brush = _brushCache.GetOrAdd(value.color, color =>
{
brush = (Brush)new BrushConverter().ConvertFromString(value.color);
}
catch
{
brush = Brushes.Black;
}
try
{
return (Brush)new BrushConverter().ConvertFromString(color);
}
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 MaterialDesignThemes.Wpf;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
@@ -32,8 +33,12 @@ namespace UIShare.GlobalVariable
public List<IBaseInterface> DeviceList { get; set; } = new();
/// <summary>当前作用域的日志接收器LogAreaViewModel 订阅此处实现按作用域隔离日志。</summary>
public IProgress<(string Message, Brush Color, int Depth)>? LogProgress { get; set; }
/// <summary>
/// 日志缓冲队列后台线程ScopeLogDispatcher直接入队
/// UI 线程DispatcherTimer定时出队并刷新到 ObservableCollection。
/// 不走 Progress&lt;T&gt; / SynchronizationContext避免 Post 淹没 UI 消息队列。
/// </summary>
public ConcurrentQueue<(string Message, Brush Color, int Depth)> LogBuffer { get; } = new();
// 【新增测试属性】:每个实例被 new 出来时独一无二的随机身份
// 证 ID