设置Gatling以根据百分比/比率发送请求?

时间:2017-05-04 11:17:52

标签: java performance scala testing gatling

我想设置Gatling,以便在一次设置中,我可以发送3000个请求,其中95%将使用一个测试文件,5%是另一个测试文件。这些文件作为json文件检索(在下面的代码中称为'userFeeder'.Gatling能否支持上述场景?

目前的代码如下,适用于每秒请求的方法,但需要修改。

class AddUserSimulation extends Simulation {

  private val conf = ConfigFactory.load() //loads a setup file of parameters
  private val TOKEN_VALUE = "tokenvalue"
  private val userFeeder =  jsonFile("/" + conf.getString("environment") + "/testaddUser.json")

  val httpConf = http
    .baseURL(conf.getString("api.gateway.url")) // Here is the root for all relative URLs
    .header("Referer", conf.getString("referer"))
    .header("Cache-Control", "no-cache")
    .contentTypeHeader("application/json")

  val Login = new ADFSAuthentication Login

  val scnAPI = scenario("test add User") // A scenario is a chain of requests and pauses
    .feed(userFeeder.circular)
    .exec(Login.process)
    .repeat(conf.getInt("repeat.count")) {
        exec(http("test add User")
        .post("/" + conf.getString("environment") + "/users/")
        .body(StringBody("${payload}")).asJSON
        .header("Authorization", "Bearer ${"+TOKEN_VALUE+"}")
    .check(status.is(200)))
    .pause(conf.getInt("execution.pause"))
    }
  setUp(scnAPI.inject(constantUsersPerSec(11) during(30 minutes)).protocols(httpConf)) 
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

当然!首先设置你的两个馈线。然后设置两个场景,一个使用第一个进纸器,一个使用第二个进纸器。最后setUp两者都包含您希望的用户数(与您的分配相对应)。

代码看起来像这样:

private val firstFeeder =  jsonFile("/" + conf.getString("environment") + "/testaddUser.json")
private val secondFeeder =  jsonFile("/" + conf.getString("environment") + "/testaddUser2.json")

val scnAPI = scenario("test add User")
    .feed(firstFeeder)
    //...

val scnAPI2 = scenario("second test add User")
    .feed(secondFeeder)
    //...

setUp(scnAPI.inject(constantUsersPerSec(95) during(30 minutes)).protocols(httpConf), 
      scnAPI2.inject(constantUsersPerSec(5) during(30 minutes)).protocols(httpConf)) 

注意:这不会创建3000个请求但我认为你明白了。