forked from jdhitsolutions/PSScriptTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest-Expression.ps1
More file actions
311 lines (254 loc) · 10.3 KB
/
Test-Expression.ps1
File metadata and controls
311 lines (254 loc) · 10.3 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#an internal function for the actual testing
Function _TestMe {
[cmdletbinding(DefaultParameterSetName = "Interval")]
Param(
[scriptblock]$Expression,
[object[]]$ArgumentList,
[ValidateScript( { $_ -ge 1 })]
[int]$Count = 1,
[Parameter(ParameterSetName = "Interval")]
[ValidateRange(0, 60)]
[double]$Interval = .5,
[Parameter(ParameterSetName = "Random", Mandatory)]
[Alias("min")]
[double]$RandomMinimum,
[Parameter(ParameterSetName = "Random", Mandatory)]
[Alias("max")]
[double]$RandomMaximum,
[switch]$IncludeExpression
)
$TestData = 1..$count | ForEach-Object -begin {
<#
PowerShell doesn't seem to like passing a scriptblock as an
argument when using Invoke-Command. It appears to pass it as
a string so I'm recreating it as a scriptblock here.
#>
$script:testblock = [scriptblock]::Create($Expression)
} -process {
#invoke the scriptblock with any arguments and measure
Measure-Command -Expression { $($script:testblock).Invoke(@($argumentlist)) } -OutVariable +out
#} -outvariable +out
#pause to mitigate any caching effects
if ($RandomMinimum -AND $RandomMaximum) {
$sleep = Get-Random -Minimum ($RandomMinimum * 1000) -Maximum ($RandomMaximum * 1000)
$TestInterval = "Random"
}
else {
$Sleep = ($Interval * 1000)
$TestInterval = $Sleep
}
Start-Sleep -Milliseconds $sleep
}
$TestResults = $TestData |
Measure-Object -Property TotalMilliseconds -Average -Maximum -Minimum |
Select-Object -Property @{Name = "Tests"; Expression = { $_.Count } },
@{Name = "TestInterval"; Expression = { $TestInterval } },
@{Name = "AverageMS"; Expression = { $_.Average } },
@{Name = "MinimumMS"; Expression = { $_.Minimum } },
@{Name = "MaximumMS"; Expression = { $_.Maximum } },
@{Name = "MedianMS"; Expression = {
#sort the values to calculate the median and trimmed values
$sort = $out.totalmilliseconds | Sort-Object
#test if there are an even or odd number of elements
if ( ($sort.count) % 2) {
#odd number
#subtract 1 because arrays start counting at 0
$sort[(($sort.count - 1) / 2) -as [int]]
}
else {
#even number
#get middle two numbers and their average
($sort[($sort.count / 2)] + $sort[$sort.count / 2 + 1]) / 2
}
}
},
@{Name = "TrimmedMS"; Expression = {
#values must be sorted in ascending order
$data = $out.totalmilliseconds | Sort-Object
#select elements from the second to next to last
($data[1..($data.count - 2)] | Measure-Object -Average).Average
}
}
#add metadata
$OS = Get-CimInstance -ClassName win32_operatingsystem
$TestResults | Add-Member -MemberType Noteproperty -Name PSVersion -Value $PSVersionTable.PSVersion.ToString()
$TestResults | Add-Member -MemberType Noteproperty -Name OS -Value $OS.caption
if ($IncludeExpression) {
Write-Verbose "Adding expression to output"
$TestResults | Add-Member -MemberType Noteproperty -Name Expression -Value $Expression
$TestResults | Add-Member -MemberType Noteproperty -Name Arguments -Value $ArgumentList
}
Write-Verbose "Inserting a new type name"
$TestResults.psobject.typenames.insert(0, "my.TestResult")
#write the result to the pipeline
$testResults
} #_TestMe function
#exposed functions
Function Test-Expression {
[cmdletbinding(DefaultParameterSetName = "Interval")]
[alias("tex")]
Param(
[Parameter(
Position = 0,
Mandatory,
HelpMessage = "Enter a scriptblock to test",
ValueFromPipeline
)]
[Alias("sb")]
[scriptblock]$Expression,
[object[]]$ArgumentList,
[Parameter(ValueFromPipelineByPropertyName)]
[ValidateScript( { $_ -ge 1 })]
[int]$Count = 1,
[Parameter(
ParameterSetName = "Interval",
ValueFromPipelineByPropertyName)]
[ValidateRange(0, 60)]
[Alias("sleep")]
[double]$Interval = .5,
[Parameter(
ParameterSetName = "Random",
Mandatory
)]
[Alias("min")]
[double]$RandomMinimum,
[Parameter(
ParameterSetName = "Random",
Mandatory
)]
[Alias("max")]
[double]$RandomMaximum,
[Parameter(ValueFromPipelineByPropertyName)]
[Alias("ie")]
[switch]$IncludeExpression,
[switch]$AsJob
)
Write-Verbose "Starting: $($MyInvocation.Mycommand)"
Write-Verbose ($PSBoundParameters | Out-String)
Write-Verbose "Measuring expression:"
Write-Verbose ($Expression | Out-String)
if ($ArgumentList) {
Write-Verbose "Arguments: $($ArgumentList -join ",")"
}
if ($PSCmdlet.ParameterSetName -eq 'Interval') {
Write-Verbose "$Count time(s) with a sleep interval of $interval seconds."
}
else {
Write-Verbose "$Count time(s) with a random sleep interval between $RandomMinimum seconds and $RandomMaximum seconds."
}
If ($AsJob) {
Write-Verbose "Running as a background job"
[void]$PSBoundParameters.remove("AsJob")
Start-Job -ScriptBlock {
Param([hashtable]$Testparams)
<#
PowerShell doesn't seem to like passing a scriptblock as an
argument when using Invoke-Command. It appears to pass it as
a string so I'm recreating it as a scriptblock here.
#>
$expression = [scriptblock]::Create($Testparams.Expression)
$TestParams.Expression = $Expression
Test-Expression @testparams
} -ArgumentList @($PSBoundParameters) -InitializationScript { Import-Module PSScriptTools}
}
else {
[void]$PSBoundParameters.remove("AsJob")
_TestMe @PSBoundParameters
}
Write-Verbose "Ending: $($MyInvocation.Mycommand)"
} #end function
Function Test-ExpressionForm {
[cmdletbinding()]
[alias("texf")]
Param()
if ((Test-IsPSWindows)) {
Add-Type -AssemblyName PresentationFramework
Add-Type -assemblyName PresentationCore
[xml]$xaml = Get-Content $psscriptroot\form.xaml
$reader = New-Object system.xml.xmlnodereader $xaml
$form = [windows.markup.xamlreader]::Load($reader)
$sb = $form.FindName("txtScriptBlock")
$count = $form.FindName("txtCount")
$results = $form.FindName("tbResults")
$slider = $form.Findname("sliderStatic")
$radioStatic = $form.FindName("radioStatic")
$radioRandom = $form.FindName("radioRandom")
$min = $form.FindName("txtMin")
$Max = $form.FindName("txtMax")
$argumentList = $form.FindName("txtArguments")
$run = $form.FindName("btnRun")
$quit = $form.Findname("btnQuit")
#defaults
$min.Text = 1
$max.text = 5
$min.IsEnabled = $False
$max.IsEnabled = $false
$slider.IsEnabled = $True
$radioStatic.add_Checked( {
$min.IsEnabled = $False
$max.IsEnabled = $false
$slider.IsEnabled = $True
})
$radioRandom.Add_checked( {
$min.IsEnabled = $True
$max.IsEnabled = $True
$slider.IsEnabled = $False
})
$quit.add_click( { $form.close() })
Function _refresh {
#this is a function to refresh a UI element
Param($element)
$element.Dispatcher.invoke("render", [action] {})
[System.Threading.Thread]::Sleep(50)
}
$run.add_click( {
$results.text = "Testing...please wait"
_refresh $results
#uncomment for troubleshooting
#write-host "running" -ForegroundColor green
$form.Dispatcher.invoke([action] {
if ($sb.Text -notmatch "\w") {
Write-Warning "You must enter something to test!"
Return
}
$params = @{
Expression = [scriptblock]::Create($sb.text)
Count = $count.text -as [int]
IncludeExpression = $True
}
If ($argumentList.text) {
$params.Add("ArgumentList", ($argumentList.Text -split ","))
}
if ($radioStatic.IsChecked) {
$interval = [math]::round($slider.value, 1)
$params.Add("interval", $interval)
}
else {
[double]$minimum = [math]::Round($min.Text, 1)
[double]$maximum = [math]::Round($max.text, 1)
$params.Add("RandomMinimum", $minimum)
$params.Add("RandomMaximum", $maximum)
}
$form.Cursor = [System.Windows.Input.Cursors]::Wait
#uncomment for troubleshooting
#$params | out-string | write-host -ForegroundColor cyan
$script:out = Test-Expression @params -errorvariable ev
if ($script:out) {
$data = ($script:out | Select-Object -property * -exclude OS, Expression, Arguments | Out-String).Trim()
}
else {
$data = $ev.exception[0].message
}
$results.text = $data
$form.Cursor = [System.Windows.Input.Cursors]::Default
})
})
[void]$sb.Focus()
[void]$form.ShowDialog()
#write the current results to the pipeline after the form is closed.
$script:out
}
else {
Write-Warning "This command requires a Windows platform that supports WPF."
}
}