我正在尝试创建一个性能测试套件,在这里我将随机选择ID数组。对于每个ID,都分配了一个指定的auth_token。
在协议中,如果我传递随机ID将使用的方法,它总是将其设置为整个操作的特定ID。
我期待的是,我定义了10个虚拟用户,对于每个用户,协议应该更改ID并继续执行场景。
目前,Gatling正在第一次设置协议,并对所有10个用户使用相同的协议。
id = random.generate //generate random id
authHeader = Method(id);
def method (id:String) : String{
if(id=="id1")
return token1
else if(id=="id2")
return token2
""
}
val httpProtocol = http.baseUrl(baseURI)
.acceptHeader(header)
.authorizationHeader(authHeader)
.contentTypeHeader(contentType)
.userAgentHeader(agentHeader)
val scn1: ScenarioBuilder = scenario("name")
.exec(http("scenario1")
.post(device_context)
.body(payload)
.check(status.is(202)))
setUp(scn1.inject(atOnceUsers(2)).protocols(httpProtocol))```
In the above code i need the suite to run for 2 different id.发布于 2019-11-08 00:56:32
要做到这一点,您必须使用会话变量来保存您的authHeader。DSL方法定义了只执行一次的构建器--这解释了您所看到的内容。
最好的方法是构建一个馈线来保存您的id和auth令牌(我假设您没有在其他地方使用id)。
因此,定义一个将键(只是字符串)映射到包含id和auth令牌的映射的馈线。
//a list of strings to hold the auth tokens
private val ids = List(Map("id" -> "id1", "auth" -> "token1"), Map("id" -> "id2", "auth" -> "token2"), ...)
//the feeder to put a random token into the session
private val idFeeder = Iterator.continually(Map("id" -> Random.shuffle(ids).head))现在,当您在.feed上调用idFeeder时,您会得到一个随机映射,它在会话变量"id“中有"id”和"auth“的键。
因此,更新您的场景以使用馈线并设置授权头(并从协议定义中删除.authorizationHeader )
val scn1: ScenarioBuilder = scenario("name")
.feed(idFeeder)
.exec(http("scenario1")
.header("Authorization", "${id.auth})"
.post(device_context)
.body(payload)
.check(status.is(202)))在您的体内,您可以使用"${id.id}“访问用户id字符串。
或者,您可以更新您的协议定义,使其具有${id}引用,但我发现在调用.feed的同一块中使用馈线值更好。
https://stackoverflow.com/questions/58744683
复制相似问题