shell统计接口响应时长

1.将接口响应时长输出到文件

读取接口文件,统计每个接口响应耗时,计算平均值,输出到文件。

#!/bin/bash
paths=`cat path_file`
echo "=====接口响应耗时统计=====" > path_time.log
for path in $paths;do
echo -n $path >> path_time.log
echo -n "   " >> path_time.log
grep $path /home/www/log/java-app/koala-course/koala-course.log | grep -aoP '(?<=total=)\d+' | awk '{a++;b+=$1}END{print a,b,b/a}' >> path_time.log 2>&1
done

path_file文件内容如下:

/excellentCourse/selectUserCourse
/excellentCourse/selectUserCourseHistory
/excellentCourse/selectUserCourseDetail
/excellentCourse/reLearnCourse
/excellentCourse/selectSharePageData
/excellentCourse/selectCourseBasicInfo
/excellentCourse/selectSharedData
/WritingCourse/queryUserCourse
/WritingCourse/queryPackageCourse
/WritingCourse/selectLessonBaseInfo

2.将接口响应时长通知到钉钉群

#!/bin/bash
paths=`cat path_file`
content="=====接口响应耗时统计=====\n"
content="$content 接口描述 接口路径 调用次数 响应总时长  平均时长\n"
for desc_path in $paths;do
desc_path=${desc_path//:/ }
arr=($desc_path)
desc=${arr[0]}
path=${arr[1]}
data=$(grep $path /home/www/log/java-app/koala-course/koala-course.log | grep -aoP '(?<=total=)\d+' | awk '{a++;b+=$1}END{print a,b,b/a}')
content="$content  $desc $path              $data\n"
done
echo $content
curl "https://oapi.dingtalk.com/robot/send?access_token=你的token" -H 'Content-Type:application/json' -d "{\"msgtype\":\"text\",\"text\":{\"content\":\"$content\"}}"                                                                               

path_file文件内容如下:

用户今日课程+明日预报:/excellentCourse/selectUserCourse
用户历史课程:/excellentCourse/selectUserCourseHistory
课文详情:/excellentCourse/selectUserCourseDetail
查看课文简要信息:/excellentCourse/selectCourseBasicInfo
查看课文报告:/excellentCourse/selectSharedData
多课包选课展示—低版本:/WritingCourse/queryUserCourse
查询具体课程包的课文:/WritingCourse/queryPackageCourse
获取班次基本信息:/WritingCourse/selectLessonBaseInfo

第二个path_file文件,加了文字注释,算接口路径按照:分隔,取注释和路径,把执行接口拼接成字符串,发到钉钉群里,可以再做个定时任务,定时将前一天的接口响应时长发到群里面,有时间优化接口速度

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章