powershell 性能测试小脚本

powershell 性能测试

转载请注明:

仰望高端玩家的小清新 http://www.cnblogs.com/luruiyuan/

1. 将待测试的脚本封装在代码块中

2. 使用  Get-ChildItem variable:"变量名规则" 获取待测试的代码块

3. 使用 ForEach-Object (别名为 % )块遍历待测试的所有代码块

4. Measure-Command -Expression 执行并记录时间,select TotalSeconds 记录执行时间

示例如下:

比较两种for循环的速度:

 1 # 测试性能示例
 2 $array = 1..100000
 3 $len = $array.Count
 4 
 5 $block1 = { foreach($i in 1..$len){ $i } }
 6 $block2 = { for($i = 0; $i -lt $len; $i++){ $i } }
 7 
 8 # variable:block* 中间不能有空格
 9 # Measure-Command -Expression 大括号中的 1..num 代表测试块被执行的次数
10 # select TotalSeconds 显示执行 num 次后的总时间
11 Get-ChildItem variable:block* | %{
12     echo $_
13     Measure-Command -Expression {1..100000 | % { $_ }} | select TotalSeconds
14 }

运行效果如下图:

原文地址:https://www.cnblogs.com/luruiyuan/p/8302678.html