petek, 11. december 2015

Jmeter - send Jmeter properties file by Email with Powershell

Introduction

Every time when test start or ends it is fine to send email about properties files used in Jmeter run and at the end of the test a location of all artifacts with created word document, log PAL reports.
Because i use powershell to drive and control jmeter test and collect log, is function send email written in powershell.

Jmeter properties files

Each test can is written to be executed in:
  • Repetition loop; how many time should test iterate
  • Time loop; it is same as it is Run loop with unlimited iterations with set duration
  • Stepping thread group.. etc
Each of there items has defined a self specified parameters saved in properties files.:
  • Repetition loop :
    • RL_ParallelRuns
    • RL_Repetitions
    • RL_RampUpPeriod
    • RL_TestDurration
  • Time Loop
    • TL_ParallelRuns
    • TL_RampUpPeriod
    • TL_TestDurration
These properties files are included in jmeter  as global variables ( it is a bold text):

cmd.exe /c jmeter.bat --nongui -JforcePerfmonFile=true  --runremote --testfile "$global:TestName.jmx.bkp" `
--logfile "$JMeterReportFileFolder\$($global:TestName)-output.jtl" `
--jmeterlogfile "$JMeterReportFileFolder\$($global:TestName)-jmeter.log" `
--globalproperty LogDir="$($JMeterReportFileFolder)\" `
--globalproperty "Jmeter\$global:TestCaseName\$($global:TestCaseName) - General.properties" `
--globalproperty "Jmeter\$global:TestCaseName\$($global:TestScenario).properties" `
--globalproperty "Jmeter\$global:TestCaseName\$($global:TestScenario)_Thread.properties" `
--globalproperty "Jmeter\$global:TestCaseName\$($global:TestScenario)_Timeouts.properties" `
--globalproperty "Jmeter\$global:TestCaseName\$($global:TestScenario)_THT.properties" `
--globalproperty "perf.properties" `
--jmeterproperty "remote_hosts=$($global:Remote_Hosts)" `
--globalproperty "ServerRun=$($global:Remote_Hosts)"


This code is executed in powershell.

Jmeter read parameters from the file :
  • Repetition group
    • Read parameters from file
    • Used parameters in Thread group
  • Time loop
    • Read parameters from file 
    •  Used parameters in Thread group

No everthing is defined in jmeter to use this file  and run tests.

Powershell 

Powershell is great scripting tool to drive and manage jmeter test run before tests and after tests. Now our jo b is to setup function to send email. Configuration of email is of course saved in XML. XML is used for driving powershell.

XML structure

XML structure is easy to extend and manage with XPath. Let's take a loop of XML structure used for sending emails:

 <config>
    <settings>
        <email>
            <smtp>SMTP</smtp>
            <username>yourusername</username>
            <password>yourpassword</password>
            <sendemail>sendmeailto</sendemail>
        </email>

     </settings>
 </config>
   

Function send email

Function to send email written in powershell ( just google and create a self function):

function sendemail([string]$to, [string]$subject,[string] $body){
    $message = New-Object System.Net.Mail.MailMessage
    $message.subject = $subject
    $message.body = $body
    $message.to.add($to)
    $message.IsBodyHtml = $true
    #$message.cc.add($cc)
    $message.from = $usernameemail
    #$message.attachments.add($attachment)

    $smtp = New-Object System.Net.Mail.SmtpClient($smtpemail, $SMTPPort);
    $smtp.EnableSSL = $true
    $smtp.Credentials = New-Object System.Net.NetworkCredential($usernameemail,$passwordemail);
    if($SendEmail -eq "Yes")
    {
        $smtp.send($message)
        $hlog.WritePSInfo("Mail Sent")
    }
 }

As you can see i do no use SMTP port because i do  not needed, for google is used port 465 just add it in XML script and read element in powershell.

 Read XML file

Location of configuration file is in root:
 

$configpath = "."
$configfile = [xml] (Get-Content $configpath\config.xml)


Email configuration is read from the XML files:

$smtpemail =  $configfile.SelectSingleNode("/config/settings/email/smtp").InnerText
$usernameemail = $configfile.SelectSingleNode("/config/settings/email/username").InnerText
$passwordemail = $configfile.SelectSingleNode("/config/settings/email/password").InnerText
$tomail = $configfile.SelectSingleNode("/config/settings/email/sendemail").InnerText 

Read jmeter properties file and send it in the mail

Here is part written in powershell how to rad jmeter properties file put it in the mail and send a email.
Here is example how to read jmeter properties file:

$file = "yourfile.properties"
$propertiesfile = convertfrom-stringdata (get-content $file -raw)

Depends on which thread group is used the body of email is set, in this case will be represented Repetition group:

       $JMeterMaxThread= $propertiesfile.'RL_ParallelRuns'
       $JmeterTestDuration=   $propertiesfile.'RL_TestDurration'
       $JmeterRepetitions =   $propertiesfile.'RL_Repetitions'
       $JMeterAddThreadsEverySeconds=  "0"
       $JMeterAddNoThreads = $propertiesfile.'RL_RampUpPeriod'/ $propertiesfile.'RL_ParallelRuns'
       $JMeterAddThreadsEverySeconds = $propertiesfile.'RL_RampUpPeriod'
       $JMeterHoldLoadInSeconds = $propertiesfile.'RL_TestDurration'
       $JMeterFinalyStopNoThread = "0"
       $JMeterFinalyStopThreadsEverySeconds = "0"


        $body ="</br></br><b>Test case information 'Repeat limited test case':</b></br>
          <table border='0'>"




Now it is time to prepare body from jmeter properties file:
$body = "<h2>START EXECUTION OF TESTSTARTER SCRIPT</h2> </br></br>
    <table border='0'>
    <tr> <td>Test to be performed            :</td> <td><b>[$TestToBeExecuted]</b></td></tr>
    <tr> <td>Temporary Report file folder is
        </br> <b>Original is send at end of test</b>:</td> <td><b>$nasFileFolder</b> </td> </tr>
    <tr> <td>TestName                        :</td> <td><b>$global:TestName</b></td> </tr> <tr>
    <td>TestCaseName                    :</td> <td><b>$global:TestCaseName</b></td></tr> <tr>
    <td>TestScenario                    :</td> <td><b>$global:TestScenario</b></td>  </tr>
    <tr> <td>Comment                        :</td> <td><b>$Comment</b></td> </tr>
    <tr> <td>TimeStampScriptLocal            :</td> <td><b>$global:TimeStampScriptLocal</b></td> </tr>
    <tr> <td>Run test on hosts               :</td><td><b>$global:Remote_Hosts</b> (Multiply Max threads with Jmeter Max threads</td>  </tr>
    <tr>   
    </table>
     </br></br>
     $body $messageSignature"

 

And call function send an email before jmeter test starts:

sendemail $tomail "Start Test in $TestType" "$body"



After jmeter tests are finished semdemail with different body.

sendemail $tomail "EndTest " "br, Performance team"

TIP when sending XML file in email body

In some case you would like to send XML file in email body in the case you need to convert these escape parameters <,> with next function:
 function XMLEscape($Xml)
 {
    $ampersand = $Xml.Replace("&", "&amp;")
    $lessThan = $ampersand.Replace("<", "&lt;")
    $greaterThan = $lessThan.Replace(">", "&gt;</br>")
    $backslash = $greaterThan.Replace("\\", "&quot;")
    $quote = $backslash.Replace("'", "&quot;")
    $global:outXML = $quote.Replace("'", "&apos;")
 }


Summary

In this blog you find how easily is to configure XML file and create a code for sending email. More complicated is to configure jmeter to read this files and prepare email body in powershell from properties. I hope you enjoy adding you structure sending email functionality in powershell.

1 komentar:

  1. 188bet - Bwin.com - ThTopBet
    Check 188bet review ➜ All sports, ✓ Live betting, ✓ Live casino, betway ⚡️ More than 4,500 slots ✓ 더킹카지노 Live casino ✓ 18+ Play for real 188bet with T&Cs Apply.

    OdgovoriIzbriši