-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposhJmeter.ps1
289 lines (226 loc) · 5.76 KB
/
poshJmeter.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
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
<#
.SYNOPSIS
Starts JMeter
.DESCRIPTION
Based on the parameters passed, JMeter is started, either in GUI mode, or non-gui mode.
If JMeter is started in GUI mode, it is launched in a seperate process, and control is returned to the shell.
If JMeter is launched in non-gui mode (using the -NoGui switch), then it is run as part of the current session, and control is only returned to the command line once the test is finished.
.PARAMETER TestFile
The file o open in JMeter.
.PARAMETER Property
The properties to pass to JMeter. NOTE This uses the -JProp=Value command line parameters.
.PARAMETER LogFile
The path to the log file
.PARAMETER NoGui
Specifies that the test should be run in non-gui mode.
.EXAMPLE
PS C:\>Start-JMeter
Starts JMeter GUI with a blank project
.EXAMPLE
PS C:\>Start-JMeter -TestFile c:\tests\MyLoadTest.jmx
Starts the jmeter GUI and opens up the test file at C:\tests\MyLoadTest.jmx. NOTE an exception will be thrown if the test file does not exist.
PS C:\Start-JMeter -TestFile C:\tests\MyLoadTest.jmx -Property @{remote_hosts="10.20.1.88,10.20.1.89"} -NoGui
Runs the test in non gui mode setting the remote_hosts to 10.20.1.88 and 10.20.1.89
#>
function Start-JMeter
{
[CmdletBinding()]
Param(
[Parameter(Position=1)]
[alias("t")]
[string]$TestFile,
[Parameter()]
[hashtable]$Property,
[Parameter()]
[string]$Logfile,
[Parameter()]
[alias("n")]
[switch]$NoGui
)
$jmeterBinPath = Find-JMeter
$jmeterBat = Join-Path $jmeterBinPath "jmeter.bat"
$properties = @()
$arguments = @()
if ($Property)
{
$properties = $Property.Keys | % {"-J{0}={1}" -f $_, $Property[$_] }
}
if ($NoGui.IsPresent)
{
if (-not $TestFile)
{
Throw "You must specify a test file to run with -TestFile"
}
if (Test-Path -PathType Leaf $TestFile)
{
$arguments = $properties
$arguments += @("-t", $TestFile)
$arguments += "-n"
if ($LogFile)
{
$arguments += @("-j", $LogFile)
}
Write-Verbose "Executing command: $jmeterBat with arguments $arguments"
& $jmeterBat $arguments
}
else
{
Throw "You must specify a valid jmeter test file to execute"
}
}
else
{
$arguments = $properties
if ($TestFile)
{
if (Test-Path -PathType Leaf $TestFile)
{
$arguments += @("-t $TestFile")
}
else
{
Throw "Specified test file invalid"
}
}
if ($LogFile)
{
$arguments += @("-j $LogFile")
}
if ($arguments.Length -gt 0)
{
Write-Verbose ("Starting jmeter with the following parameters {0}" -f ($arguments -join ", "))
Start-Process $jmeterBat -ArgumentList $arguments -NoNewWindow
}
else
{
Start-Process $jmeterBat -NoNewWindow
}
}
}
<#
.SYNOPSIS
Finds the jmeter bin folder.
.DESCRIPTION
Searches the most likely locations for the JMeter bin folder. The order of search is
- $env:JMeterBinPath - Environment variable you can set to explicitly use a particular version of JMeter
- One of the following path locations
* C:\
* C:\Program Files
* C:\Program Files (x86)
* $env:ChocolateyInstall\lib\jmeter\tools
If more than one is found, the most recent of these will be used.
.EXAMPLE
PS C:\>Find-JMeter
#>
function Find-JMeter
{
[CmdletBinding()]
Param(
)
# First look for an environment variable
Write-Verbose "Looking for JMeterBinPath environment variable"
if ($env:JMeterBinPath)
{
# test the path
if (Test-JMeterPath -JMeterPath $env:JMeterBinPath)
{
# send the path to the pipeline and exit
$env:JMeterBinPath
return
}
}
else
{
Write-Verbose "No JMeterBinPath environment variable found"
}
Write-Verbose "Attempting to guess the path"
$pathToTry = @( `
$env:ProgramFiles, `
${env:ProgramFiles(x86)}, `
"C:\")
if ($env:ChocolateyInstall)
{
$chocoJMeterPath = Join-Path $env:ChocolateyInstall "lib\jmeter\tools"
if (Test-Path $chocoJMeterPath)
{
$pathToTry += $chocoJMeterPath
}
}
Write-Verbose ("Trying the following locations {0}" -f ($pathToTry -join ", "))
$apacheDir = Get-ChildItem -Path $pathToTry -Filter "apache-jmeter-*" | Sort-Object LastWriteTime -Descending
if ($apacheDir.Length -gt 0)
{
Write-Verbose ("Found {0} possible JMeter paths" -f $apacheDir.Length)
foreach ($pathToTest in $apacheDir)
{
$binPathToTest = Join-Path $pathToTest.FullName "bin"
if (Test-JMeterPath -JMeterPath $binPathToTest)
{
Write-Verbose "Found Jmeter at $binPathToTest"
$binPathToTest
return
}
}
}
Throw "Unable to find JMeter path"
}
<#
.SYNOPSIS
Tests a path to see if it has JMeter.bat
.DESCRIPTION
Validates that JMeter.bat is at the path specified.
.PARAMETER JMeterPath
The suspected JMeter path to validate
.EXAMPLE
PS C:\>Test-JMeterPath "C:\Program Files\apache-jmeter-2.13"
#>
function Test-JMeterPath
{
[CmdletBinding()]
Param(
[Parameter(Position=1, Mandatory=$True)]
[string] $JMeterPath
)
$jmeterBatFile = "jmeter.bat"
if (Test-Path -PathType Container $JMeterPath)
{
# now validate that we can find jmeter.bat
$batPath = Join-Path $JMeterPath $jmeterBatFile
if (Test-Path $batPath)
{
# return True on the pipeline
$True
return
}
else
{
Write-Verbose "JMeterBinPath Environment variable found, but could not find jmeter.bat at $JMeterPath"
}
}
else
{
Write-Verbose "JMeterBinPath Environment variable found, but is not a valid directory"
}
$False
}
<#
.SYNOPSIS
Stops a JMeter test that has been started in non-Gui mode
.DESCRIPTION
Stops a non-gui initialted JMeter test by calling the underlying stoptest.cmd in the jmeter bin folder.
#>
function Stop-JMeterTest
{
[CmdletBinding()]
Param(
[Parameter()]
[int] $CommandPort
)
$jmeterBinPath = Find-JMeter
$jmeterStopCommand = Join-Path $jMeterBinPath "stoptest.cmd"
if ($CommandPort -ne $null)
{
$jmeterStopCommand += (" {0}" -f $CommandPort)
}
Invoke-Expression -Command $jmeterStopCommand
}