Linux服务器上shell脚本批量循环测试接口连通性,bash工具循环测试curl性能

使用curl的-w选项来输出各种时间信息

  • -o /dev/null 用于丢弃响应体,只关心头部信息
  • -s 用于静默模式,不输出进度信息
  • %{http_code} 输出HTTP状态码
  • %{time_namelookup} 输出DNS解析时间
  • %{time_connect} 输出连接时间
  • %{time_total} 输出总时间(包括响应时间)

结合shell脚本的循环执行逻辑就可以批量测试了。
实例:

#!/bin/bash
# 测验URL访问速度 Test URL access speed
# author: xiongzaiqiren

# 测试次数
num=3
testUrl="https://www.bing.com"
# testUrl="http://10.10.28.121/#/open/"
# testUrl="http://wenxuebank.com/book/info/b2d5e8"

start_time=$(date +%Y%m%d%H%M%S)
echo -e ">>>总次数=${num} URL=${testUrl} 开始>>> \n" | tee -a "./log_testUrl_${start_time}.log"

while true; do
        echo ${num}
  if [ ${num} -lt 1 ]; then
    echo -e "<<< 完成 <<< \n" | tee -a "./log_testUrl_${start_time}.log"
        break
  else
        date_now=$(date +%Y/%m/%d[%H:%M:%S])
        total_time=$(curl -w "StatusCode: %{http_code}, 总耗时: %{time_total}, tcp连接建立时间: %{time_connect}, 服务端处理时间: %{time_starttransfer}, DNS解析时间: %{time_namelookup}, 网络响应时间: %{time_pretransfer}, 握手时间: %{time_redirect}" -o /dev/null -s "$testUrl")
        total_time_show=$(echo -e "$total_time"|head -1|awk '{print $4}')
        total_time_sec=$(echo -e "$total_time"|head -1|awk '{print $4}'|awk -F ['.'] '{print $1}')
        if [ ${total_time_sec} -lt 1 ]; then
                echo -e "【$date_now】 URL耗时 ${total_time_show} seconds,小于1s 正常\n ${total_time} \n" | tee -a "./log_testUrl_${start_time}.log"
        else
                echo -e "\033[1;31m############### \n 【$date_now】 URL耗时 ${total_time_show} seconds,大于1s 异常 \n ${total_time}\n################\033[0m\n" | tee -a "./log_testUrl_${start_time}.log"
        fi
        
        let num--
        sleep .5
  fi
done
# 使用curl的-w选项来输出各种时间信息  
# -o /dev/null 用于丢弃响应体,只关心头部信息  
# -s 用于静默模式,不输出进度信息  
# %{http_code} 输出HTTP状态码  
# %{time_namelookup} 输出DNS解析时间  
# %{time_connect} 输出连接时间  
# %{time_total} 输出总时间(包括响应时间)  

以上代码复制到.sh文件里(例如 testSingleUrlAccessSpeed.sh )然后赋予+x可执行权限。
执行过程及结果如图:

当然,也可以直接一条命令测试:

for i in {1..1000}; do \
curl -w "StatusCode: %{http_code} 总耗时: %{time_total} tcp连接建立时间: %{time_connect} 服务端处理时间: %{time_starttransfer} DNS解析时间: %{time_namelookup} 网络响应时间: %{time_pretransfer} 握手时间: %{time_redirect}" -o /dev/null -s  'http://wenxuebank.com/book/info/b2d5e8' -H 'X-MyUser-Id:1023174472804585472'; \
done | awk '{if ($4 > 1 || $10 > 1) print $0}'

也可以直接测试 DNS解析:

for i in {1..100}; do dig @192.168.0.4 xxx.xxx.com|grep "Query time" >>dns_1056.query.txt; sleep 1s;done

循环遍历测验URL访问速度

我们可以结合 for ... in ... 循环,和 while ... 循环来实现多个Url组合与多次访问测验。
原理:

  • 首先定义两个变量,分别将多个目标Url组合,与期望循环测验的次数预设起来;
  • 其次在Traverse_URLs()方法实现遍历预设的Url集合,在Loop_Nums()方法实现每个Url循环使用测验;
  • 第三在Measure_time()方法里实现Url测验核心代码。主要是通过执行curl命令并收集相关各项时间参数,以易于理解的格式输出。根据URL响应耗时时长判断正常或异常。
  • 最后,将脚本文件赋予可执行权限,执行测验即可。

