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文件,加了文字註釋,算接口路徑按照:分隔,取註釋和路徑,把執行接口拼接成字符串,發到釘釘羣裏,可以再做個定時任務,定時將前一天的接口響應時長髮到羣裏面,有時間優化接口速度

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