当前位置:网站首页>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自动化测试/测试开发,走向高薪之路.
喜欢软件测试的小伙伴们,如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “点赞” “评论” “收藏” 一 键三连哦!
边栏推荐
- pc端判断当前使用浏览器类型
- Shell 脚本循环遍历日志文件中的值进行求和并计算平均值,最大值和最小值
- 基于FPGA的售货机
- cudaMemcpy学习笔记
- Crypto Life, a day in the life of a Web3 project partner
- android的webview缓存相关知识收集
- What have I experienced when I won the offer of BAT and TMD technical experts?
- Can an inexperienced college graduate switch to software testing?my real case
- 蛮力法/邻接表 广度优先 有向带权图 无向带权图
- What is the ideal college life?
猜你喜欢

rpm install postgresql12

MySQL installation tutorial (detailed, package teaching package~)

设置浏览器滚动条样式

pycharm重命名后无法运行(报错: can‘t open file......No such file or directory)

斩获BAT、TMD技术专家Offer,我都经历了什么?

软件测试基础接口测试-入门Jmeter,你要注意这些事

1782. Count the number of point pairs Double pointer

"Cloud native's master, master and vulgar skills" - 2022 National New College Entrance Examination Volume I Composition

Parameter introduction and selection points of wireless module

mysql 视图
随机推荐
MySql的初识感悟,以及sql语句中的DDL和DML和DQL的基本语法
一个无经验的大学毕业生,可以转行做软件测试吗?我的真实案例
PDF 拆分/合并
1782. 统计点对的数目 双指针
Programmer's debriefing report/summary
keep-alive缓存组件
"Cloud native's master, master and vulgar skills" - 2022 National New College Entrance Examination Volume I Composition
设置浏览器滚动条样式
Can an inexperienced college graduate switch to software testing?my real case
C语言小程序 -- 常见经典练习题
聚簇索引和非聚簇索引到底有什么区别
MySql installation and configuration super detailed tutorial and simple method of building database and table
MySql的安装配置超详细教程与简单的建库建表方法
验证 XML 文档
Centos 7.9 install PostgreSQL14.4 steps
Teach you how to configure Jenkins automated email notifications
第一学年课程期末考试
最高月薪20K?平均薪资近万...在华为子公司工作是什么体验?
Multiplication, DFS order
【flask入门系列】Flask-SQLAlchemy的使用