LR BASH

LoadRunner CD with Atlassian Bamboo: A bash script for it

This script runs a scenario using the Jenkins built into our AWS builds, waits for it to finish, then gets the results XML, including SLAs, which allow us to do proper pass/fail based on requirements. It then returns 0 for a pass and 1 for a fail.

#Web version NOTE: #MUST cater for 404s - itís in here now, may need further testing though.

#Web version: made a few other changes, get xml file from results folder. Last run number will need testing on your projects.

[ Typical run command now: project="test1_ci" controller="54.ppp.xx.yy" analysis_only="1" full_zip="1" ./run_jenkins.sh ]


#!/bin/bash

####################### These lines are for Bamboo specifically, setting variables etc. ############
####################### AND remove the command line variable section ###############################
#set -e
#set -x

################################################################################################ ####
#  You only need to change below values
#
   #env=${loadrunnerEnvironment}                              #stage / perf / int / test
   #duration=${loadrunnerDuration}                        #minutes
   controller=${performanceController}      #79.125.xxx.yy / 79.125.xxy.xx / 54.247.yy.xyx
   #scenario=${loadrunnerScenario}
   project=${performanceProject}
   #resultsdir=${loadrunnerResultsDir}
   analysis_only=${performanceAnalysis_only}
   full_zip=${performanceFull_zip}
#
################################################################################################ ####

#################################### End of Bamboo specifically, setting variables etc. ############



passmark=0

# First make sure we have the required params and if not print out an instructive message
if [ -z "$project" ] || [ -z "$controller" ]; then
     echo "check_jenkins1: Required parameters missing"
     echo
     echo 'usage: project="project_ci"  controller="79.125.xxy.xx" ./check_jenkins1.sh'
     echo "[project]        -     required, Jenkins project to run"
     echo "[controller]     -      required, controller to use (ip address typically)"
     echo "[analysis_only]  -      optional, set to "1" if you don't want to start a new job"
     echo "[full_zip]]        -      optional, set to "1" if you want to download the full results"
     echo "[filename]       -      optional, filename to process"
     echo "[stepname]           -      optional, for jenkins graphs"
     echo "[workspace]            -     optional, for jenkins graphs - jtl location"
     echo "[passFailFlag]     -      optional, will default to passed requests only (\"pass\"). string values: \"pass\", \"fail\", \"all\""
     echo "[limit]                  -     optional, percentile result must be above this. Integer value in ms."
     echo "[percentile]           -      optional, percentile to check for. Will default to 95. numeric value: 1 to 100"
    
     passmark=1
     echo passmark = $passmark - NOTE 0 = success -
     exit 0
fi

if [ -z "$analysis_only" ] ; then
     analysis_only="0"
fi

if [ -z "$full_zip" ] ; then
     full_zip="0"
fi

if [ -z "$passFailFlag" ] ; then
     passFailFlag="pass"
fi

if [ -z "$percentile" ] ; then
     percentile=95
fi

if (($percentile < 1)) ; then
     percentile=1
fi

if (($percentile > 100)) ; then
     percentile=100
fi

echo project = $project
echo controller = $controller

#run a script test
     rm -f test.html
     echo about to wget url: http://${controller}/job/${project} as test.html
     #wget http://${controller}/job/${project} -O test.html
     wget -E --save-headers http://${controller}/job/${project} -O test.html
    
     grep 'HTTP/' test.html
     code=$(grep 'HTTP/' test.html | awk '{print $2}')
     echo code = ${code}
     if [[ $code =~ "200" ]] || [[ $code =~ "302" ]]; then
           echo "returned ok"
     else
           echo "error on the test.html request"
           passmark=1
           echo passmark = $passmark - NOTE 0 = success -
           exit 0 #Only exit a pass mark for CI
     fi
    
     #rm -f workspace.html
     #wget http://${controller}/job/${project}/ws/ -O workspace.html
     #wc -l workspace.html
     #R_num=$(grep 'Results' workspace.html | awk 'BEGIN {FS="Results"} {print $2}' | awk 'BEGIN {FS=".xml"} {print $1}')
     #echo $R_num
    
     #wget http://${controller}/job/${project}/ws/Results${R_num}.xml
    
     #exit 0

