2015-11-17 76 views
2

我使用下面的腳本,它應該發送一個包含從文件中讀取的一些數據的post請求。我想隨機化這些請求,所以有時會發送「R1_BINFile_0」,有時候會發送「R2_Binfile_1」。出於某種原因,這是行不通的,它總是發出同樣的請求

Gatling scala腳本隨機場景

package computerdatabase 

import io.gatling.core.Predef._ 
import io.gatling.http.Predef._ 
import scala.concurrent.duration._ 

class MYSimulation extends Simulation { 

    val feeder = csv("CsvConfigFile.csv").circular 
    val PATH = sys.env("GATLING_HOME") 

    val httpConf = http 
    .baseURL("https://HOST") // Here is the root for all relative URLs 
    .acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") 
    .acceptCharsetHeader("UTF-8") 
    .acceptLanguageHeader("en-US,en;q=0.5") 
    .acceptEncodingHeader("gzip, deflate") 
    .userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0") 

    val headers_10 = Map("Content-Type" -> "application/octet-stream") 
    val rnd = new scala.util.Random 
    val scn = scenario("Scenario1") 
     .feed(feeder) 
     .doIfOrElse(rnd.nextInt(100) > 50) { 
     exec(http("R1") 
       .post("https://HOST/web/") 
       .headers(headers_10) 
       .body(RawFileBody(PATH + "\\user-files\\data\\" + "${R1_BinFile}" + "_0"))) 
     } { 
     .exec(http("R1") 
       .post("https://HOST/web/") 
       .headers(headers_10) 
       .body(RawFileBody(PATH + "\\user-files\\data\\" + "${R1_BinFile}" + "_1"))) 
     } 


    setUp(scn.inject(constantUsersPerSec(5) during (100 seconds)).protocols(httpConf)) 
} 

回答

3

的問題是,該參數doIfOrElse只執行一次,所以成爲一個常數,同一個分支選擇各一次。嘗試傳球而不是一個功能,這會爲每個請求執行:

.doIfOrElse(_ => rnd.nextInt(100) > 50) 

你也可以使用randomSwitch代替doIfOrElse,這給了你的目標是什麼了更清晰的語法。