当前位置:网站首页>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自动化测试/测试开发,走向高薪之路.
喜欢软件测试的小伙伴们,如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “点赞” “评论” “收藏” 一 键三连哦!
边栏推荐
- Inner monologue from a female test engineer...
- prometheus 监控概述
- MySql installation and configuration super detailed tutorial and simple method of building database and table
- 勾股数元组 od js
- C语言_结构体指针数组函数选票系统
- pycharm重命名后无法运行(报错: can‘t open file......No such file or directory)
- 计算S=a+aa+…+aa…a
- Jiuzhou Cloud was selected into the "Trusted Cloud's Latest Evaluation System and the List of Enterprises Passing the Evaluation in 2022"
- Word 表格跨页,仍然显示标题
- 软件测试基础接口测试-入门Jmeter,你要注意这些事
猜你喜欢

MySQL的安装教程(嗷嗷详细,包教包会~)

倍增、DFS序

Centos 7.9安装PostgreSQL14.4步骤

What have I experienced to become a tester who is harder than development?

uniapp使用第三方字体

mysql 视图

Jiuzhou Cloud was selected into the "Trusted Cloud's Latest Evaluation System and the List of Enterprises Passing the Evaluation in 2022"

成为比开发硬气的测试人,我都经历了什么?

手把手教你配置Jenkins自动化邮件通知

Fiddler抓包模拟弱网络环境测试
随机推荐
软件测试工作3年了,谈谈我是如何从刚入门进阶到自动化测试的?
加密生活,Web3 项目合伙人的一天
Kyushu cloud as cloud computing standardization excellent member unit
简易表白小页面
Word 表格跨页,仍然显示标题
coldfusion文件读取漏洞(CVE-2010-2861)
C语言_结构体指针数组函数选票系统
keep-alive cache component
软件测试基础接口测试-入门Jmeter,你要注意这些事
验证整数输入
12 pictures take you to fully understand service current limit, circuit breaker, downgrade, and avalanche
最大路径和
1782. Count the number of point pairs Double pointer
kotlin中函数作为参数和函数作为返回值实例练习
MySql的安装配置超详细教程与简单的建库建表方法
android的webview缓存相关知识收集
力扣刷题之有效的正方形(每日一题7/29)
观察者(observer)模式(一)
数字图像隐写术之JPEG 隐写分析
Gateway路由的配置方式