性能对比报告记录

2021-01-06  本文已影响0人  LeslieFind

背景:

基于流水线的触发代码和定时执行的功能,考虑能够加上发布前的性能对比报告给开发更好,最终呈现出图表更加直观

思路:

一、首先构建jmeter的基础镜像jmeter-base(运行时需2个容器,一个执行master分支环境,一个执行dev分支环境)
二、在流水线构建两套环境,master和dev
三、jmeter-base中shell脚本,需实现的功能:

1、根据传入的branch或ip或hulk平台的开放接口获取请求的ip
2、有了ip后,传入持续时长,生成3分不同的jmx脚本,比如:list_thread_num_10.jmx的压测脚本
3、分别执行这些脚本,生成对应的报告文件
4、从报告中获取到压测数据,存入变量中
5、调用自定义接口,新增记录;

数据库中参数大致如下:

1、branch:分支
2、type:类型(先=1,为指定3种不同线程数;后续可以指定开始线程和结束线程数及区间,还有按照资源维度等)
3、thread_num:并发数
4、tps:
5、resp_time:
6、online:是否是线上版本,考虑可以跑一下线上版本,备用
7、during:持续时间
8、tps_exp:map/list(方便生成图)
9、resp_time_exp:map/list(方便生成图)
10、err_rate:错误率

自定义接口功能:

1、新增记录
2、获取最近3条master的数据(onlineList):用于日常开发合分支diff展示
3、获取dev和master的数据(testList):用于功能测试结束,上线之前diff展示,性能测试报告

=============================================================================
后续又使用locust
locust统计脚本思路:

#1、执行脚本,结束后
#2、解析:ldtest_stats.csv(总概数据):
#    grep "Aggregated" ldtest_stats.csv |awk -F , '{print $3,$4,$6,$10}'
#    grep "Aggregated" ldtest_stats.csv |awk -F , '{printf ("%.2f\n",$6)}' ——保留2位小数
#    结果:6118 90 193.21251854796148 51.00158406755608
#    ($3:总事务数,$4:错误数,$6:平均响应时长,$10:rps)
#    ------------------------------------------------
#    总事务数:grep "Aggregated" ldtest_stats.csv |awk -F , '{print $3}'——6118
#    错误数:grep "Aggregated" ldtest_stats.csv |awk -F , '{print $4}'——90
#    平均响应时长:grep "Aggregated" ldtest_stats.csv |awk -F , '{printf ("%.2f\n",$6)}'——193.21
#    rps每秒请求数:grep "Aggregated" ldtest_stats.csv |awk -F , '{printf ("%.2f\n",$10)}'——51.00
#    *额外计算错误率:grep "Aggregated" ldtest_stats.csv |awk -F , '{printf ("%.2f\n",$4/$3)}'——0.01
#    --------------------------------------------------------------------------------------------
#3、解析:ldtest_stats_history.csv
#    1、开始时间:sed -n 2p ldtest_stats_history.csv | cut -d ',' -f1
#    2、结束时间:sed -n '$p' ldtest_stats_history.csv | cut -d ',' -f1
#    3、时间与rps: awk -F , '{print $1,$5}' ldtest_stats_history.csv
#    4、时间与rt:awk -F , '{print $1,$21}' ldtest_stats_history.csv

locust统计脚本:

#!/bin/bash

# nohup locust -f locustfile.py --host=http://10.209.141.48 -u 10 -r 10 -t 120 --headless --csv ldtest --logfile testlogs -L debug  > debug.file 2>&1 &
# nohup locust -f locustfile.py --host=http://10.209.141.48 -u 2 -r 2 -t 20 --headless --csv ldtest --logfile testlogs -L debug > debug.file 2>&1 &

#1、执行脚本,结束后
# 6个参数:例:“locustfile.py http://10.209.141.48 2 2 20 ldtest testlogs”

if [[ $1 = "help" ]] || [[ $1 = "-h" ]]; then
    echo "test script and statistics the test's results"
    echo "\$1: locustfile"
    echo "\$2: serverHost"
    echo "\$3: threadNum"
    echo "\$4: threadNumPerSecond"
    echo "\$5: duration"
    echo "\$6: logfile"
