shell写一个压测脚本
ab命令
ab是apache下面的一个性能压测工具
yum install -y httpd-tools
ab -n 1000 -c 10 http://xxxx
# -n 请求数 -c 并发数
基于ab写一个压测脚本
#!/bin/bash
requests=(1000 2000 10000)
concurrency=(10 100)
path="http://localhost:3000"
benchmark() {
if [ $3 -eq 0 ];then
ab -n $1 -c $2 $path 2>&1 | awk 'BEGIN {printf "%-10s %-15s %-8s %-8s %-8s\n", "Requests", "Concurrency", "Min(ms)", "Max(ms)", "Mean(ms)";printf "------------------------------------------------------ \n"} /Total:/ {printf "%-10s %-15s %-8s %-8s %-8s\n", "'$1'", "'$2'", $2, $6, $3}'
else
ab -n $1 -c $2 $path 2>&1 | awk '/Total:/ {printf "%-10s %-15s %-8s %-8s %-8s\n", "'$1'", "'$2'", $2, $6, $3}'
fi
}
index=0
for request in ${requests[@]}
do
for con in ${concurrency[@]}
do
benchmark $request $con $index
let 'index++'
done
done
压测结果:
Requests Concurrency Min(ms) Max(ms) Mean(ms)
------------------------------------------------------
1000 10 4 23 7
1000 100 9 52 39
2000 10 3 63 4
2000 100 9 42 31
10000 10 2 409 5
10000 100 9 1980 49
几个细节
- shell遍历数组
for request in ${requests[@]}
- awk中
/Total:/
必须放在BEGIN{printf ''}后面 - 管道符默认只传递标准输出,所以需要2>&1
- awk中printf中
%-8s
,负号表示左对齐,8表示占8位字符,s表示字符串 - shell中使用let进行数学计算