我是Gatling和Scala的新手,我需要您的建议。 我想获得N位用户的负载测试。每个用户都必须发送创建不同帐户的请求。这是通过发送带有适当对象数组(在本例中为“ entries”)的json文件获得的。 每个后端用户都必须发送不同的登录名,因为我们的后端系统正在检查用户名是否唯一。我们必须以某种方式确保加特林正在为每个虚拟用户以及每个条目发送不同的数据。 我们注意到那里有代表虚拟用户状态的会话元素。问题在于,下面显示的代码将不起作用,因为与表达式函数一起使用的Exec结构不会发送任何请求。 有部分可以工作,但我不知道如何确定第三个参数来区分虚拟用户ID。请在下面找到用于此测试的简单json文件结构
{
"entries": [
{
"userName": "some user name",
"password": "some password"
}
}
和带有我的评论的scala代码
import io.gatling.core.Predef._
import io.gatling.http.Predef._
class UserCreationTest extends Simulation {
val profilesNumber = 2
val virtualUsers = 2
val httpConf = http
.baseURL("some url")
.acceptHeader("application/json")
.basicAuth("username", "password")
// This method will multiply 'entries' section in JSON 'entriesNumber' times
def createJsonUserEntries(entriesNumber: Int, users: List[String], userId : Long): String = {
val header = """{"entries": ["""
val footer = """]}"""
val builder = StringBuilder.newBuilder
for (i <- 0 until entriesNumber) {
val userIndex = (userId.toInt - 1) * entriesNumber + i
val userName = users(userIndex).get
val apiString =
s"""{
"userName": "${userName}"
"password": "password"
}"""
builder.append(apiString)
if (i != entriesNumber) {
builder.append(",")
}
}
header + builder.toString() + footer
}
// We do have method for generating user names based on profilesNumber and virtualUsers variables
// but for sake of this example lets hardcode 4 (profilesNumber * virtualUsers) user names
val usersList = List("user-1", "user-2", "user-3", "user-4")
//This will throw exception as no request was send. According to documentation function block is used to debugging/editing session
val scn = scenario("Create WiFi User Profile")
.exec(session => {
http("CreateUserProfile")
.post("/userProfiles/create/")
.body(StringBody(
createJsonUserEntries(profilesNumber, userslList, session.userId).toString
)
).asJSON
session})
// This exec block will send a request but I do not know how to determine third param that should be virtual user Id
// To run this section please comment previous whole scenario block
/*
val scn = scenario("")
.exec(http("CreateUserProfile")
.post("/userProfiles/create/")
.body(StringBody(
createJsonUserEntries(profilesNumber, emailList, ???).toString
)
).asJSON
)
*/
setUp(scn.inject(atOnceUsers(virtualUsers)).protocols(httpConf))
}
您能帮我吗?还有其他方法可以做到这一点吗?预先非常感谢
答案 0 :(得分:0)
所以您要让每个用户都有一个唯一的userId?
您可以创建一个可以执行此操作的供稿器
var userIdFeeder = (1 to 999999).toStream.map(i => Map("userId" -> i)).toIterator
val scn = scenario("")
.feed(userIdFeeder)
.exec(http("CreateUserProfile")
.post("/userProfiles/create/")
.body(StringBody(
createJsonUserEntries(profilesNumber, emailList, "${userId}").toString
)
).asJSON
)