JMeter test plan automation

This automation script runs JMeter test plan with multiple input files specified by JMeter. It also has ability to send email before working on an input file and once the input file processing is complete.

  • This is a working attempt for JMeter test plan automation using powershell.
  • Powershell makes it portable for multi-OS environment.
  • Script takes wildcard for input files, for each input file a directory is created in output folder.
    • All output files – .jtl, .png, jmeter.log from the run will be available in output folder

 

# ########## #################### ########## #################### ########## #
#
# 2020-07-06:[email protected]: v1: invoke jmeter with multiple input files
# 2020-08-12:[email protected]: v2: Fixed email vars
#
# [ sample command line ]:
#   ./runner_batch.ps1 './Test Plan.jmx' './working/datasets/*csv' './working/output'
# ########## #################### ########## #################### ########## #

# email vars
$email_server = "smtp.server.com"
$email_sender = "Robin <[email protected]>"
$email_recipients = @("Robin <[email protected]>") #, "Lastname, Firstname <[email protected]>")

# pass along the input variables to output jtl file
$in_sample_vars="var1_col_from_input_csv,var2_col_from_input_csv,var3_col_from_input_csv"

# setup logging function and filename
$Logfile = $MyInvocation.MyCommand.Name + ".log"
Function LogWrite
{
   Param ([string]$logstring)
   Add-content $Logfile -value $logstring -PassThru
}

# Inititlize jmeter home and bin
$jmeter_base="c:\path\to\apache-jmeter\apache-jmeter-5.2.1\"
$jmeter=Join-Path -Path $jmeter_base -ChildPath "bin\jmeter.bat"
if ($IsMacOS) {
    $jmeter_base="/path/to/apache-jmeter/apache-jmeter-5.2.1/"
  $jmeter=Join-Path -Path $jmeter_base -ChildPath "bin\jmeter.sh"
}
elseif ($IsLinux) {
    # Write-Host "Linux"
}

# Main program code
$input_jmx = $args[0]
$input_file_wildcard = $args[1]
$output_folder_location = $args[2]
# Get all input files that will be looped through for execution
$input_files = Get-Item $input_file_wildcard

foreach ($file in $input_files) {
    $input_file_no_ext = [io.path]::GetFileNameWithoutExtension($file)
    $out_dir = Join-Path -Path $output_folder_location -ChildPath $input_file_no_ext
    $out_jtl = Join-Path -Path $out_dir -ChildPath ($input_file_no_ext + "-output-results.jtl")
  $out_log_location = Join-Path -Path $out_dir -ChildPath ($input_file_no_ext + "-jmeter-log.log")
    
    $cmd = "$jmeter -n -t `"$input_jmx`" -l `"$out_jtl`" -Jsample_variables=`"$in_sample_vars`" -JTEST_RESULTS_FOLDER=`"$out_dir`" -JQUERY_FILE=`"$file`" -j`"$out_log_location`""

  New-Item -ItemType "directory" -Path "$out_dir"
  Copy-Item -Path "$input_jmx" -Destination "$out_dir"
  Send-MailMessage -From $email_sender -To $email_recipients -Subject "Starting file - $input_file_no_ext" -Body "Attempting to run this command - $cmd" -SmtpServer $email_server
    LogWrite "Running command - $cmd"
  Invoke-Expression $cmd
  LogWrite "Run Complete"
  Send-MailMessage -From $email_sender -To $email_recipients -Subject "completed file - $input_file_no_ext" -Body "Ran this command - $cmd" -SmtpServer $email_server
}

# End? - yes

 

HTH

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *