循环遍历列表以调用不同的函数
Loop through list to call different functions
我有一个整数列表,需要遍历列表并调用具有相同输入参数的不同函数,这是我的代码:
def callRandomFunctions(config: config, prefix: String): ChainBuilder = {
val randomList = Random.shuffle(List(1, 2, 3, 4))
randomList.foreach { _ =>
_ match {
case 1 => func1(config, prefix)
case 2 => func2(config, prefix)
case 3 => func3(config, prefix)
case 4 => func4(config, prefix)
}
}
}
def func1(config: config, prefix: String): ChainBuilder = {...}
def func2(config: config, prefix: String): ChainBuilder = {...}
def func3(config: config, prefix: String): ChainBuilder = {...}
def func4(config: config, prefix: String): ChainBuilder = {...}
并得到这些错误:
missing parameter type for expanded function
[error] The argument types of an anonymous function must be fully known. (SLS 8.5)
[error] Expected type was: ?
[error] _ match {
[error] ^
[error] type mismatch;
[error] found : Unit
[error] required: io.gatling.core.structure.ChainBuilder
[error] randomList.foreach {
[error] ^
[error] two errors found
您可以简单地这样做:
randomList.foreach {
case 1 => func1(config, prefix)
case 2 => func2(config, prefix)
case 3 => func3(config, prefix)
case 4 => func4(config, prefix)
}
它将作为模式匹配传递给 foreach
回调的数字。
您的代码的另一个问题是您希望从 callRandomFunctions
return ChainBuilder
但您使用的 foreach
是终止运算符 return正在 Unit
。您可能想使用 map
并将 return 类型更改为 List[ChainBuilder]
:
def callRandomFunctions(config: Config, prefix: String): List[ChainBuilder] = {
val randomList = Random.shuffle(List(1, 2, 3, 4))
randomList.map {
case 1 => func1(config, prefix)
case 2 => func2(config, prefix)
case 3 => func3(config, prefix)
case 4 => func4(config, prefix)
}
}
简单的打乱函数,然后直接调用。
def func1(config: config, prefix: String): ChainBuilder = ???
def func2(config: config, prefix: String): ChainBuilder = ???
def func3(config: config, prefix: String): ChainBuilder = ???
def func4(config: config, prefix: String): ChainBuilder = ???
def callRandomFunctions(config: config, prefix: String): Seq[ChainBuilder] =
Random.shuffle(Seq(func1 _, func2 _, func3 _, func4 _))
.map(_(config, prefix))
你可以通过函数的名字和反射来执行函数(虽然性能不是很好)。
您不需要模式匹配,它只是另一种实现方式:
您需要将所有函数放在特定的 class:
case class FunctionsClass(config: config, prefix: String) {
def func1(config: config, prefix: String): ChainBuilder = ???
def func2(config: config, prefix: String): ChainBuilder = ???
def func3(config: config, prefix: String): ChainBuilder = ???
def func4(config: config, prefix: String): ChainBuilder = ???
}
然后,像这样实现 callRandomFunctions
:
def callRandomFunctions(config: config, prefix: String):Unit = {
val args = List(config, prefix)
val argtypes = args.map(_.getClass)
val functionsClassObj = FunctionsClass(config, prefix)
val randomList = Random.shuffle(List(1, 2, 3, 4))
val result = randomList.map{ i =>
val mtd = functionsClassObj.getClass.getMethod(s"func$i", argtypes: _*)
Try {mtd.invoke(functionsClassObj, args: _*)}.recover { case _ => println("ERROR")}
}
result.filter(_.isSuccess).map(_.get))
}
这样你就可以根据随机列表中的 ID 调用你的 func
方法
更多gatling-y解决方案
def callRandomFunctions(config: config, prefix: String): ChainBuilder = {
val randomList = Random.shuffle(List(1, 2, 3, 4))
exec(session => session.set("randomList")
.forEach("${randomList}", "currentVal") {
doSwitch("${currentVal}") (
1 -> exec(func1(config: config, prefix: String)),
2 -> exec(func2(config: config, prefix: String)),
3 -> exec(func3(config: config, prefix: String)),
4 -> exec(func4(config: config, prefix: String))
)
}
如果执行顺序真的不需要是随机的,您也可以使用 .roundRobinSwitch
我有一个整数列表,需要遍历列表并调用具有相同输入参数的不同函数,这是我的代码:
def callRandomFunctions(config: config, prefix: String): ChainBuilder = {
val randomList = Random.shuffle(List(1, 2, 3, 4))
randomList.foreach { _ =>
_ match {
case 1 => func1(config, prefix)
case 2 => func2(config, prefix)
case 3 => func3(config, prefix)
case 4 => func4(config, prefix)
}
}
}
def func1(config: config, prefix: String): ChainBuilder = {...}
def func2(config: config, prefix: String): ChainBuilder = {...}
def func3(config: config, prefix: String): ChainBuilder = {...}
def func4(config: config, prefix: String): ChainBuilder = {...}
并得到这些错误:
missing parameter type for expanded function
[error] The argument types of an anonymous function must be fully known. (SLS 8.5)
[error] Expected type was: ?
[error] _ match {
[error] ^
[error] type mismatch;
[error] found : Unit
[error] required: io.gatling.core.structure.ChainBuilder
[error] randomList.foreach {
[error] ^
[error] two errors found
您可以简单地这样做:
randomList.foreach {
case 1 => func1(config, prefix)
case 2 => func2(config, prefix)
case 3 => func3(config, prefix)
case 4 => func4(config, prefix)
}
它将作为模式匹配传递给 foreach
回调的数字。
您的代码的另一个问题是您希望从 callRandomFunctions
return ChainBuilder
但您使用的 foreach
是终止运算符 return正在 Unit
。您可能想使用 map
并将 return 类型更改为 List[ChainBuilder]
:
def callRandomFunctions(config: Config, prefix: String): List[ChainBuilder] = {
val randomList = Random.shuffle(List(1, 2, 3, 4))
randomList.map {
case 1 => func1(config, prefix)
case 2 => func2(config, prefix)
case 3 => func3(config, prefix)
case 4 => func4(config, prefix)
}
}
简单的打乱函数,然后直接调用。
def func1(config: config, prefix: String): ChainBuilder = ???
def func2(config: config, prefix: String): ChainBuilder = ???
def func3(config: config, prefix: String): ChainBuilder = ???
def func4(config: config, prefix: String): ChainBuilder = ???
def callRandomFunctions(config: config, prefix: String): Seq[ChainBuilder] =
Random.shuffle(Seq(func1 _, func2 _, func3 _, func4 _))
.map(_(config, prefix))
你可以通过函数的名字和反射来执行函数(虽然性能不是很好)。 您不需要模式匹配,它只是另一种实现方式:
您需要将所有函数放在特定的 class:
case class FunctionsClass(config: config, prefix: String) {
def func1(config: config, prefix: String): ChainBuilder = ???
def func2(config: config, prefix: String): ChainBuilder = ???
def func3(config: config, prefix: String): ChainBuilder = ???
def func4(config: config, prefix: String): ChainBuilder = ???
}
然后,像这样实现 callRandomFunctions
:
def callRandomFunctions(config: config, prefix: String):Unit = {
val args = List(config, prefix)
val argtypes = args.map(_.getClass)
val functionsClassObj = FunctionsClass(config, prefix)
val randomList = Random.shuffle(List(1, 2, 3, 4))
val result = randomList.map{ i =>
val mtd = functionsClassObj.getClass.getMethod(s"func$i", argtypes: _*)
Try {mtd.invoke(functionsClassObj, args: _*)}.recover { case _ => println("ERROR")}
}
result.filter(_.isSuccess).map(_.get))
}
这样你就可以根据随机列表中的 ID 调用你的 func
方法
更多gatling-y解决方案
def callRandomFunctions(config: config, prefix: String): ChainBuilder = {
val randomList = Random.shuffle(List(1, 2, 3, 4))
exec(session => session.set("randomList")
.forEach("${randomList}", "currentVal") {
doSwitch("${currentVal}") (
1 -> exec(func1(config: config, prefix: String)),
2 -> exec(func2(config: config, prefix: String)),
3 -> exec(func3(config: config, prefix: String)),
4 -> exec(func4(config: config, prefix: String))
)
}
如果执行顺序真的不需要是随机的,您也可以使用 .roundRobinSwitch