#1. start a run:
if [ "$analysis_only" -ne "1" ]
     then
           echo "Starting up a test run"
           rm -f run_test.html
           #curl http://${controller}/job/${project}/build?delay=0sec
           echo about to wget url: http://${controller}/job/${project}/build?delay=0sec as run_test.html
           wget -E --save-headers http://${controller}/job/${project}/build?delay=0sec -O run_test.html
          
           grep 'HTTP/' run_test.html
           code=$(grep 'HTTP/' run_test.html | awk '{print $2}')
           echo code = ${code}
           if [[ $code =~ "201" ]] || [[ $code =~ "200" ]] || [[ $code =~ "302" ]]; then
                 echo "returned ok"
           else
                 echo "error on last request"
                 passmark=1
                 echo passmark = $passmark - NOTE 0 = success -
                 exit 0 #adjust this if required
           fi
    
           sleep 1m
     else
           echo "You selected analysis_only so not starting a new test run"
fi

#2. **NEED to work out when it's finished...

count=99
result=0
#look for pass (red) or fail(blue) for our project: ${project}
redwork="0"
bluework="0"
blue="0"
red="0"
aborted="0"
abortedwork="0"

if [ "$redwork" -eq "0" ] || [ "$bluework" -eq "0" ]
     then
           echo "test: yes"
     else
           echo "test: no"
fi

