全部学科
NodeJS全栈
nodejs
Python全栈
python
小程序首页
📅 2026-05-18 10 分钟 ✍️ juanwangdev

Spring Boot压测工具使用

性能压测是验证系统承载能力的关键手段。

JMeter基本使用

创建测试计划

  1. 新建线程组:设置虚拟用户数和循环次数
  2. 添加HTTP请求:配置服务器地址和请求路径
  3. 添加监听器:聚合报告、查看结果树

核心配置

properties
# 线程组配置
线程数: 100
Ramp-Up时间: 10秒
循环次数: 10

# HTTP请求配置
服务器地址: localhost
端口: 8080
路径: /api/users
方法: GET

参数化压测

csv
# users.csv
username,password
user1,pass1
user2,pass2
user3,pass3

配置CSV Data Set Config,使用 ${username} 引用变量。

Gatling压测

依赖配置

scala
libraryDependencies ++= Seq(
  "io.gatling.highcharts" % "gatling-charts-highcharts" % "3.9.0" % Test,
  "io.gatling"            % "gatling-test-framework"    % "3.9.0" % Test
)

测试脚本示例

scala
class ApiSimulation extends Simulation {

  val httpProtocol = http
    .baseUrl("http://localhost:8080")
    .acceptHeader("application/json")

  val scn = scenario("API压测")
    .exec(http("获取用户列表")
      .get("/api/users")
      .check(status.is(200)))
    .pause(1)

  setUp(
    scn.injectOpen(
      rampUsers(100).during(10),
      constantUsersPerSec(50).during(60)
    )
  ).protocols(httpProtocol)
    .assertions(
      global.responseTime.max.lt(500),
      global.successfulRequests.percent.gt(99)
    )
}

wrk轻量压测

Bash
# 基本用法
wrk -t4 -c100 -d30s http://localhost:8080/api/users

# 参数说明
-t4: 4个线程
-c100: 100个连接
-d30s: 持续30秒

Lua脚本扩展

lua
-- post.lua
wrk.method = "POST"
wrk.body   = '{"name":"test"}'
wrk.headers["Content-Type"] = "application/json"
Bash
wrk -t4 -c100 -d30s -s post.lua http://localhost:8080/api/users

工具对比

工具特点适用场景
JMeterGUI界面,功能全面功能测试、接口压测
GatlingScala DSL,高性能CI/CD集成、大数据量
wrk命令行,轻量快速快速验证、基准测试
abApache自带,简单简单HTTP压测

压测指标关注

指标说明目标值
TPS每秒事务数越高越好
RT响应时间<500ms
错误率失败请求占比<1%
并发数同时在线用户按需设定

压测前确保环境隔离,避免影响生产系统。

要点总结

  • JMeter适合复杂场景,Gatling适合CI集成
  • wrk轻量快速,适合快速验证
  • 关注TPS、RT、错误率核心指标
  • 参数化模拟真实用户行为

📝 发现内容有误?点击此处直接编辑

← 上一篇 连接器参数调优
下一篇 → Spring Boot性能瓶颈分析
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库