zoukankan      html  css  js  c++  java
  • 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)
    
  • 相关阅读:
    JZ初中OJ 2266. 古代人的难题
    JZ初中OJ 1341. [南海2009初中] water
    JZ初中OJ 1340. [南海2009初中] jumpcow
    JZ初中OJ 2000. [2015.8.6普及组模拟赛] Leo搭积木
    JZ初中OJ 1999.[2015.8.6普及组模拟赛] Wexley接苹果
    Unity Android平台下插件/SDK开发通用流程
    UNITY接入支付宝(未测试可行)
    Unity接入支付宝(免写安卓代码,使用JAR方式)
    Unity之多态
    unity与android交互(1)与(2)网友的整理
  • 原文地址:https://www.cnblogs.com/CSunShine/p/11607476.html
Copyright © 2011-2022 走看看