当前位置:网站首页>Shell script to loop through values in log file to sum and calculate average, max and min
Shell script to loop through values in log file to sum and calculate average, max and min
2022-07-31 01:49:00 【love coriander】
最近在进行压测,为了观察并定位服务性能的瓶颈,所以在代码中很多地方加了执行耗时的日志,但这种方式只能观察,却在压测的时候,不太能准确的把握代码中某些方法的性能,所以想到写一个脚本,用来统计所加的日志中的平均耗时,最大耗时,最小耗时等等,这需要保证每行日志都是唯一的,代码中添加日志的方式如下:
为了便于验证,写了一个简单的日志文件 console.log ,内容如下:
[[email protected] shell]# cat console.log
filterStr 23
filterStr 56
filterStr 89
filterStr 1
filterStr 10
filterStr 2
requireTime 3
requireTime 4
requireTime 3
requireTime 10
1.先编写一个计算 含 filterStr 日志行的所有平均时间,最大时间,最小时间,脚本示例如下:
#!/bin/bash
sum=0
min=`cat console.log|grep "filterStr"|awk '{print $2}'|head -n 1`
max=0
for value in `cat console.log|grep 'filterStr'|awk '{print $2}'`
do
sum=$(($sum+$value))
if [ $value -le $min ];then
min=$value
fi
if [ $value -ge $max ];then
max=$value
fi
done
echo "总共耗时:"$sum
totalCount=`cat console.log|grep 'filterStr'|awk '{print $2}'|wc -l`
echo "总共请求次数为:"$totalCount
#计算请求的平均时间
avageTime=$(($sum/$totalCount))
echo "平均请求响应时间为: "$avageTime
#打印最大值与最小值
echo "最大值为:"$max
echo "最小值为:"$min
最小值的初始定义命令为: min=cat console.log|grep "filterStr"|awk '{print $2}'|head -n 1
; 这行命令过滤出日志的所有行,并取第一行中过滤出的第二个值(awk ‘{print $2}’).所以需要提前预定好 该耗时计算在日志中的位置,我这边示例中位于第二个位置,所以取第二个$2 的 值
该脚本执行效果如下:
2. 通过动态传参过滤内容的方式执行执行脚本
由于服务中加了很多位于不同代码处的耗时日志,所以想到通过动态传参的方式执行脚本,shell 脚本示例如下:
#!/bin/bash
sum=0
min=`cat console.log|grep "$filterStr"|awk '{print $2}'|head -n 1`
max=0
filterStr=$1
for value in `cat console.log|grep "$filterStr"|awk '{print $2}'`
do
sum=$(($sum+$value))
if [ $value -le $min ];then
min=$value
fi
if [ $value -ge $max ];then
max=$value
fi
done
echo "总共耗时:"$sum
totalCount=`cat console.log|grep "$filterStr"|awk '{print $2}'|wc -l`
echo "总共请求行数为:"$totalCount
#计算请求的平均时间
avageTime=$(($sum/$totalCount))
echo "平均请求响应时间为: "$avageTime
#打印最大值与最小值
echo "最大值为:"$max
echo "最小值为:"$min
用该脚本计算 console.log 中的 requireTime 相关行的耗时统计:
现在我邀请你进入我们的软件测试学习交流群:【746506216
】,备注“入群”, 大家可以一起探讨交流软件测试,共同学习软件测试技术、面试等软件测试方方面面,还会有免费直播课,收获更多测试技巧,我们一起进阶Python自动化测试/测试开发,走向高薪之路.
喜欢软件测试的小伙伴们,如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “点赞” “评论” “收藏” 一 键三连哦!
边栏推荐
猜你喜欢
coldfusion8后台计划任务拿shell
1782. Count the number of point pairs Double pointer
Parameter introduction and selection points of wireless module
MySQL的分页你还在使劲的limit?
MySql的安装配置超详细教程与简单的建库建表方法
"Cloud native's master, master and vulgar skills" - 2022 National New College Entrance Examination Volume I Composition
pycharm重命名后无法运行(报错: can‘t open file......No such file or directory)
Can an inexperienced college graduate switch to software testing?my real case
加密生活,Web3 项目合伙人的一天
勾股数元组 od js
随机推荐
C language _ structure pointer array function voting system
Word 表格跨页,仍然显示标题
力扣每日一题-第46天-704. 二分查找
初识C语言 -- 数组
leetcode-128: longest continuous sequence
软件测试工作3年了,谈谈我是如何从刚入门进阶到自动化测试的?
验证 XML 文档
Charging effect simulation
Gateway routing configuration
What have I experienced to become a tester who is harder than development?
The PC side determines the type of browser currently in use
Interprocess communication study notes
Parameter introduction and selection points of wireless module
[Map and Set] LeetCode & Niu Ke exercise
Gateway路由的配置方式
【AcWing 62nd Weekly Game】
leetcode-128:最长连续序列
cudaMemcpy学习笔记
leetcode-399: division evaluation
计算S=a+aa+…+aa…a