debugJmx.sh 5.06 KB
#!/bin/bash

function usage(){
echo "----------------------------------"
echo "参数说明:"
echo "-p : jmx文件全父路径"
echo "-n : jmx文件名称"
echo "-t : 线程数"
echo "-c : 运行次数"
echo "-e : 要修改的hosts"
echo ""
echo "举例:sh debugJmx.sh -p /opt/jmeter/script/default -n test123.jmx -t 1 -c 1 -e 10.10.11.1@test.test.com,10.10.11.1@test.test.com"
echo "----------------------------------"
}

if [[ $# -lt 1 ]];then
usage
exit 1
fi

while getopts "p:n:t:c:e:" arg
do
case $arg in
p)
jmxPath=$OPTARG
;;
n)
jmxName=$OPTARG
;;
t)
threadCount=$OPTARG
;;
c)
loops=$OPTARG
;;
e)
hosts=$OPTARG
;;
h)
usage
exit 1
;;
?)
echo "未知的参数!"
exit 1
;;
esac
done

###
if [[ $hosts ]];then
  HOST_NAME=$(hostname)
  IP=$(/sbin/ip addr show | grep -e eth -e p4p | grep inet | awk '{print $2}' | awk -F '/' '{print $1}')
  hosts=$(echo $hosts | sed "s/@/ /g" | sed "s/,/\n/g")
  echo -e "127.0.0.1      localhost\n::1  localhost\n${IP}  ${HOST_NAME}\n$hosts" > /etc/hosts
fi

###
DEBUG_JMX_PATH=/tmp/debugJmx
DEBUG_LOG_PATH=$DEBUG_JMX_PATH/${jmxName}.debug.log
ResultCollector="<ResultCollector guiclass=\"StatVisualizer\" testclass=\"ResultCollector\" testname=\"聚合报告\" enabled=\"true\">\n"
ResultCollector="${ResultCollector}        <boolProp name=\"ResultCollector.error_logging\">false<\/boolProp>\n"
ResultCollector="${ResultCollector}        <objProp>\n"
ResultCollector="${ResultCollector}          <name>saveConfig<\/name>\n"
ResultCollector="${ResultCollector}          <value class=\"SampleSaveConfiguration\">\n"
ResultCollector="${ResultCollector}            <time>true<\/time>\n"
ResultCollector="${ResultCollector}            <latency>true<\/latency>\n"
ResultCollector="${ResultCollector}            <timestamp>true<\/timestamp>\n"
ResultCollector="${ResultCollector}            <success>true<\/success>\n"
ResultCollector="${ResultCollector}            <label>true<\/label>\n"
ResultCollector="${ResultCollector}            <code>true<\/code>\n"
ResultCollector="${ResultCollector}            <message>true<\/message>\n"
ResultCollector="${ResultCollector}            <threadName>true<\/threadName>\n"
ResultCollector="${ResultCollector}            <dataType>true<\/dataType>\n"
ResultCollector="${ResultCollector}            <encoding>true<\/encoding>\n"
ResultCollector="${ResultCollector}            <assertions>true<\/assertions>\n"
ResultCollector="${ResultCollector}            <subresults>true<\/subresults>\n"
ResultCollector="${ResultCollector}            <responseData>true<\/responseData>\n"
ResultCollector="${ResultCollector}            <samplerData>true<\/samplerData>\n"
ResultCollector="${ResultCollector}            <xml>true<\/xml>\n"
ResultCollector="${ResultCollector}            <fieldNames>true<\/fieldNames>\n"
ResultCollector="${ResultCollector}            <responseHeaders>true<\/responseHeaders>\n"
ResultCollector="${ResultCollector}            <requestHeaders>true<\/requestHeaders>\n"
ResultCollector="${ResultCollector}            <responseDataOnError>false<\/responseDataOnError>\n"
ResultCollector="${ResultCollector}            <saveAssertionResultsFailureMessage>true<\/saveAssertionResultsFailureMessage>\n"
ResultCollector="${ResultCollector}            <assertionsResultsToSave>0<\/assertionsResultsToSave>\n"
ResultCollector="${ResultCollector}            <bytes>true<\/bytes>\n"
ResultCollector="${ResultCollector}            <sentBytes>true<\/sentBytes>\n"
ResultCollector="${ResultCollector}            <url>true<\/url>\n"
ResultCollector="${ResultCollector}            <fileName>true<\/fileName>\n"
ResultCollector="${ResultCollector}            <hostname>true<\/hostname>\n"
ResultCollector="${ResultCollector}            <threadCounts>true<\/threadCounts>\n"
ResultCollector="${ResultCollector}            <sampleCount>true<\/sampleCount>\n"
ResultCollector="${ResultCollector}            <idleTime>true<\/idleTime>\n"
ResultCollector="${ResultCollector}            <connectTime>true<\/connectTime>\n"
ResultCollector="${ResultCollector}          <\/value>\n"
ResultCollector="${ResultCollector}        <\/objProp>\n"
ResultCollector="${ResultCollector}        <stringProp name=\"filename\">${DEBUG_LOG_PATH//\//\\/}<\/stringProp>\n"
ResultCollector="${ResultCollector}      <\/ResultCollector>\n"
ResultCollector="${ResultCollector}      <hashTree\/>"

if [[ ! -d $DEBUG_JMX_PATH ]];then
  mkdir -p $DEBUG_JMX_PATH
fi

if [[ -n $jmxPath && -n $jmxName ]];then
  \cp -f $jmxPath/$jmxName $DEBUG_JMX_PATH
  if [[ -n $threadCount ]];then
    sed -i "s/<stringProp name=\"ThreadGroup.num_threads\">.*<\/stringProp>/<stringProp name=\"ThreadGroup.num_threads\">${threadCount}<\/stringProp>/g" $DEBUG_JMX_PATH/$jmxName
  fi
  if [[ -n $loops ]];then
    sed -i "s/<intProp name=\"LoopController.loops\">.*<\/intProp>/<intProp name=\"LoopController.loops\">${loops}<\/intProp>/g" $DEBUG_JMX_PATH/$jmxName
  fi

  sed -i "s/<hashTree\/>.*<\/hashTree>/<hashTree\/>${ResultCollector}<\/hashTree>/g" $DEBUG_JMX_PATH/$jmxName 

  if [[ -f $DEBUG_LOG_PATH ]];then
    echo > $DEBUG_LOG_PATH
  fi

  /opt/jmeter/bin/jmeter -n -t $DEBUG_JMX_PATH/$jmxName -l /dev/null  -j /dev/null 1>/dev/null 2>&1
  
fi