0

We have received 20 jmeter test plans, each testing one endpoint, which we need to run. On tests we need to pass parameters and others we don't.

My idea was to create a powershell script that loops through the directories and runs a test, waits until finished and then runs the next test. When we develop a new endpoint we just create a new test plan and save it in the appropriate folder and the powershell script will include it next time we loop through tests.

I need the tests to finish before starting the next plan, so I'm looking at something like:

Write-Output "Running Test 1"


$proc =  Start-Process -FilePath "C:\JmeterLoadTests\apache-jmeter-5.2.1\bin\jmeter" -ArgumentList "-n -t C:\JmeterLoadTests\test\enpointsType1\test-1-1.jmx -Jduration=10"
$proc.WaitForExit()

Write-Output "Proc 1 Done"
Write-Output "Running Proc 2"

$proc2 =  Start-Process -FilePath "C:\JmeterLoadTests\apache-jmeter-5.2.1\bin\jmeter" -ArgumentList "-n -t C:\JmeterLoadTests\test\enpointsType1\test-1-1.jmx -Jduration=10"
$proc2.WaitForExit()

This just launches both tests simultaneously. My question is then how to make Powershell wait for the previous test to finish.

mklement0
  • 382,024
  • 64
  • 607
  • 775
OrigamiEye
  • 864
  • 1
  • 12
  • 31
  • Did you checked https://stackoverflow.com/questions/1741490/how-to-tell-powershell-to-wait-for-each-command-to-end-before-starting-the-next ? – Ori Marko Nov 10 '21 at 14:33
  • Yes, that's where I got a version of my answer and i'm working through the answer their. – OrigamiEye Nov 10 '21 at 14:35

2 Answers2

1

Your immediate problem is that your Start-Process call is missing the -PassThru switch, which is required for the call to return a System.Diagnostics.Process instance representing the newly launched process.

# ... 

# Note the use of -PassThru
$proc =  Start-Process -PassThru -FilePath "C:\JmeterLoadTests\apache-jmeter-5.2.1\bin\jmeter" -ArgumentList "-n -t C:\JmeterLoadTests\test\enpointsType1\test-1-1.jmx -Jduration=10"
$proc.WaitForExit()

# ... 

Alternatively, if you don't need to examine the process exit code (which $proc.ExitCode in the above command would give you), you can simply use the -Wait switch, which makes Start-Process itself wait for the process to terminate:

# ... 

# Note the use of -Wait
Start-Process -Wait -FilePath "C:\JmeterLoadTests\apache-jmeter-5.2.1\bin\jmeter" -ArgumentList "-n -t C:\JmeterLoadTests\test\enpointsType1\test-1-1.jmx -Jduration=10"

# ... 

Taking a step back:

To synchronously execute console applications or batch files in the current console window, call them directly, do not use Start-Process (or the System.Diagnostics.Process API it is based on).

Aside from being syntactically easier and less verbose, this has two key advantages:

Assuming that jmeter is a console application (the docs suggests it runs as one when invoked with arguments):

# ... 

# Direct invocation in the current window.
# Stdout and stderr output will print to the console by default,
# but can be captured or redirected.
# Note: &, the call operator, isn't strictly needed here,
#       but would be if your executable path were quoted 
#       or contained variable references.
& C:\JmeterLoadTests\apache-jmeter-5.2.1\bin\jmeter -n -t C:\JmeterLoadTests\test\enpointsType1\test-1-1.jmx -Jduration=10

# Use $LASTEXITCODE to examine the process exit code.

# ... 

See this answer for more information.

mklement0
  • 382,024
  • 64
  • 607
  • 775
-1

It might be the case you're suffering from Out-Default cmdlet execution, the easiest is separating the commands with the semicolon like:

cmd1;cmd2;cmd3;etc;

this way Powershell will wait for the previous command to complete before starting the next one

Demo:

enter image description here

It might be a better idea considering switching to Maven JMeter Plugin which by default executes all tests it finds under src/test/jmeter folder relative to the pom.xml file

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Whether you use `;` to place multiple PowerShell commands on the same line or place them each on their own line makes no difference. `Out-Default` does not (meaningfully) come into play here. The OP's only (immediate) problem is neglecting to pass `-PassThru` to `Start-Process`, which is the prerequisite for making `$proc.WaitForExit()` work to ensure synchronous execution. – mklement0 Nov 11 '21 at 17:14