else
    currentTime=`date "+%m%d%H%M"`
    # locust脚本
    locustfile=$1
    echo "***locustfile is: $1"

    # 服务端host
    serverHost=$2
    echo "***serverHost is: $2"

    # 并行数,虚拟用户数
    threadNum=$3
    echo "***threadNum is: $3"

    # 每s启动线程数
    threadNumPerSecond=$4
    echo "***threadNumPerSecond is: $4"

    # 持续时长
    duration=$5
    echo "***duration is: $5"

    #csvPrefix
    csvPrefix=testCsvData-$currentTime
    echo "***csvPrefix is: $csvPrefix"

    #logfile
    logfile=$6
    echo "***logfile is: $6"

    # 压测分支
#    若无,存master
    branch=$7


    # is_online
#    若无存0
    isOnline=$8
    
    if [ $# -lt 6 ]; then
        echo "ERROR====> the params must six!!!"
        exit 1;
    fi

    nohup locust -f $locustfile --host=$serverHost -u $threadNum -r $threadNumPerSecond -t $duration --headless --csv $csvPrefix --logfile $logfile -L debug > debug.file 2>&1 &
    echo "===locust commond is: nohup locust -f $locustfile --host=$serverHost -u $threadNum -r $threadNumPerSecond -t $duration --headless --csv $csvPrefix --logfile $logfile -L debug > debug.file 2>&1 &"

    locustProcess=`ps -ef | grep "locust -f locustfile.py" | grep -v grep | wc -l`
    while [ $locustProcess -gt 0 ]
    do
      locustProcess=`ps -ef | grep "locust -f locustfile.py" | grep -v grep | wc -l`
    done

    echo "===locust commond is end==="

    #================statistics==========================

    # 文件名称
    statsCsv=$csvPrefix"_stats.csv"
    echo "statsCsv file is: $statsCsv"

    statsHistoryCsv=$csvPrefix"_stats_history.csv"
    echo "statsHistoryCsv file is: $statsHistoryCsv"

    # 总事务数
    sumReq=`grep "Aggregated" $statsCsv |awk -F , '{print $3}'`
    echo "---->sumReq is: $sumReq"

    #平均响应时间
    avgRt=`grep "Aggregated" $statsCsv |awk -F , '{printf ("%.2f\n",$6)}'`
    echo "---->avgRt is: $avgRt"

    #tps
    tps=`grep "Aggregated" $statsCsv |awk -F , '{printf ("%.2f\n",$10)}'`
    echo "---->tps is: $tps"

    # 错误数
    errNum=`grep "Aggregated" $statsCsv |awk -F , '{print $4}'`
    echo "---->errNum is: $errNum"

    # 错误百分比
    errPerc=`grep "Aggregated" $statsCsv |awk -F , '{printf ("%.2f\n",$4/$3)}'`
    echo "---->errPerc is: $errPerc"

    # 开始时间
    creeateTime=`sed -n 2p $statsHistoryCsv | cut -d ',' -f1`
    echo "creeateTime is:$creeateTime"

    # 结束时间
    endTime=`sed -n '$p' $statsHistoryCsv | cut -d ',' -f1`
    echo "endTime is: $endTime"

#    ********************计算时间对应的***********************************

    declare -A timeRpsData
    declare -A timeRtData

    # 先取出时间
    timeList=`grep -v "Timestamp" $statsHistoryCsv | awk -F , '{print $1}'`

    # 取出rps
    rpsList=`grep -v "Timestamp" $statsHistoryCsv | awk -F , '{print $5}'`

    # 取出rt
    rtList=`grep -v "Timestamp" $statsHistoryCsv | awk -F , '{print $21}'`

    for t in $timeList;
    do
      # 取时间和rps对应关系
      timeRpsData[$t]=`grep "$t" $statsHistoryCsv | awk -F , '{printf ("%.2f\n", $5)}'`

      # 取时间和rt对应关系
      timeRtData[$t]=`grep "$t" $statsHistoryCsv | awk -F , '{printf ("%.2f\n", $21)}'`

    done

#    for key in "${!timeRpsData[@]}";
#    do
#      echo "$key------>${timeRpsData[$key]}"
#    done
#
#    for key in "${!timeRtData[@]}";
#    do
#      echo "$key==========>${timeRtData[$key]}"
#    done

    #=============================数据存库==============================================================================
    

fi
上一篇 下一篇

猜你喜欢

热点阅读