【问题标题】:How to execute separate Jmeter test plans one at a time with powershell?如何使用powershell一次执行一个单独的Jmeter测试计划?
【发布时间】:2021-12-23 03:39:40
【问题描述】:

我们收到了 20 个 jmeter 测试计划,每个测试一个端点,我们需要运行它。在测试中,我们需要传递参数,而其他我们不需要。

我的想法是创建一个 powershell 脚本,该脚本循环遍历目录并运行测试,等待完成,然后运行下一个测试。当我们开发一个新的端点时,我们只需创建一个新的测试计划并将其保存在适当的文件夹中,powershell 脚本将在我们下次循环测试时包含它。

我需要在开始下一个计划之前完成测试,所以我正在寻找类似的东西:

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()

这只是同时启动两个测试。 那么我的问题是如何让 Powershell 等待上一个测试完成。

【问题讨论】:

标签: powershell jmeter automated-tests performance-testing


【解决方案1】:

这可能是您遇到Out-Default cmdlet 执行的情况,最简单的方法是用分号分隔命令,例如:

cmd1;cmd2;cmd3;etc;

这样Powershell会等待上一个命令完成后再开始下一个

演示:

考虑切换到Maven JMeter Plugin 可能是一个更好的主意,它默认执行它在src/test/jmeter 文件夹下找到的所有测试,相对于pom.xml file

【讨论】:

  • 您是使用; 将多个 PowerShell 命令放在同一行还是将它们各自放在自己的行上都没有区别。 Out-Default 没有(有意义地)在这里发挥作用。 OP 的唯一(直接)问题是忽略将-PassThru 传递给Start-Process,这是使$proc.WaitForExit() 工作以确保同步执行的先决条件。
【解决方案2】:

您的直接问题是您的Start-Process 调用缺少-PassThru 开关,这是调用返回代表的System.Diagnostics.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()

# ... 

或者,如果您不需要检查进程退出代码(上面命令中的$proc.ExitCode 会为您提供),您可以简单地使用-Wait 开关,这使得Start-Process 本身等待进程终止:

# ... 

# 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"

# ... 

退后一步:

同步在当前控制台窗口中执行控制台应用程序或批处理文件直接调用,不要 使用Start-Process(或它所基于的System.Diagnostics.Process API)。

除了在语法上更简单、更简洁之外,这还有两个关键优势:

假设jmeter 是一个控制台应用程序(the docs 建议它在使用参数调用时作为一个应用程序运行):

# ... 

# 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.

# ... 

更多信息请参见this answer

【讨论】:

    猜你喜欢
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    相关资源
    最近更新 更多