1. 统计依据

根据xchain.log中的txCount进行统计,如日志

t=2021-06-21T19:24:51+0800 lvl=info msg="start to confirm block" module=xchain blockid=649a6f7848e7103aa64f0b7c102291d972c7485717ebfd03a4c561d3e669ae54 txCount=1

 

 2. 使用

bash get_tps_from_log.sh xchain.log 21:21:28 21:21:58

 

百度区块链 xuperchain 节点端 tps统计脚本_bash

 

3. 统计脚本get_tps_from_log.sh

#!/bin/bash
function Usage()
{
    echo "Usage:"
    echo "bash $0 log_file_path start_time end_time"
    echo "bash ./node_side/xuperchain/get_tps_from_log.sh ../logs/xchain.log 20:50:00 20:51:30"
    exit 1
}


log_file="${1}"
if [ $# -eq 1 ];then
    if [ "${log_file}"="-h" ]||[ "${log_file}"="--help" ];then
       Usage
    fi
fi
if [ $# -lt 3 ];then
    Usage
fi

start_time="${2}"
end_time="${3}"

# t=2021-06-21T19:24:51+0800 lvl=info msg="start to confirm block" module=xchain blockid=649a6f7848e7103aa64f0b7c102291d972c7485717ebfd03a4c561d3e669ae54 txCount=1
# grep 'lvl=info msg="start to confirm block"' xchain.log | awk -F' ' '{print $1}' | cut -d'T' -f2 | cut -d'+' -f1 | sort | uniq
timeList=$(grep 'lvl=info msg="start to confirm block"' ${log_file} | awk -F' ' '{print $1}' | cut -d'T' -f2 | cut -d'+' -f1 | sort | uniq)


txTotal=0
statistic_end=${end_time}
statistic_start=${start_time}

started=0

for time in ${timeList}
do
   if [ "${time}" \> "${start_time}" ]&&[ "${time}" \< "$end_time" ];then

         echo "====time:${time}"
         
         txList=$(grep 'lvl=info msg="start to confirm block"' ${log_file} | grep ${time} | awk -F' ' '{print $9}' | cut -d'=' -f2)

         for tx in ${txList}
         do

            echo "tx:${tx}"

            if [ $started = 0 ] && [ $tx -gt 1 ];then
               started=1
               statistic_start="${time}"
            fi

            if [ $tx -gt 1 ];then
                # Every 3 seconds, xuperchain makes a block by default
                # In each block, there will be a TX, which is a transaction to save a reward for miners
                txTotal=$((txTotal+tx-1))
                statistic_end=${time}
            fi

         done
         
   fi
done

echo "statistic_start = ${statistic_start}"
echo "statistic_end = ${statistic_end}"

start_hour=$(echo ${statistic_start} | awk -F':' '{print $1}')
start_min=$(echo ${statistic_start} | awk -F':' '{print $2}')
start_s=$(echo ${statistic_start} | awk -F':' '{print $3}'| cut -d'.' -f1)
start_t=$(echo "$start_hour*3600000+ $start_min*60000 + $start_s*1000" | bc)

end_hour=$(echo ${statistic_end} | awk -F':' '{print $1}')
end_min=$(echo ${statistic_end} | awk -F':' '{print $2}')
end_s=$(echo ${statistic_end} | awk -F':' '{print $3}'| cut -d'.' -f1)
end_t=$(echo "$end_hour*3600000+ $end_min*60000 + $end_s*1000" | bc)

delta_time=$((end_t-start_t))

if [ $delta_time = 0 ];then
    tps=$(echo "scale=1;${txTotal}*1000/3" | bc)
else
    tps=$(echo "scale=1;${txTotal}*1000/$delta_time" | bc)
fi

echo "the unit of execute_time is ms, the unit of tps is tx/s"
echo "[tps_summary],total_transactions=${txTotal},execute_time=${delta_time},tps=${tps}"