feat: 新增CommandRunningTime步骤耗时统计命令(序号区间RunTime求和,依赖倒置+AsyncLocal作用域隔离)

This commit is contained in:
2026-09-14 17:08:56 +08:00
parent b1a160d159
commit 186eec4e75
3 changed files with 102 additions and 0 deletions
+28
View File
@@ -73,6 +73,8 @@ namespace UIShare
ResetAllStepStatus(program.ErrorStepCollection);
tmpParameters.Clear();
TestRoundID = Guid.NewGuid();
// 注入步骤耗时查询器:AsyncLocal 随异步执行流流动,命令库(CommandRunningTime)据此查询当前作用域主程序的步骤耗时
StepRuntimeContext.StepTimeQuery.Value = QueryStepRunTimeSum;
}
foreach (var item in program.Parameters)
{
@@ -261,6 +263,8 @@ namespace UIShare
tmpParameters.Clear();
testItemStack.Clear();
TestRoundID = Guid.NewGuid();
// 注入步骤耗时查询器:AsyncLocal 随异步执行流流动,命令库(CommandRunningTime)据此查询当前作用域主程序的步骤耗时
StepRuntimeContext.StepTimeQuery.Value = QueryStepRunTimeSum;
}
int initialLoopStackCount = loopStack.Count;
foreach (var item in program.Parameters)
@@ -800,6 +804,30 @@ namespace UIShare
}
}
/// <summary>
/// 查询当前作用域主程序步骤集合中,序号位于 [起始序号, 结束序号](含两端)范围内步骤的运行时间之和(毫秒)。
/// <para>由 ExecuteSteps / ExecuteErrorSteps 顶层启动时经 StepRuntimeContext 注入(AsyncLocal 随异步流流动),
/// 供命令库 CommandRunningTime.获取步骤运行时间之和 调用(依赖倒置:命令库不引用宿主类型)。</para>
/// <para>序号倒置、超界(范围内不存在步骤)时记录错误日志并返回 0;未执行步骤的耗时按 0 计入。</para>
/// </summary>
private double QueryStepRunTimeSum(int startIndex, int endIndex)
{
var steps = _scopedContext.Program.StepCollection;
if (startIndex > endIndex)
{
LoggerHelper.Error($"[步骤耗时] 查询失败:起始序号 {startIndex} 大于结束序号 {endIndex},返回 0");
return 0;
}
var matched = steps.Where(s => s.Index >= startIndex && s.Index <= endIndex).ToList();
if (matched.Count == 0)
{
LoggerHelper.Error($"[步骤耗时] 查询失败:序号 {startIndex}~{endIndex} 范围内不存在测试步骤(主程序共 {steps.Count} 步),返回 0");
return 0;
}
// 尚未执行或未启用的步骤 RunTime 为 null,按 0 计入
return matched.Sum(s => s.RunTime ?? 0);
}
private void UpdateCurrentStepResult(StepVM step, bool paraResult = true, bool stepResult = true, int depth = 0)
{
if (stepResult && paraResult)