完整代码:

#!/bin/bash     
# 循环遍历测验URL访问速度 Loop traversal test URL access speed
# author: xiongzaiqiren

# 测验目标URL的集合,多个之间用空格分隔  
urls=("http://wenxuebank.com/book/info/b2d5e8" "https://www.google.com" "https://www.bing.com")  
# 每个URL测验次数
num=2

######以下是业务逻辑,请勿改动!!!(The following is the business logic, please do not modify it !!!)
start_time=$(date +%Y%m%d%H%M%S)
echo -e ">>>循环遍历次数=${num} URL数量=${#urls[@]} 开始>>> \n" | tee -a "./log_testUrl_${start_time}.log"

# 定义一个函数来测量DNS解析时间  
Measure_time() {
        testUrl=$1
        echo -e "\033[0;32m Measure_time()> $testUrl \033[0m" | tee -a "./log_testUrl_${start_time}.log"

        date_now=$(date +%Y/%m/%d[%H:%M:%S])
        total_time=$(curl -w "StatusCode: %{http_code} 总耗时: %{time_total} tcp连接建立时间: %{time_connect} 服务端处理时间: %{time_starttransfer} DNS解析时间: %{time_namelookup} 网络响应时间: %{time_pretransfer} 握手时间: %{time_redirect}" -o /dev/null -s "$testUrl")
        total_time_show=$(echo -e "$total_time"|head -1|awk '{print $4}')
        total_time_sec=$(echo -e "$total_time"|head -1|awk '{print $4}'|awk -F ['.'] '{print $1}')
        if [ ${total_time_sec} -lt 1 ]; then
                echo -e "【$date_now】 URL耗时 ${total_time_show} seconds,小于1s 正常\n ${total_time} \n" | tee -a "./log_testUrl_${start_time}.log"
        else
                echo -e "\033[1;31m############### \n 【$date_now】 URL耗时 ${total_time_show} seconds,大于1s 异常 \n ${total_time}\n################\033[0m\n" | tee -a "./log_testUrl_${start_time}.log"
        fi
}

Loop_Nums() {
        local Single_url=$1
        local Single_num=$2
  while true; do
    # echo -e "\033[47;36m $Single_num \033[0m \033[1;36m $Single_url \033[0m" | tee -a "./log_testUrl_${start_time}.log"
    if [ ${Single_num} -lt 1 ]; then
        echo -e "单个URL已完成。" | tee -a "./log_testUrl_${start_time}.log"
        break
    else
        echo -e "\033[47;36m $Single_num \033[0m \033[1;36m $Single_url \033[0m" | tee -a "./log_testUrl_${start_time}.log"
        Measure_time "${Single_url}"
        
        let Single_num--
        sleep .5
    fi
  done
}

Traverse_URLs() {
  # 循环遍历URL数组  
  for url in "${urls[@]}"; do  
    echo -e "\033[47;30m -----Start----- ${url} \033[0m" | tee -a "./log_testUrl_${start_time}.log"
    # 测量DNS时间  
    Loop_Nums "${url}" $num  
    
    echo -e "\033[47;30m -----Complete----- ${url} \033[0m \n" | tee -a "./log_testUrl_${start_time}.log"
  done
}

Traverse_URLs


# 使用curl的-w选项来输出各种时间信息  
# -o /dev/null 用于丢弃响应体,只关心头部信息  
# -s 用于静默模式,不输出进度信息  
# %{http_code} 输出HTTP状态码  
# %{time_namelookup} 输出DNS解析时间  
# %{time_connect} 输出连接时间  
# %{time_total} 输出总时间(包括响应时间)  

将代码复制到你的Linux里面,文件命名为 testUrlAccessSpeed.sh,赋予可执行权限 sudo chmod +x testUrlAccessSpeed.sh 修改开头的两个变量的值(urls、num),然后就可以执行了。

示例:

执行过程记录在相同目录下的 log_testUrl_20241231xxx.log 日志文件里。
【完】

posted @   熊仔其人  阅读(91)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
历史上的今天:
2021-12-31 苹果手机卡死,强制关机方法
点击右上角即可分享
微信分享提示