压力测试优化2

This commit is contained in:
hsc
2026-06-26 10:04:57 +08:00
parent f83c93fcf9
commit 52edf16176
4 changed files with 51 additions and 10 deletions

View File

@@ -18,7 +18,7 @@ using static UIShare.UIViewModel.ParameterVM;
namespace UIShare
{
public class StepRunning
public class StepRunning:IDisposable
{
private ScopedContext _scopedContext;
private SystemConfig _systemConfig;
@@ -39,6 +39,9 @@ namespace UIShare
public CancellationTokenSource errorStepCTS = new();
private bool SubSingleStep = false;
/// <summary>标记是否已被注销,执行方法据此安全退出</summary>
private volatile bool _disposed = false;
private Guid TestRoundID;
public StepRunning(ScopedContext ScopedContext, SystemConfig systemConfig,IEventAggregator eventAggregator, DeviceManager deviceManager)
{
@@ -50,6 +53,7 @@ namespace UIShare
}
public async Task<bool> ExecuteErrorSteps(ProgramVM program, int depth = 0, CancellationToken cancellationToken = default)
{
if (_disposed) return false;
int index = 0;
bool stepSuccess = false;
if (depth == 0)
@@ -67,7 +71,7 @@ namespace UIShare
while (index < program.ErrorStepCollection.Count)
{
if (cancellationToken.IsCancellationRequested)
if (_disposed || cancellationToken.IsCancellationRequested)
{
break;
}
@@ -215,6 +219,7 @@ namespace UIShare
}
public async Task<bool> ExecuteSteps(ProgramVM program, int depth = 0, CancellationToken cancellationToken = default)
{
if (_disposed) return false;
int index = 0;
bool stepSuccess = false;
if (depth == 0)
@@ -232,11 +237,11 @@ namespace UIShare
while (index < program.StepCollection.Count)
{
while (_scopedContext.IsStop == true)
while (!_disposed && _scopedContext.IsStop == true)
{
await Task.Delay(50);
await Task.Delay(50, cancellationToken);
}
if (cancellationToken.IsCancellationRequested)
if (_disposed || cancellationToken.IsCancellationRequested)
{
break;
}
@@ -392,11 +397,12 @@ namespace UIShare
public async Task ExecuteMethodStep(StepVM step, Dictionary<Guid, ParameterVM> parameters, int depth, CancellationToken cancellationToken = default)
{
if (_disposed) return;
try
{
if(_scopedContext.Program.StepCollection.Count>1)
_scopedContext.SelectedStep = null;
await Task.Delay(_systemConfig.PerformanceLevel);
await Task.Delay(_systemConfig.PerformanceLevel, cancellationToken);
// 1. 查找类型
@@ -711,7 +717,41 @@ namespace UIShare
}
}
public void Dispose()
{
if (_disposed) return;
_disposed = true;
// 1. 唤醒暂停循环IsStop 可能卡住后台线程)
try
{
if (_scopedContext != null)
{
_scopedContext.IsStop = false;
_scopedContext.IsTerminate = true;
_scopedContext.RunState = "运行";
}
}
catch { }
try
{
if (stepCTS != null && !stepCTS.IsCancellationRequested) stepCTS.Cancel();
}
catch (ObjectDisposedException) { }
try
{
if (errorStepCTS != null && !errorStepCTS.IsCancellationRequested) errorStepCTS.Cancel();
}
catch (ObjectDisposedException) { }
tmpParameters.Clear();
loopStack.Clear();
loopStopwatchStack.Clear();
stepStopwatch.Stop();
}
#region