-
Notifications
You must be signed in to change notification settings - Fork 2
/
Measure-CommandPerformance.ps1
50 lines (36 loc) · 1.25 KB
/
Measure-CommandPerformance.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
##############################################################################
##
## Measure-CommandPerformance
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
##############################################################################
<#
.SYNOPSIS
Measures the average time of a command, accounting for natural variability by
automatically ignoring the top and bottom ten percent.
.EXAMPLE
PS > Measure-CommandPerformance.ps1 { Start-Sleep -m 300 }
Count : 30
Average : 312.10155
(...)
#>
param(
## The command to measure
[Scriptblock] $Scriptblock,
## The number of times to measure the command's performance
[int] $Iterations = 30
)
Set-StrictMode -Version 3
## Figure out how many extra iterations we need to account for the outliers
$buffer = [int] ($iterations * 0.1)
$totalIterations = $iterations + (2 * $buffer)
## Get the results
$results = 1..$totalIterations |
Foreach-Object { Measure-Command $scriptblock }
## Sort the results, and skip the outliers
$middleResults = $results | Sort TotalMilliseconds |
Select -Skip $buffer -First $iterations
## Show the average
$middleResults | Measure-Object -Average TotalMilliseconds