当前位置:网站首页>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自动化测试/测试开发,走向高薪之路.
喜欢软件测试的小伙伴们,如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “点赞” “评论” “收藏” 一 键三连哦!
边栏推荐
- Centos 7.9安装PostgreSQL14.4步骤
- Maximum monthly salary of 20K?The average salary is nearly 10,000... What is the experience of working in a Huawei subsidiary?
- [1153]mysql中between的边界范围
- pc端判断当前使用浏览器类型
- 蛮力法/邻接矩阵 广度优先 有向带权图 无向带权图
- coldfusion8后台计划任务拿shell
- uniapp使用第三方字体
- 类似 MS Project 的项目管理工具有哪些
- Charging effect simulation
- 软件测试基础接口测试-入门Jmeter,你要注意这些事
猜你喜欢
曼城推出可检测情绪的智能围巾,把球迷给整迷惑了
case语句的综合结果,你究竟会了吗?【Verilog高级教程】
数字图像隐写术之卡方分布
Maximum monthly salary of 20K?The average salary is nearly 10,000... What is the experience of working in a Huawei subsidiary?
ROS Action通信
Fiddler抓包模拟弱网络环境测试
CV-Model [3]: MobileNet v2
华为od 转骰子 js
rpm install postgresql12
I have been working in software testing for 3 years, how did I go from just getting started to automated testing?
随机推荐
leetcode-399:除法求值
I have been working in software testing for 3 years, how did I go from just getting started to automated testing?
Fiddler抓包模拟弱网络环境测试
VSCode Plugin: Nested Comments
link与@import的区别
What is the ideal college life?
两个有序数组间相加和的Topk问题
Programmer's debriefing report/summary
[1153]mysql中between的边界范围
Simple confession page
验证 XML 文档
MySQL (6)
太阳能板最大面积 od js
计算S=a+aa+…+aa…a
rpm安装postgresql12
Teach you how to configure Jenkins automated email notifications
CV-Model【3】:MobileNet v2
MySql installation and configuration super detailed tutorial and simple method of building database and table
数字图像隐写术之JPEG 隐写分析
软件测试报告有哪些内容?