并发测试工具seige使用介绍
需求
抽奖每天每个用户只能抽3次 需要验证高并发情况下同一用户是否存在超抽 即抽奖次数超过3次
# 模拟同一用户并发提交10次抽奖请求siege 'http://localhost:8080/lottery/draw/ POST userId=foo' -r 1 -c 10 -b SIEGE 4.0.2 Preparing 10 concurrent users for battle.The server is now under siege...HTTP/1.1 200 0.39 secs: 122 bytes ==> POST http://localhost:8080/lottery/draw/HTTP/1.1 200 0.39 secs: 122 bytes ==> POST http://localhost:8080/lottery/draw/HTTP/1.1 200 0.40 secs: 122 bytes ==> POST http://localhost:8080/lottery/draw/HTTP/1.1 200 0.40 secs: 122 bytes ==> POST http://localhost:8080/lottery/draw/HTTP/1.1 200 0.40 secs: 122 bytes ==> POST http://localhost:8080/lottery/draw/HTTP/1.1 200 0.40 secs: 122 bytes ==> POST http://localhost:8080/lottery/draw/HTTP/1.1 200 0.40 secs: 122 bytes ==> POST http://localhost:8080/lottery/draw/HTTP/1.1 200 0.41 secs: 122 bytes ==> POST http://localhost:8080/lottery/draw/HTTP/1.1 200 0.41 secs: 122 bytes ==> POST http://localhost:8080/lottery/draw/HTTP/1.1 200 0.41 secs: 122 bytes ==> POST http://localhost:8080/lottery/draw/Transactions: 10 hitsAvailability: 100.00 %Elapsed time: 0.41 secsData transferred: 0.00 MBResponse time: 0.40 secsTransaction rate: 24.39 trans/secThroughput: 0.00 MB/secConcurrency: 9.78Successful transactions: 10Failed transactions: 0Longest transaction: 0.41Shortest transaction: 0.39需求
同一用户同时进行兑奖和抽奖操作 可能会有数据紊乱的情况 如当前红包总额20元, 这时同时进行兑奖和抽奖操作, 假如抽奖中了10元, 那么最终结果可能会有如下的几种情形
正常情况 兑了一个20元的优惠券, 红包总额为10元
异常情况一 兑了一个20元的优惠券 但红包总额为30元
异常情况二 兑了一个20元的优惠券 但红包总额为0
使用siege验证上述情况
# 准备接口url文件cat test_urlshttp://localhost:8080/lottery/draw/ POST userId=foo # 抽奖http://localhost:8080/lottery/redeem/ POST userId=foo # 兑换# 并发提交上述两个接口siege -r 1 -c 2 -f test_urls -b SIEGE 4.0.2 Preparing 2 concurrent users for battle.The server is now under siege...HTTP/1.1 200 0.08 secs: 51 bytes ==> POST http://localhost:8080/lottery/redeem/HTTP/1.1 200 0.12 secs: 122 bytes ==> POST http://localhost:8080/lottery/draw/Transactions: 2 hitsAvailability: 100.00 %Elapsed time: 0.12 secsData transferred: 0.00 MBResponse time: 0.10 secsTransaction rate: 16.67 trans/secThroughput: 0.00 MB/secConcurrency: 1.67Successful transactions: 2Failed transactions: 0Longest transaction: 0.12Shortest transaction: 0.08参考文档
https://github.com/JoeDog/siege
关键字:siege, 服务端, 并发, secs