简体   繁体   中英

Scala random number in a string

val scn = scenario("newUser")
    .exec(http("request_0")
        .get("/wordpress/?page_id=83")
        .headers(headers_0))
    .pause(1)
    .exec(http("request_1")
        .post("/wordpress/?page_id=83")
        .headers(headers_1)
        .body(RawFileBody("new_user_request_0001.txt")))

setUp(scn.inject(atOnceUsers(100))).protocols(httpProtocol)

My question is the following... I have to load 100 text files with structure like that in the example ("new_user_request_0001.txt"), using numbers between 0 to 100 randomly. How can I do? Thank you all

It's easy. You can use Random.shuffle

scala.util.Random.shuffle(0 to 100)

You could try to use uniformRandomSwitch , generate a sequence of 100 ChainBuilders , and then use the (...): _* syntax to unpack it as an argument to a vararg-method:

val scn = scenario("newUser")
    .exec(http("request_0")
        .get("/wordpress/?page_id=83")
        .headers(headers_0))
    .pause(1)
    .uniformRandomSwitch(
        ((0 until 100).map{ idx =>
          http("request_1")
          .post("/wordpress/?page_id=83")
          .headers(headers_1)
          .body(RawFileBody(s"new_user_request_0${idx}.txt")))
        }): _*
    )

The s" ... ${idx} ..." syntax injects the idx argument passed by map into the string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM