Gatling學習筆記-Scenario(場景)


官方文檔

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

創建場景

scenario("My Scenario")

結構元素

exec

  • 基本使用
scenario("My Scenario")
  .exec(http("Get Homepage").get("http://github.com/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 here
    http("Get Homepage").get("http://github.com/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 } // Int
repeat("${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 true
  exec(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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM