Gatling的进阶二

1. 参数化

 
 
Gatling可以很方便使用csv文件进行参数化,例如一个用户信息表:
 
/* user_information.csv */
username,password,account_id
user1,password1,4
user2,password2,7
...
user10,password10,34
 
那么在gatling中就这样调用参数文件:
 
/* Scenario */
.feed(csv("user_information.csv")) 
/×获取csv文件,该文件在固定的data文件夹下×/
 
.exec(
  http("request_3")
    .post("/login")
    .param("username", "${username}") //参数化的用户名
    .param("password", "${password}") //参数化的密码
    .headers(headers_3)
    .check(status.is(302))
)
...
.exec(
  http("request_5")
 .get("/private/bank/account/ACC${account_id}/operations.html")//参数化的用户id
 .headers(headers_5)
)
 
2. 重复执行
 
很多性能测试需要不断重复的执行同一个场景,那么loop就很必要。Gatling提供两种循环的方法:
 
第一种:repeat指定的次数
repeat(10) { // 重复执行10次
    exec( http(...) ... )
    .pause(...)
}
 
第二种:在指定的时间内重复执行直到时间结束
.during(20 seconds) { // 在20秒内不断的执行
    exec( http(...) ... )
    .pause(...)
}