【问题标题】:Scala dynamic scenarioScala 动态场景
【发布时间】:2021-07-31 10:43:18
【问题描述】:

有没有办法像这样创建动态场景?下面的代码给出错误未找到:类型场景定义 my_test():场景 = {。我认为场景不需要导入

    def my_test() : scenario = {
    if(condition) 
        scenario(build_name)
        .exec(http("post"+build_name+"content")    
        .post(end_point_url)
        .header("Authorization", "Bearer "+ token)
        .body(RawFileBody(input_file_path)).asXml
        .check(status is 200))
     else
        scenario(build_name)
        .exec(http("post"+build_name+"content")    
        .post(end_point_url)
        .header("Authorization", "Bearer "+ token)
        .body(RawFileBody(input_file_path)).asJson
        .check(status is 200))
}



setUp(my_test()
    .inject(
        nothingFor(2 seconds),
        constantUsersPerSec(users).during(10 seconds)
    ))
    .assertions(
        global.responseTime.mean.lt(threshold = mean_response_time),
        global.successfulRequests.percent.gte(minSuccessPercent)
     )
    .protocols(httpConf)
    .assertions(
        forAll.responseTime.max.lt(threshold = responseTime)
    )

编辑 当我尝试使用

def my_test() = {
    if(condition) 
        scenario(build_name)
        .exec(http("post"+build_name+"content")    
        .post(end_point_url)
        .header("Authorization", "Bearer "+ token)
        .body(RawFileBody(input_file_path)).asXml
        .check(status is 200))
     else
        scenario(build_name)
        .exec(http("post"+build_name+"content")    
        .post(end_point_url)
        .header("Authorization", "Bearer "+ token)
        .body(RawFileBody(input_file_path)).asJson
        .check(status is 200))
}

出现以下错误

17:10:01.582 [main][ERROR][ZincCompiler.scala:151] i.g.c.ZincCompiler$
- ... .scala:132:2: method my_test has return statement; needs result type
        return scenario(build_name)
        ^ 17:10:01.645 [main][ERROR][ZincCompiler.scala:122] i.g.c.ZincCompiler$ - one error found 17:10:01.648 [main][ERROR][ZincCompiler.scala:219] i.g.c.ZincCompiler$ - Compilation crashed sbt.internal.inc.CompileFailed: null
        at sbt.internal.inc.AnalyzingCompiler.call(AnalyzingCompiler.scala:242)

编辑

感谢所有为这篇文章做出贡献的人。我必须使用导入语句 io.gatling.core.structure.{PopulationBuilder, ScenarioBuilder} 将场景修改为 ScenarioBuilder,如提到的 here

【问题讨论】:

标签: scala gatling


【解决方案1】:

正如编译器所说,scenario 是未知的(顺便说一下,它实际上不是 Gatling 类型)而且它不是您的方法返回的类型。

您可以删除返回类型并让编译器为您推断它或编写正确的返回类型。

编辑:Gatling 使用的 Zinc 编译器需要明确的返回类型,因此您必须找出正确的类型(可能是 ScenarioBuilder)。

【讨论】:

  • 盖尔,我不擅长scala。我尝试将我的代码修改为 def my_test() : Unit = { ..... 但出现以下错误 16:46:01.094 封闭方法 my_test 具有结果类型 Unit: return value discarded return scenario(build_name) ^ .... .scala:146:3: value injection 不是 Unit 的成员 可能原因:在“value injection”之前可能缺少分号? .inject( ^ [main][ERROR][ZincCompiler.scala:122] i.g.c.ZincCompiler$ - 发现一个错误 [main][ERROR][ZincCompiler.scala:219] i.g.c.ZincCompiler$ - 编译崩溃 sbt.internal.inc.CompileFailed :空
  • 我建议您先了解一下 Scala。 Unit 是一种类型。我建议不要写返回类型:def my_test() = { ... }
  • 好的,Gatling 中使用的 Zinc 编译器可能需要显式类型。然后你需要找出表达式的类型,也许你的IDE可以帮助你?我现在没有,也许ScenarioBuilder 是您正在寻找但不确定的类型..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多