Search code examples
jenkinsjmeterpsexec

Run JMeter through PSExec with parameters from Jenkins


I have the following structure:

  1. Machine 1: Jenkins slave (Windows)
  2. Machine 2: JMeter machine (Windows)

What I want to obtain? Machine 1 receives some parameters from Jenkins (used for JMeter tests).

path/Performance.bat %param1% %param2% %param3%

So, Performance.bat looks something like:

IF %param1%==param1_value(
IF %param2%==param2_value(
@echo
path_to_psexec\PsExec.exe \\machine1_address -u user -p password cmd /c (

 MKDIR %param3%\Automation_Results_Build_%BUILD_NUMBER%_ID
 cd %JMeterPath%/
 jmeter -n -t [...])
 
exit /b 0
))

So, basically it create a result folder, then it navigates to JMeter location and tries to run the performance tests with given values.

Problem. It seems like PSExec does not execute the commands as intended. There are 3 commands sent through CMD: create folder for results, navigate to JMeter path, run tests. What it's wrong with my approach? How can I run multiple CMD commands in the same PSexec call?

Output:

ECHO is on.

PsExec v2.2 - Execute processes remotely
Copyright (C) 2001-2016 Mark Russinovich
Sysinternals - www.sysinternals.com

Connecting to machine_address...
   
Starting PSEXESVC service on machine_address...
    
Connecting with PsExec service on machine_address...
    
Starting cmd on machine_address...
    
cmd exited on machine_address with error code 0.
The system cannot find the path specified.
'jmeter' is not recognized as an internal or external command,
operable program or batch file.

Why the The system cannot find the path specified.?

I don't want to create a .bat file on machine 2, because I want to see the output of the tests in Jenkins.


Solution

  • You can create one PowerShell script in which you can execute all of your commands.

    Below Commands need to be included in your PowerShell script which will connect to your remote machine and execute commands on remote machine.

    $machine_addr = "your_ip_address"
    
    $user = your_username
    
    $Password = your_password
    
    $pass = ConvertTo-SecureString -AsPlainText $Password -Force
    
    $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $user ,$pass
    
    Invoke-Command -ComputerName $machine_addr -ScriptBlock {C:\path_to_your_jmeter_with_parameters} -Credential $Cred
    

    You can add other commands as per your convenience, For instance you want to create folder

    New-Item -Path "folder name with path" -ItemType directory 
    

    After creating your PowerShell script, just make a call from your batch file.

    C:\Users\%username%\Desktop\path_to_your_powershell_file\filename.ps1
    

    Suggestion: You can create single PowerShell script with parameters as well, like you are doing with batch file.(It will just remove the burden of creating batch file)