2017-06-06 143 views
0

我想在for循環中動態創建並設置gatling場景以加載測試web服務。動態創建和設置場景

所以我嘗試了以下(縮短):

class RecordedSimulation extends Simulation { 

    val httpProtocol = http 
    .baseURL("http://127.0.0.1") 

    val overallUsers = 1000 

    val methods: Map[String, Double] = Map(
    "FindContact" -> 0.6, 
    "FindAddress" -> 0.3, 
    "FindNumber" -> 0.1 
) 

    for ((methodname, probability) <- methods) { 
    val scen = scenario(methodname) 
     .exec(http(methodname) 
     .get("/contactservice") 
     .queryParam("method", methodname)) 

    setUp(scen.inject(constantUsersPerSec(overallUsers * probability) during (60 seconds))).protocols(httpProtocol) 
    } 
} 

,如果我嘗試運行此模擬沒有任何反應:沒有啓動模擬,也沒有出現錯誤。

所以我的問題是,如果它甚至可以動態創建和設置gatling場景。我錯過了什麼或做錯了什麼?


爲什麼我要動態地動手呢?

那麼我有很多非常相似的方法來測試,我想避免一遍又一遍複製/粘貼相同的場景。

回答

0

我自己發現了問題。 This gist讓我走向正確的方向。

看來您只能撥打setUp方法一次。所以我把我的場景放在一個ArraySeq中,並以此ArraySeq作爲參數調用setUp

import scala.collection.mutable.ArraySeq 
import io.gatling.core.structure.PopulationBuilder 

class RecordedSimulation extends Simulation { 

    val httpProtocol = http 
    .baseURL("http://127.0.0.1") 

    val overallUsers = 1000 

    val methods: Map[String, Double] = Map(
    "FindContact" -> 0.6, 
    "FindAddress" -> 0.3, 
    "FindNumber" -> 0.1 
) 

    def scnList() = { 
    var scnList = new ArraySeq[PopulationBuilder](methods.size) 
    var i = 0 
    for ((methodname, probability) <- methods) { 
     var scen = scenario(methodname) 
     .exec(http(methodname) 
      .get("/contactservice") 
      .queryParam("method", methodname)) 
     .inject(constantUsersPerSec(overallUsers * probability) during (60 seconds) randomized) 

     scnList(i) = scen 
     i = i + 1 
    } 
    scnList 
    } 

    setUp(scnList: _*).protocols(httpProtocol) 
}