性能测试工具 -- Gatling

本文详细介绍Gatling性能测试工具的安装与使用,包括环境搭建、案例演示及常见应用场景,如HTTP请求模拟、循环控制、错误处理等,助力高效进行性能测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景:近期调研和使用了一下Gatling,这款功能强大的性能测试工具,这里把学习到的一丢丢知识点做简单总结和整理

环境:gatling-charts-highcharts-bundle-3.31

目录

1.简介

2.安装Gatling

3.案例

4.Gatling的常用场景

1.简介

Gatling这款测试工具,具有高性能高稳定性,方便的代码维护管理等特点。官网(https://gatling.io/)的介绍中,Gatling分为免费版和企业版。这里使用的是免费版。

2.安装Gatling

下载地址:https://gatling.io/#/download。直接解压文件即可。

文件目录结构及说明如下:

  • bin:存放gatling.bat/ gatling.sh(Linux环境使用)和recorder.bat/ recorder.sh脚本。.bat文件是在Windows上使用, .sh文件是在Linux上使用。其中recorder脚本用来启动录制脚本的GUI,gatling脚本用来运行测试
  • conf:Gatling自身的一些配置
  • lib:Galing自身依赖的库文件
  • results:存放测试报告
  • user-files:存放测试脚本

注意:Gatling依赖Java8及以上版本,因此,在使用之前,请务必配置好JDK以及环境变量。

3.案例

使用的案例网上已经有很多,这里不再赘述。可参考如下几个文:

https://www.jianshu.com/p/f0b32004c819

https://testerhome.com/articles/16531

4.Gatling的常用场景

1)scenario:启动新场景。如,scenario("My Scenario")。这里除了制表符"\t"外,其余字符均可以输入

2)exec:执行测试。该方法中通常模拟过程中发送的请求(如,HTTP, LOAD, POP, IMAP等)

e.g.

scenario("My Scenario")
    .exec(http("Get Homepage").get("http://github.com/gatling/gatling"))

3)pause:当用户看到页面时,如果需要选择页面其他内容,可以选择点击另一链接。如果要模拟这样的行为,需要用pause方法

  • 固定的暂停时间:
    • pause(duration: Duration)
    • pause(duration: String, unit: TimeUnit=TimeUnit.SECONDS)
    • pause(duration: Expression[Duration])
  • 均匀的随机暂停时间:
    • pause(min: Duration, max: Duration)
    • pause(min: String, max: String, unit: TimeUnit)
    • pause(min: Expression[Duration], max: Expression[Duration])

4)rendezVous:集合点。等待用户达到指定数量。如:rendezVous(users: Int)

5)repeat:指定重复循环次数

repeat(times, counterName){
    myChain
}

*********** 循环 ***********

6)foreach:对指定序列中的每个元素重复循环

foreach(sequenceName, elementName, counterName) {
  myChain
}

7)during:在指定的时间内遍历循环

during(duration, counterName, exitASAP) {
  myChain
}

8)asLongAs:只要条件满足,就遍历循环

asLongAs(condition, counterName, exitASAP) {
  myChain
}

9)doWhile:类似asLongAs,但条件是在循环后判断

doWhile(condition, counterName) {
  myChain
}

10)asLongAsDuring:只要满足条件且未达到持续时间,就遍历循环

asLongAsDuring(condition, duration, counterName) {
  myChain
}

11)doWhileDuring:类似asLongAsDuring,但条件是在循环后判断

doWhileDuring(condition, duration, counterName) {
  myChain
}

12)forever:一直循环

forever(counterName) {
  myChain
}

13)doIf:如果满足条件,则执行操作

doIf("${myBoolean}") {
  // executed if the session value stored in "myBoolean" is true
  exec(http("...").get("..."))
}

14)doIfEquals:如果测试条件是比较两个值,则可以使用该场景

doIfEquals("${actualValue}", "expectedValue") {
  // executed if the session value stored in "actualValue" is equal to "expectedValue"
  exec(http("...").get("..."))
}

15)doIfOrElse:与doIf类似,但如果条件的判定结果为false,则回退

doIfOrElse(session => session("myKey").as[String].startsWith("admin")) {
  // executed if the session value stored in "myKey" starts with "admin"
  exec(http("if true").get("..."))
} {
  // executed if the session value stored in "myKey" does not start with "admin"
  exec(http("if false").get("..."))
}

16)doIfEqualsOrElse:与doIfEquals类似,但条件为false时会回退

doIfEqualsOrElse(session => session("actualValue").as[String], "expectedValue") {
  // executed if the session value stored in "actualValue" equals to "expectedValue"
  exec(http("if true").get("..."))
} {
  // executed if the session value stored in "actualValue" is not equal to "expectedValue"
  exec(http("if false").get("..."))
}

17)doSwitch:条件开关

doSwitch("${myKey}")( // beware: use parentheses, not curly braces!
  key1 -> chain1,
  key2 -> chain2
)

18)doSwitchOrElse:与doSwitch类似,如果未满足任何开关条件,则使用缺省

doSwitchOrElse("${myKey}")(
    key1 -> chain1,
    key2 -> chain2
)(
    myFallbackChain
)

********** 错误处理 *********

19)tryMax:如果发生错误,则用户将绕过myChain的其余部分并从头开始尝试。times为最大尝试次数,counterName可选

tryMax(times, counterName) {
  myChain
}

20)exitBlockOnFail:与tryMax类似,但不会在失败时循环

exitBlockOnFail {
  myChain
}

21)exitHereIfFailed:如果前面有错误,可选择该方法退出

exitHereIfFailed
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值