#while [ "$redwork" -eq "0" ] || [ "$bluework" -eq "0" ] #test loop line
while [ "$red" -eq "0" ] && [ "$blue" -eq "0" ]
do

     #get the top level status list
     #-----------------------------
     rm -f index.html
     echo about to wget url: http://${controller} as run_index.html
     wget -E --save-headers http://${controller} -O index.html
    
     grep 'HTTP/' index.html
     code=$(grep 'HTTP/' index.html | awk '{print $2}')
     echo code = ${code}
     if [[ $code =~ "200" ]] || [[ $code =~ "302" ]]; then
           echo "returned ok"
     else
           echo "error on last request"
           passmark=1
           echo passmark = $passmark - NOTE 0 = success -
           exit 0 #adjust this if required
     fi

     echo ------------------------------
     echo ${project}
     grep -o 'id=${project}.*' index.html | awk 'BEGIN {FS="></td"} {print $1}' | grep 'job-status-red-anime'
     echo ------------------------------
     redwork=$(grep -o id=\"job_${project}.* index.html | awk 'BEGIN {FS="></td"} {print $1}' | grep 'job-status-red-anime' | wc -l)
     echo redwork = $redwork
     bluework=$(grep -o id=\"job_${project}.* index.html | awk 'BEGIN {FS="></td"} {print $1}' | grep 'job-status-blue-anime' | wc -l)
     echo bluework = $bluework
     abortedwork=$(grep -o id=\"job_${project}.* index.html | awk 'BEGIN {FS="></td"} {print $1}' | grep 'job-status-aborted-anime' | wc -l)
     echo abortedwork = $abortedwork
     red=$(grep -o id=\"job_${project}.* index.html | awk 'BEGIN {FS="></td"} {print $1}' | grep 'job-status-red"' | wc -l)
     echo red = $red
     blue=$(grep -o id=\"job_${project}.* index.html | awk 'BEGIN {FS="></td"} {print $1}' | grep 'job-status-blue"' | wc -l)
     echo blue = $blue
     aborted=$(grep -o id=\"job_${project}.* index.html | awk 'BEGIN {FS="></td"} {print $1}' | grep 'job-status-aborted"' | wc -l)
     echo aborted = $aborted
    
     #job-status-aborted
     #job-status-aborted-anime"
    


     if [ $red -eq 1 ]
           then
             echo "last run is a fail"
             #exit 1 #terminate and indicate error
             result=1
            
     else
           if [ "$blue" == "1" ]
                 then
                 echo "last run PASS"
                 #exit 0 #terminate ok
                 result=0
          
           else
                 if [ "$redwork" == "1" ]
                       then
                             echo "last run NOT finished"
                       #exit 0 #terminate
                 else
                       if [ "$bluework" == "1" ]
                             then
                                   echo "last run NOT finished"
                                   #exit 0 #terminate
                       else
                             if [ "$abortedwork" == "1" ]
                                   then
                                         echo "last run NOT finished"
                                         #exit 0 #terminate
                             else
                                   if [ "$blue" == "1" ]
                                         then
                                         echo "last run ***ABORTED***"
                                         passmark=1
                                         echo passmark = $passmark - NOTE 0 = success -
                                         exit 0 #terminate and indicate error
                                   else                                  
                                         echo "no result"
                                         #echo "test here"
                                         #exit 1 # terminate and indicate error
                                   fi
                             fi
                       fi
                 fi
           fi
     fi    
    
     if [ "$analysis_only" -ne "1" ]
           then
                 echo "sleeping for a while"
                 sleep 60s
     fi
    
done

##3. Last run pass or fail? Got the answer above but want to get results back as well...
echo test run result = $result

############## get last run number ###################
#
# Some chancges here to cater for passed/failed build - you might want to change this depending on your needs
#
######################################################

#get the top level status list AGAIN - saw error here with webpagetest run
     #-----------------------------
     rm -f index.html
     echo about to wget url for final time: http://${controller} as run_index.html
     wget -E --save-headers http://${controller} -O index.html
    
# get absolute last successful run
last_run=$(grep job/${project}/lastSuccessfulBuild index.html | head -2 | tail -1 | awk 'BEGIN {FS=">#"} {print $2}' | awk 'BEGIN {FS="<"} {print $1}')
echo -----------------------------
echo last successful run number = ${last_run}
echo -----------------------------

if [ -z "${last_run}" ] ; then
     last_run="0"
     echo no successful build
fi

# get absolute last failed run
last_run=$(grep job/${project}/lastFailedBuild index.html | head -2 | tail -1 | awk 'BEGIN {FS=">#"} {print $2}' | awk 'BEGIN {FS="<"} {print $1}')
echo -----------------------------
echo last run number - failed build = ${last_run}
echo -----------------------------

if [ -z "${last_run}" ] ; then
     last_run="0"
     echo no failed build
fi

    
# get absolute last run
last_run=$(grep job/${project}/last index.html | head -2 | tail -1 | awk 'BEGIN {FS=">#"} {print $2}' | awk 'BEGIN {FS="<"} {print $1}')
echo -----------------------------
echo absolute last run number = ${last_run}
echo -----------------------------


#get the latest results xml file
    
    
     #use run number to go to results archive
     #http://${controller}/job/${project}/${last_run}/artifact/
    
     rm -f workspace.html
     rm -f project.html
     #was using workspace saved xml file: wget -E --save-headers http://${controller}/job/${project}/ws/ -O workspace.html
    
     echo about to wget url: http://${controller}/job/${project}/${last_run}/artifact/ as project.html
     wget -E --save-headers http://${controller}/job/${project}/${last_run}/artifact/ -O project.html
    
     grep 'HTTP/' project.html
     code=$(grep 'HTTP/' project.html | awk '{print $2}')
     echo code = ${code}
     if [[ $code =~ "200" ]] || [[ $code =~ "302" ]]; then
           echo "returned ok"
     else
           echo "error on last request"
           passmark=1
           echo passmark = $passmark - NOTE 0 = success -
           exit 0 #adjust this if required
     fi
    
     wc -l project.html
     R_num=$(grep 'Results' project.html | awk 'BEGIN {FS="Results"} {print $2}' | awk 'BEGIN {FS=".xml"} {print $1}')
     echo R_num = $R_num
    
     #MUST cater for 404 - no results file, especially for analysis only
    
     rm -f *.xml
     #was using workspace saved xml file: wget -E --save-headers http://${controller}/job/${project}/ws/Results${R_num}.xml -O results.xml
     #http://localhost/job/all4_ci/17/artifact/Results20062016110938033.xml
     echo about to wget url: http://${controller}/job/${project}/${last_run}/artifact/Results${R_num}.xml as results.xml
     wget -E --save-headers http://${controller}/job/${project}/${last_run}/artifact/Results${R_num}.xml -O results.xml
    
     grep 'HTTP/' results.xml
     code=$(grep 'HTTP/' results.xml | awk '{print $2}')
     echo code = ${code}
     if [[ $code =~ "200" ]] || [[ $code =~ "302" ]]; then
           echo "returned ok"
     else
           echo "error on last request"
           passmark=1
           echo passmark = $passmark - NOTE 0 = success -
           exit 0 #adjust this if required
     fi
    
sleep 2
#cd "${WORKSPACE}"
echo -n "post analysis: using "
ls -rt1 *.xml | tail -1
cat $(ls -rt1 *.xml|tail -1)|grep "Actual value"
cat $(ls -rt1 *.xml|tail -1)|grep "Goal Value"

count=$(cat $(ls -rt1 *.xml|tail -1)|grep "Actual value"|wc -l)

i=0
while [ $i != $count ] ; do

     i=$(echo "" | awk '{print '$i' + '1'}')
    
     if [ -z $(eval echo '$'metric_$i) ] ; then
           our_metric="metric_${i}"
     else
           our_metric=$(eval echo '$'metric_$i)
     fi
    
     echo analysing $our_metric
    
     #our_description=$(cat $(ls -rt1 *.xml|tail -1)|grep "Transaction Name" | head -${i} | tail -6 | awk '{print $3}')
     echo i = ${i}
     #our_description=$(cat $(ls -rt1 *.xml|tail -1)| head -${i} | tail -6 | head -1)
     #echo our_description ${i}: $our_description
    
     line=$(cat $(ls -rt1 *.xml|tail -1) | grep -n "Actual value" | head -${i} | tail -1 |awk 'BEGIN {FS=":"} {print $1}')
     echo -n 'line relating to this metric: '
     cat $(ls -rt1 *.xml|tail -1) | head -${line} | tail -1

     our_description=$(cat $(ls -rt1 *.xml|tail -1) | head -${line} | tail -6 | grep 'Name')
     echo our_description: $our_description
    
     metric1=$(echo $our_description | awk 'BEGIN {FS=":"} {print $2}' | awk 'BEGIN {FS=" Full"} {print $1}')
     echo metric1_: ${metric1}
    
     #our_metric=$metric1
     our_metric=$(eval echo ${metric1} | sed 's/ /_/g')
     echo our_metric__: ${our_metric}
    
     #metric1=$(echo $our_description | awk 'BEGIN {FS="Name: "} {print $2}' | awk 'BEGIN {FS=" Full"} {print $1}')
     #echo metric1: ${metric1}
    
     echo $our_description | awk 'BEGIN {FS="Name: "} {print $2}'
     #echo $our_description | awk 'BEGIN {FS="Name: "} {print $2}' | awk 'BEGIN {FS=" Full"} {print $1}'
     #echo $our_description | awk 'BEGIN {FS="Name: "} {print $2}' | awk 'BEGIN {FS=" Full"} {print $1}'
    
     our_status=$(cat $(ls -rt1 *.xml|tail -1) | head -$((line+2)) | tail -6 | grep 'status')
     echo our_status: $our_status
    
     our_response=$(cat $(ls -rt1 *.xml|tail -1)|grep "Actual value" | head -${i} | tail -1 | awk '{print $4}')
     echo response ${i}: $our_response
     our_goal=$(cat $(ls -rt1 *.xml|tail -1)|grep "Goal Value" | head -${i} | tail -1 | awk '{print $4}')
     echo goal ${i}: $our_goal

     our_diff=$(echo "" | awk '{print '$our_goal' - '$our_response'}')
     echo our_diff = $our_diff
     our_neg=$(echo "check for neg" ${our_diff} | grep "-")
     echo our_neg = $our_neg
    
     #if [ -z "${our_neg}" ] ; then
     #      echo our neg says pass
     #      our_pass="true"
     #      our_rc="200"
     #else
     #      if [[ "$our_description" =~ "Hits Per Second" ]]; then
     #            echo our neg says pass
     #            our_pass="true"
     #            our_rc="200"
     #      else
     #            echo our neg says fail
     #            our_pass="false"
     #            our_rc="500"
     #            passmark=1
     #      fi
          
     #fi
    
    
     if [[ $our_status =~ "Passed" ]] ; then
           echo status says PASS
           our_pass="true"
           our_rc="200"
     else
           echo status says FAIL
           our_pass="false"
           our_rc="500"
           passmark=1
     fi

     timenow=$(date +%s)000
     jtl_time=$(echo "" | awk '{print '$our_response' * '1000'}'|awk 'BEGIN {FS="."};{print $1}')

     fname=$(eval echo ${our_metric}.jtl)
     echo jtl filename: ${fname}
     echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > "${fname}"
     echo "<testResults version=\"1.2\">" >> "${fname}"
     echo "<httpSample t=\"${jtl_time}\" ts=\"${timenow}\" s=\"${our_pass}\" lb=\"${our_metric}\" rc=\"${our_rc}\"/>" >> "${fname}"
     echo "</testResults>" >> "${fname}"
    
done


#----------------------------------------------------------------------
#Errors separately:
     echo
     echo "NOTE: Analysing errors per second pass/fail"
     line=$(cat $(ls -rt1 *.xml|tail -1) | grep -n "Errors Per Second" | head -${i} | tail -1 |awk 'BEGIN {FS=":"} {print $1}')
     echo -n 'line relating to this metric _e: '
     cat $(ls -rt1 *.xml|tail -1) | head -${line} | tail -1
    
     our_status=$(cat $(ls -rt1 *.xml|tail -1) | head -$((line+6)) | tail -5 | grep 'status')
    
     echo our_status_e: $our_status
    
    
     if [[ $our_status =~ "Passed" ]] ; then
           echo status says PASS
           our_pass="true"
           our_rc="200"
     else
           echo status says FAIL
           our_pass="false"
           our_rc="500"
           passmark=1
     fi

     num_errors1=$(grep "total number of errors" results.xml | head -1 | awk 'BEGIN {FS=": "} {print $3}')
     echo num_errors reported = ${num_errors1}
    
     our_metric="Errors_and_sla_value"
     timenow=$(date +%s)000
     #echo our_response variable = $our_response
     #jtl_time=$(echo "" | awk '{print '$our_response' * '1000'}'|awk 'BEGIN {FS="."};{print $1}')
     jtl_time=${num_errors1}

     fname=$(eval echo ${our_metric}.jtl)
     echo jtl filename: ${fname}
     echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > "${fname}"
     echo "<testResults version=\"1.2\">" >> "${fname}"
     echo "<httpSample t=\"${jtl_time}\" ts=\"${timenow}\" s=\"${our_pass}\" lb=\"${our_metric}\" rc=\"${our_rc}\"/>" >> "${fname}"
     echo "</testResults>" >> "${fname}"
    
    
if [ "${full_zip}" == "1" ]
     then
           echo "Getting the zipped up results"
           #rm -f run_test.html
           #curl http://${controller}/job/${project}/build?delay=0sec
           #wget http://${controller}/job/${project}/${last_run}/artifact/..............lrs_Report.zip -O run_test.html
          
           wc -l project.html
           Results=$(grep '_Report.zip' project.html | awk 'BEGIN {FS="_Report.zip\">"} {print $2}' | awk 'BEGIN {FS="</a>"} {print $1}')
           echo Results = $Results
    
           echo about to try: wget http://${controller}/job/${project}/${last_run}/artifact/${Results}
           rm -f ${Results}
           echo about to wget url: http://${controller}/job/${project}/${last_run}/artifact/${Results}
           wget http://${controller}/job/${project}/${last_run}/artifact/${Results}
          
           #grep 'HTTP/' run_test.html
           #code=$(grep 'HTTP/' run_test.html | awk '{print $2}')
           #echo code = ${code}
           #if [[ $code =~ "201" ]] || [[ $code =~ "200" ]] || [[ $code =~ "302" ]]; then
           #      echo "returned ok"
           #else
           #      echo "error on last request"
           #      exit 1 #adjust this if required
           #fi
    
           #sleep 1m
     else
           echo "You did not select Ďfull_zip'"
fi

echo
echo ------------------ debug notes ------------------
num_errors=$(eval 'grep "total number of errors" results.xml | head -1')
echo ${num_errors}
echo ------------------ end of debug ------------------
echo

#disk space check
if grep -q DiskSpace project.html
then
     d_start=$(grep DiskSpace project.html | awk 'BEGIN {FS="DiskSpace"} {print $2}'| awk 'BEGIN {FS=">"} {print $2}')
     d_end=$(grep DiskSpace project.html | awk 'BEGIN {FS="DiskSpace"} {print $3}'| awk 'BEGIN {FS="<"} {print $1}')
     echo about to wget -E --save-headers http://${controller}/job/${project}/${last_run}/artifact/${d_start}DiskSpace${d_end}
     wget -E --save-headers http://${controller}/job/${project}/${last_run}/artifact/${d_start}DiskSpace${d_end} -O diskspace.jtl
                
           grep 'HTTP/' diskspace.jtl
           code=$(grep 'HTTP/' diskspace.jtl | awk '{print $2}')
           echo code = ${code}
           if [[ $code =~ "201" ]] || [[ $code =~ "200" ]] || [[ $code =~ "302" ]]; then
                 echo "returned ok"
                
                 if grep -q 's="false"' diskspace.jtl
                 then
                       passmark=1
                       echo disk space low - check the controller. Must fail test run due to this.
                 else
                       echo disk space check passed
                 fi
           else
                 echo "error on last request"
           #      exit 1 #adjust this if required
           fi
          
          
fi    
          
echo passmark = $passmark - NOTE 0 = success -
exit 0


 

[Home] [About (CV)] [Contact Us] [JMeter Cloud] [webPageTest] [_64 images] [asset moniitor] [Linux Monitor] [Splunk ETL] [Splunk API] [AWS bash] [LR Rules OK] [LR Slave] [LR CI Graphs] [LoadRunner CI] [LR CI Variables] [LR Bamboo] [LR BASH] [LR BASH2] [LR Methods] [LR CI BASH] [Bash methods] [Jenkins V2] [Streaming vid] [How fast] [Finding Issues] [Reporting] [Hand over] [VB Scripts] [JMeter tips] [JMeter RAW] [Dynatrace] [Documents] [FAQ] [Legal]

In the Cartesian Elements Ltd group of companies