• Gatling-Session


    概念:
    Session is a virtual user’s state.
    格式:
    Map[String, Any]

    存入数据

    • 通过 Feeders
    • 从响应中提取数据并保存(如 http check 中的 saveAs)
    • 通过 Session API 手动注入

    示例:todo

    feed(feeder)
    

    取得数据

    • 通过 Expression Language 取得
    • 通过 Session API 手动取得

    示例:todo

    "${attributeName}"
    

    Session API

    设置数据:

    // set(key: String, value: Any): Session    单个插入
    val scn = scenario("scn").during(30 seconds) {
      // 正确的用法
      exec(session => {
        session.set("a", "1")
          .set("b", "2")
          .set("c", "3")
          .set("d", "4")
          .set("e", "5")
      })
      // 错误的用法
      exec(session => {
        session.set("a", "1")
        session.set("b", "2")
        session.set("c", "3")
        session.set("d", "4")
        session.set("e", "5") // 上面的都会被丢弃掉
      }),
    }
    
    // setAll(newAttributes: (String, Any)*): Session    接收可变参数
    val scn = scenario("scn").during(30 seconds) {
      exec(session => {
        session.setAll(("a", "1"), ("b", "2"), ("c", "3"), ("d", "4"), ("e", "5"))
      })
    }
    
    // setAll(newAttributes: Iterable[(String, Any)]): Session    接收一个可迭代对象
    val scn = scenario("scn").during(30 seconds) {
      exec(session => {
        session.setAll(Array(("a", "1"), ("b", "2"), ("c", "3"), ("d", "4"), ("e", "5")))
      })
    }
    
    // reset    重置
    // reset all attributes but loop counters, timestamps and Gatling internals (baseUrl, caches, etc)
    val scn = scenario("scn").during(30 seconds) {
      exec(session => {
        session..reset
      })
    }
    

    使用数据:

    val scn = scenario("scn").during(30 seconds) {
      exec(session => {
        session.setAll(("a", "1"), ("b", "2"), ("c", "3"), ("d", "4"), ("e", "5"))
      }).exec(session => { // 注意:设置的Session数据需要在同一个链式调用下使用
        println(session)
        session
      })
    
      // 这里就取不到数据,是不同的Session
      exec(session => {
        println(session)
        session
      })
    }
    
    session.set("foo", "FOO")
    // 注意:session("foo")并不是foo的值,而是一个wrapper(包装)
    val attribute: SessionAttribute = session("foo")
    
    val scn = scenario("scn").during(30 seconds) {
      exec(session => {
        session.set("a", 1)
      }).exec(session => {
        // IDE里面这样写会报错,其实是没错的,不要理它
        val a1: Int = session("a").as[Int] // 也可能出现:NoSuchElementException || NumberFormatException || ClassCastException
        val a2: Option[Int] = session("a").asOption[Int] // 也可能出现:NumberFormatException || ClassCastException
        val a3: Validation[Int] = session("a").validate[Int]
        println(a1)
        a2 match {
          case Some(value) => {
            println(value)
          }
          case None => {
            println("None")
          }
        }
        a3 match {
          case Success(value) => {
            println(value)
          }
          case Failure(errorMessage) => {
            println(errorMessage)
          }
        }
        session
      })
    }
    

    Expression Language

    request("page").get("/foo?${bar}")
    
    "${foo.size()}"          // returns the size of `foo` if `foo` points to a Scala or Java collection attribute
    "${foo.random()}"        // returns a random element of `foo` if `foo` points to an indexed collection
    "${foo.exists()}"        // returns true if the session contains a `foo` attribute, false otherwise
    "${foo.isUndefined()}"   // returns true if the session doesn't contains a `foo` attribute, false otherwise
    "${foo(5)}"              // returns the 5th element of `foo` if `foo` points to an indexed collection
    "${foo(n)}"              // returns the n-th element of `foo` if `n` points to an Int and `foo` to an indexed collection or a Tuple
    "${foo.bar}"             // returns the value associated with key `bar` if `foo` points to a map
    "${foo._2}"              // returns the second element if `foo` points to a Tuple object (identical to idiomatic Scala Tuple syntax, 1 based index)
    "${foo.jsonStringify()}" // properly formats into a JSON value (wrap Strings with double quotes, deal with null)
    
    "${foo(0)(0)}"           // List[List[T]]
    "${foo.list.random()}"   // List[List[T]]
    
    // 转义前
    "$${foo}    $$${foo}"
    // 转义后
    "${foo}    $FOO"
    

    Validation(验证)

    Validation[T] has a type parameter T that is the type of the value in case of a success.

    • Success[T](value: T) that wraps a value in case of a success
    • Failure(message: String) that wraps a String error message
    // 创建实例的方法
    val foo: Validation[String] = Success("foo")
    val bar: Validation[String] = Failure("errorMessage")
    
    // 或者
    val foo: Validation[String] = "foo".success
    val bar: Validation[String] = "errorMessage".failure
    
    // Validation支持match匹配
    def display(v: Validation[String]) = v match {
      case Success(string) => println("success: " + string)
      case Failure(error)  => println("failure: " + error)
    }
    
    // map与flatMap
    // map用于连接一个不会失败的操作,因此返回一个原始值
    val foo = Success(1)
    val bar = foo.map(value => value + 2)
    
    // flatMap用于连接可能失败的操作,因此返回一个Validation
    val foo = Success("foo")
    val bar = foo.flatMap(value => Success("bar"))
    val baz = foo.flatMap(value => Failure("error"))
    
    // 在以上两种情况下,如果原始Validation失败,则不会调用链式函数
    
  • 相关阅读:
    Uboot的串口下载文件命令:loads / loadb / loady
    U-Boot中关于TEXT_BASE,代码重定位,链接地址相关说明
    u-boot-2014.04分析
    Spring MVC + Java 多文件上传及多文件中转上传
    Java 文件上传中转
    backdrop-filter 和filter 写出高斯模糊效果 以及两者区别
    解读浮动闭合最佳方案:clearfix
    JavaScript ES6中export及export default的区别
    webpack配置常用loader加载器
    chrome jsonView插件安装
  • 原文地址:https://www.cnblogs.com/CSunShine/p/11982732.html
Copyright © 2020-2023  润新知