编辑 最后修改时间 2023/07/26 18:18
压测-wrk
github源码 https://github.com/wg/wrk
安装
#获取源码
cd /usr/local/
git clone https://github.com/wg/wrk.git
#make编译wrk
cd /usr/local/wrk
make
#将wrk加入全局命令
ln -s /usr/local/wrk/wrk /usr/sbin/
#测试命令是否安装完成
wrk --version
wrk 4.1.0 [epoll] Copyright (C) 2012 Will Glozer
Usage: wrk <options> <url>
Options:
-c, --connections <N> Connections to keep open
-d, --duration <T> Duration of test
-t, --threads <N> Number of threads to use
-s, --script <S> Load Lua script file
-H, --header <H> Add header to request
--latency Print latency statistics
--timeout <T> Socket/request timeout
-v, --version Print version details
Numeric arguments may include a SI unit (1k, 1M, 1G)
Time arguments may include a time unit (2s, 2m, 2h)
使用
#新建目录用于写测试脚本
mkdir /usr/local/wrk/sh
新建文件并写入 demo.sh 命令用于生成 .lua 文件执行测试任务
vim /usr/local/wrk/sh/demo.sh
#-----------------------------------------文件内容如下 demo 仅供参考 begin
#!/bin/bash
#serverKey
SERVERKEY="000001"
#钉钉token
DD_TOKEN="98bd965653e2d9cc5260b12xxxxxxxxxxxx"
#pc-flatform token
PC_FLATFORM_TOKEN="a7969d3bcb351xxxxxxxxxxxxx"
#投票body
BODY_VOTE='{"course_id":"34d382bfa601f196366767xxxxxxxxxx","options":["xxxxxxxxxxxa6c3xxxxxxxxxxxxx"]}'
#投票选项url
URL_VOTE='ec58f67xxxxxxxxxxxxxxxxxxxxx8d3440%7Ca23xxxxxx1f7c901c4cxxxxxxxxxxxxxx'
#course_id
COURSE_ID='1e0bc5eedxxxxxxxxxxxxxxxxxxxxxxxxxxx'
#vote_id=''
VOTE_ID='401b6eaxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
#venue_num
VENUE_NUM=666
#域名url
HTTP_URL='https://t.jsx6.com/fast'
#命令参数列表
COMMAND='polling(pc-partform)|home(首页)|vote(投票)|barrage(上墙)|sigin(签到)'
#停止标志
STOP="[\033[5;31m停止\033[0m]"
#警告标志
WARM="[\033[5;31m警告\033[0m]"
#启动标志
START="[\033[5;32m启动\033[0m]"
if [ ! -n "$1" ];then
echo -e "接口:$COMMAND 线程:{4,8,16} 并发:{500,1000,2000} 时间(秒){10,30,60} $WARM"
exit
fi
if [ $# != "4" ];then
echo -e "接口:$COMMAND 线程:{4,8,16} 并发:{500,1000,2000} 时间(秒){10,30,60} $WARM"
exit
fi
#加载测试脚本url
LUAURL="/usr/local/wrk/sh/demo.lua"
if [ ! -f $LUAURL ]; then
touch $LUAURL
fi
export LUA=$LUAURL
case $1 in
#pc-partform:分会场获取投票上墙签到等信息
polling)
#生成测试脚本
PCLUA='wrk.method = "GET"\nwrk.headers["Content-Type"] = "application/x-www-form-urlencoded/json"\nwrk.headers["serverKey"] = "'$SERVERKEY'"\nwrk.headers["token"] ="'$PC_FLATFORM_TOKEN'"'
echo -e '接口测试:polling' $START
echo -e $PCLUA > $LUA
wrk -t$2 -c$3 -d$4s --script=$LUA --latency $HTTP_URL/v2/demo/test1/$COURSE_ID/$VENUE_NUM?sign_id=0\&next_start=0\&brrage_id=0
;;
#首页接口:getHomePageData
home)
#生成首页测试脚本
HOMELUA='wrk.method = "GET"\nwrk.body = "this is body"\nwrk.headers["Content-Type"] = "application/x-www-form-urlencoded/json"\nwrk.headers["serverKey"] = "'$SERVERKEY'"\nwrk.headers["token"] ="'$DD_TOKEN'"'
echo -e '接口测试:getHomePageData' $START
echo -e $HOMELUA > $LUA
wrk -t$2 -c$3 -d$4s --script=$LUA --latency $HTTP_URL/v2/demo/test2
;;
#签到接口:signIn
signin)
SIGINLUA=''
echo -e '接口测试:signIn' $START
;;
#上墙接口:barrage
barrage)
CONTENT='{"course_id":"'$COURSE_ID'","content":"'$(date +%Y-%m-%d\ %H:%M:%S)'"}'
BARRAGELUA="wrk.method = 'POST'\n--wrk.body = '$CONTENT'\nwrk.headers['Content-Type'] = 'application/json'\nwrk.headers['serverKey'] = '$SERVERKEY'\nwrk.headers['token'] ='$DD_TOKEN'\n\ncounter = 0\nrequest = function()\n path='$HTTP_URL/v2/demo/test3/$SERVERKEY/$COURSE_ID/$VENUE_NUM'\n --wrk.headers['X-Counter'] = counter\n counter = counter +1\n wrk.body = '{\"course_id\":\"$COURSE_ID\",\"content\":\"上墙测试: ' .. counter .. '\"}'\n return wrk.format(nil,path)\nend"
echo -e '接口测试:barrage' $START
echo -e $BARRAGELUA > $LUA
wrk -t$2 -c$3 -d$4s --script=$LUA --latency $HTTP_URL/v2/demo/test3/$SERVERKEY/$COURSE_ID/$VENUE_NUM
;;
#投票接口:vote
vote)
#生成投票测试脚本
VOTELUA='wrk.method = "GET"\nwrk.body = "this is body"\nwrk.headers["Content-Type"] = "application/x-www-form-urlencoded/json"\nwrk.headers["serverKey"] = "'$SERVERKEY'"\nwrk.headers["token"] ="'$DD_TOKEN'"'
echo -e '投票测试:vote' $START
echo -e $VOTELUA > $LUA
wrk -t$2 -c$3 -d$4s --script=$LUA --latency $HTTP_URL/v2/demo/test6/$COURSE_ID/$VOTE_ID/$VENUE_NUM/$URL_VOTE
;;
*)
echo -e "接口:$COMMAND 线程:{4,8,16} 并发:{500,1000,2000} 时间(秒){10,30,60} $WARM"
exit
;;
esac
#-----------------------------------------文件内容如下 demo 仅供参考 end
#为 demo.sh 增加可执行权限
chmod +x demo.sh
#执行 demo.sh 进行测试
./demo.sh barrage 1 1 1
接口测试:barrage [启动]
Running 1s test @ http://t.jsx6.com/fast/v2/demo/test3/000001/1e0bc5eedxxxxxxxxxxxxxxxxxxxxxxxxxxx/666
1 threads and 1 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 3.02ms 1.54ms 16.49ms 93.96%
Req/Sec 348.80 43.59 404.00 70.00%
Latency Distribution
50% 2.67ms
75% 2.84ms
90% 3.03ms
99% 10.67ms
347 requests in 1.00s, 144.34KB read
Requests/sec: 346.91
Transfer/sec: 144.31KB
异常记录
/bin/sh: -c: line 0: syntax error near unexpected token `('
/bin/sh: -c: line 0: `PATH=obj/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/d/Program Files (x86)/NetSarang/Xshell 6:/mnt/d/Java/javapath:/mnt/c/Program Files (x86)/Intel/Intel(R) Management Engine Components/iCLS:/mnt/c/Program Files/Intel/Intel(R) Management Engine Components/iCLS:/mnt/c/Windows/System32:/mnt/c/Windows:/mnt/c/Windows/System32/wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0:/mnt/c/Windows/System32/OpenSSH:/mnt/c/Program Files (x86)/Intel/Intel(R) Management Engine Components/DAL:/mnt/c/Program Files/Intel/Intel(R) Management Engine Components/DAL:/mnt/c/Program Files (x86)/Intel/Intel(R) Management Engine Components/IPT:/mnt/c/Program Files/Intel/Intel(R) Management Engine Components/IPT:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/d/Program Files/nodejs:/mnt/d/Go/bin:/mnt/d/Program Files/Git/cmd:/mnt/d/Program Files/TortoiseGit/bin:/mnt/c/Program Files/dotnet:/mnt/d/phpStudy/php/php-7.1.12-nts:/mnt/d/phpStudy/php/php-5.3.29-nts:/mnt/d/phpStudy/MySQL/bin:/mnt/d/MinGW/bin:/mnt/d/ffmpeg_builds/bin:/mnt/d/Program Files (x86)/Nmap:/mnt/d/gource-0.47:/mnt/c/php-sdk/bin:/mnt/c/Users/13485/AppData/Roaming/Composer/vendor/bin:/mnt/c/Program Files/Intel/WiFi/bin:/mnt/c/Program Files/Common Files/Intel/WirelessCommon:/mnt/c/Users/13485/AppData/Local/Microsoft/WindowsApps:/mnt/d/bat:/mnt/c/Users/13485/AppData/Roaming/npm:/snap/bin luajit -b /usr/local/wrk/src/wrk.lua /usr/local/wrk/obj/bytecode.o'
Makefile:64: recipe for target 'obj/bytecode.o' failed
make: *** [obj/bytecode.o] Error 1
原因:
sudo使用"by accident"工作,因为sudo用户有一个 $PATH env变量不包含空格.
我的解决方案更好,因为它解决了根问题: 使用空格处理 $PATH 变量,处理这个存储库的人似乎完全忽略了PR
参考地址: https://github.com/RobertoPrevato/wrk/commit/9d33c16d70d7b308650e89a7bd6b6de49430415e
解决:
使用sudo构建wrk
sudo make