服务端Http接口并发测试工具--siege使用介绍

并发测试工具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 hits
Availability:              100.00 %
Elapsed time:                0.41 secs
Data transferred:            0.00 MB
Response time:                0.40 secs
Transaction rate:           24.39 trans/sec
Throughput:                0.00 MB/sec
Concurrency:                9.78
Successful transactions:          10
Failed transactions:               0
Longest transaction:            0.41
Shortest transaction:            0.39

需求

同一用户同时进行兑奖和抽奖操作 可能会有数据紊乱的情况 如当前红包总额20元, 这时同时进行兑奖和抽奖操作, 假如抽奖中了10元, 那么最终结果可能会有如下的几种情形

正常情况 兑了一个20元的优惠券, 红包总额为10元
异常情况一 兑了一个20元的优惠券 但红包总额为30元
异常情况二 兑了一个20元的优惠券 但红包总额为0

使用siege验证上述情况

# 准备接口url文件
cat test_urls
http://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 hits
Availability:             100.00 %
Elapsed time:               0.12 secs
Data transferred:           0.00 MB
Response time:              0.10 secs
Transaction rate:          16.67 trans/sec
Throughput:             0.00 MB/sec
Concurrency:                1.67
Successful transactions:           2
Failed transactions:               0
Longest transaction:            0.12
Shortest transaction:           0.08

参考文档

https://github.com/JoeDog/siege

### 高并发压力测试方法与工具 对于高并发系统的压力测试,可以通过深入了解和监控关键参数来实现更精确的评估和优化[^1]。 压力测试的主要目标在于模拟真实环境下的负载情况,从而发现潜在瓶颈并提升系统性能。 #### 工具介绍 以下是几种常用的高并发压力测试工具及其特点: - **ab (Apache Benchmark)** ab 是 Apache 提供的一个简单易用的压力测试工具。它支持通过命令行快速设置请求总数 (`-n`) 和并发用户数 (`-c`)[^2]。例如,针对 URL `localhost/index.php` 进行一次总请求数为 1000、并发用户数为 100 的测试时,可运行以下命令: ```bash ab -n 1000 -c 100 http://localhost/index.php ``` 此外,ab 输出的结果可以帮助分析服务器响应时间、吞吐量以及其他性能指标[^4]。 - **Locust** Locust 是一种基于 Python 编写的灵活压力测试工具,特别适合用于 Web 应用程序的大规模并发测试。其优势在于能够自定义脚本以模拟复杂的业务逻辑,并允许动态调整用户的加载模式[^3]。下面是一个简单的 Locust 脚本示例: ```python from locust import HttpUser, task, between class WebsiteUser(HttpUser): wait_time = between(1, 5) @task def index(self): self.client.get("/") @task def about(self): self.client.get("/about") ``` - **Siege** Siege 是另一种功能强大的开源压力测试工具,尤其适用于 CentOS 系统中的部署场景。安装完成后可通过指定 URL 及其他选项发起多线程攻击,验证服务端在高压条件下的稳定性[^5]。基本使用方式如下所示: ```bash siege -c 100 -r 10 http://example.com/ ``` #### 测试策略建议 为了获得更加全面而可靠的测试数据,应遵循以下几个原则: - 明确设定具体的测试目标; - 设计合理的任务流,确保覆盖主要业务操作; - 记录详细的日志信息以便后续分析; - 结合实际需求调整各项参数直至达到预期效果为止。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值