You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PowerShell汇总XML文件中各函数的运行时长

解决PowerShell统计XML中操作耗时的问题

你的命令核心逻辑没问题,但问题出在XML节点的属性是XmlAttribute类型,直接用operationTimeInSeconds传给Measure-Object时无法正确识别为数值求和,且PowerShell默认输出规则可能只展示了Name列。

修正后的命令

Select-Xml -Path ".\PerformanceTracking.xml" -XPath "//*[@operationName]" | 
    Select-Object -ExpandProperty Node | 
    ForEach-Object {
        # 将XML属性转换为数值类型,确保后续计算正常
        [PSCustomObject]@{
            operationName = $_.operationName
            operationTime = [double]$_.operationTimeInSeconds
        }
    } | 
    Group-Object operationName | 
    ForEach-Object {
        [PSCustomObject]@{
            函数名称 = $_.Name
            调用次数 = $_.Count
            总耗时(秒) = ($_.Group | Measure-Object operationTime -Sum).Sum
        }
    } | Format-Table -AutoSize

关键修改说明

  1. 新增转换步骤:通过ForEach-Object把XML节点的属性提取出来,将耗时属性转为double数值类型,生成标准的PSCustomObject,让Measure-Object能正确计算总和。
  2. 强制列展示:最后用Format-Table -AutoSize确保所有列(函数名称、调用次数、总耗时)都显示出来,避免PowerShell默认只显示部分列的问题。

示例测试

假设你的XML结构如下:

<root>
    <operation operationName="IsNullOrEmpty" operationTimeInSeconds="0.078"/>
    <parent>
        <operation operationName="IsNullOrEmpty" operationTimeInSeconds="1"/>
        <operation operationName="CalculateHash" operationTimeInSeconds="0.5"/>
    </parent>
</root>

执行修正后的命令会输出:

函数名称       调用次数 总耗时(秒)
----------       -------- -----------
IsNullOrEmpty        2        1.078
CalculateHash        1         0.5

内容的提问来源于stack exchange,提问作者MakePeaceGreatAgain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 05:42:42