100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > Gatling学习笔记-Scenario(场景)

Gatling学习笔记-Scenario(场景)

时间:2020-11-03 18:04:38

相关推荐

Gatling学习笔记-Scenario(场景)

目录

创建场景结构元素 execpausepacerendezVous循环控制条件语句错误处理组定义协议定义

官方文档

// 官方示例scenario("Standard User").exec(http("Access Github").get("")).pause(2, 3).exec(http("Search for 'gatling'").get("/search?q=gatling")).pause(2)

创建场景

scenario("My Scenario")

结构元素

exec

基本使用

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

exec可以接受一个Session => Validation[T]类型的参数

exec { session =>// 打印session信息到控制台(debug模式下有效)println(session)// 可以在这里面修改/设置session中的数据,给接下来的请求使用// 请注意,返回值是一个session(scala中不需要return,语句执行的最后结果就是返回)session}exec { session =>// return a new session instance with a new "foo" attribute whose value is "bar"session.set("foo", "bar")}

不要在函数表达式中添加if等条件判断,如果需要根据session进行流程控制请使用其他的控制器方法,如:doIf、randomSwitch

exec { session =>if (someSessionBasedCondition(session)) {// just create a builder that is immediately discarded, hence doesn't do anything// you should be using a doIf herehttp("Get Homepage").get("/gatling/gatling")}session}

flattenMapIntoAttributes - 内置的会话表达

// assuming the Session contains an attribute named "theMap" whose content is :// Map("foo" -> "bar", "baz" -> "qix")exec(flattenMapIntoAttributes("${theMap}"))// the Session contains 2 new attributes "foo" and "baz".

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])

pace

用户控制action的执行频率,可用于控制QPS

固定的速度持续时间pace(duration: Duration)pace(duration: String, unit: TimeUnit = TimeUnit.SECONDS)pace(duration: Expression[Duration])均匀随机步长pace(min: Duration, max: Duration)pace(min: String, max: String, unit: TimeUnit)pace(min: Expression[Duration], max: Expression[Duration])

forever(pace(5 seconds).exec(pause(1 second, 4 seconds) // 将每5秒运行一次,而不管使用什么暂停时间))

rendezVous

集合点

rendezVous(users: Int)该方法接受等待的用户数量,当满足达到的用户数量时才会继续下面的执行

循环控制

repeat

重复循环指定的次数

语法:

repeat(times, counterName) {myChain}

repeat(20) { myChain } // Intrepeat("${myKey}") { myChain } // EL表达式repeat(session => session("foo").as[Int]) { myChain } // 从session中拿到的Int属性repeat((session) => 10) { myChain } // 任何返回值为Int均可

foreach

语法:

foreach(sequenceName, elementName, counterName) {myChain}

during

语法:

during(duration, counterName, exitASAP) {myChain}

duration 默认seconds,也可以使用Duration500 milliseconds

counterName 可选参数

exitASAP 可选参数,默认为true

asLongAs

语法:

asLongAs(condition, counterName, exitASAP) {myChain}

condition是一个session function,返回值是boolean值

counterName可选参数

exitASAP可选参数,默认为false,如果为true,将对循环内的每个元素评估条件,可能导致在到达迭代结束之前退出。

doWhile

语法:

doWhile(condition, counterName) {myChain}

condition是一个session function,返回值是boolean值

counterName可选参数

asLongAsDuring

asLongAsDuring(condition, duration, counterName) {myChain}

condition是一个session function,返回值是boolean值

duration是一个整数或者一个duration expressed(500 milliseconds)

counterName可选参数

forever

forever(counterName) {myChain}

counterName可选参数

条件语句

doIf

支持***

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

支持session表达式

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

doIfEquals

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

doIfOrElse

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("..."))}

doIfEqualsOrElse

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("..."))}

doSwitch

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

doSwitchOrElse

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

randomSwitch

randomSwitch( // beware: use parentheses, not curly braces!percentage1 -> chain1,percentage2 -> chain2)

randomSwitchOrElse

randomSwitchOrElse( // beware: use parentheses, not curly braces!percentage1 -> chain1,percentage2 -> chain2) {myFallbackChain}

uniformRandomSwitch

uniformRandomSwitch( // beware: use parentheses, not curly braces!chain1,chain2)

roundRobinSwitch

roundRobinSwitch( // beware: use parentheses, not curly braces!chain1,chain2)

错误处理

tryMax

tryMax(times, counterName) {myChain}

counterName可选参数

exitBlockOnFail

exitBlockOnFail {myChain}

exitHereIfFailed

exitHereIfFailed

如果用户之前出现了错误,则从这里开始退出该场景。

组定义

group(groupName) {myChain}

协议定义

scn.inject(atOnceUsers(5)).protocols(httpProtocol)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。