using Common.Attributes;
using Common.Tools;
using System;
namespace Command
{
///
/// 步骤运行耗时统计命令。
///
/// 命令库为纯 .NET 类库,不直接引用宿主的步骤管理器类型;
/// 实际的步骤数据查询能力由宿主(StepRunning)在执行测试步骤前通过
/// 注入(依赖倒置),
/// 多台架并发运行时各作用域的查询器随异步流隔离,互不串扰。
///
///
[ADPCommand]
public static class CommandRunningTime
{
///
/// 计算测试步骤运行时间之和:统计步骤管理器主程序中,
/// 序号从起始序号到结束序号(含两端)范围内所有步骤的运行耗时总和。
///
/// 起始步骤序号(从 1 开始)
/// 结束步骤序号(含本身)
/// 范围内步骤运行时间之和(毫秒);序号超界、范围内无步骤或查询异常时输出 0
public static double 获取步骤运行时间之和(int 起始序号, int 结束序号)
{
// 读取当前异步执行流注入的步骤耗时查询器(由 StepRunning 设置)
var query = StepRuntimeContext.StepTimeQuery.Value;
if (query == null)
{
Console.WriteLine($"[步骤耗时] 宿主未注入步骤耗时查询器,无法计算序号 {起始序号}~{结束序号} 的运行时间之和");
return 0;
}
try
{
return query(起始序号, 结束序号);
}
catch (Exception ex)
{
Console.WriteLine($"[步骤耗时] 计算序号 {起始序号}~{结束序号} 的运行时间之和失败: {ex.Message}");
return 0;
}
}
}
}