# Using "repeat" in an "advanced" (Simulation Modularization) scenario

**URL:** <https://community.gatling.io/t/using-repeat-in-an-advanced-simulation-modularization-scenario/367>\
**Category:** Gatling (Open-Source)\
**Created:** [May 7, 2013, 11:05pm UTC](https://community.gatling.io/t/using-repeat-in-an-advanced-simulation-modularization-scenario/367 "2013-05-07T23:05:45Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Price](https://avatars.discourse-cdn.com/v4/letter/c/a4c791/32.png) [@Chris\_Price](https://community.gatling.io/u/Chris_Price)\
**Post date:** [May 7, 2013, 11:05pm UTC](https://community.gatling.io/t/using-repeat-in-an-advanced-simulation-modularization-scenario/367/1 "2013-05-07T23:05:45Z")

</div>

Hi,

Hopefully I’m just missing something simple here.

I’m trying to build a re-usable gatling test runner for our environment. The idea is that I will save out several “Simulations” from the Gatling Recorder, and store them in a directory. I’ll then have one “master” simulation, which will read a config file containing a list of references to some of the other simulations that I’ve saved out, instantiate them and build up a list, and then call “setUp” with the dynamically assembled list of simulations.

So far this is mostly working. The one problem I’m having is that I’d like to use the “repeat” loop, and allow that to be dynamically configured as well. However, it seems like “repeat” is only available from within an individual Simulation class, meaning that I’d have to modify each of the generated Simulation code files to accomplish this.

Is there a better way?

Thanks!  
Chris

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [May 8, 2013, 5:00am UTC](https://community.gatling.io/t/using-repeat-in-an-advanced-simulation-modularization-scenario/367/2 "2013-05-08T05:00:31Z")

</div>

Easy: import bootstrap.\_

Cheers,

Stéphane

---

<div class="post-metadata">

**Author:** ![Chris\_Price](https://avatars.discourse-cdn.com/v4/letter/c/a4c791/32.png) [@Chris\_Price](https://community.gatling.io/u/Chris_Price)\
**Post date:** [May 8, 2013, 6:06pm UTC](https://community.gatling.io/t/using-repeat-in-an-advanced-simulation-modularization-scenario/367/3 "2013-05-08T18:06:37Z")

</div>

Thanks for the reply, and for tolerating my lack of familiarity with some of the intricacies of Scala’s type system and generics.

I still can’t quite solve what I’m trying to do, though. Here’s some example code:

class AdvancedSimulation extends Simulation {

…

val scn1 = new SomeOtherSimGeneratedViaRecorder()  
.scn  
//.repeat(10)  
.users(n.numInstances)  
.ramp(n.rampUpDuration)  
.protocolConfig(httpConf)

setUp(scn1)  
}

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [May 8, 2013, 6:21pm UTC](https://community.gatling.io/t/using-repeat-in-an-advanced-simulation-modularization-scenario/367/4 "2013-05-08T18:21:24Z")

</div>

> Thanks for the reply, and for tolerating my lack of familiarity with some  
> of the intricacies of Scala's type system and generics.
> 
> I still can't quite solve what I'm trying to do, though. Here's some  
> example code:
> 
> class AdvancedSimulation extends Simulation {
> 
> ...
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;val scn1 = new SomeOtherSimGeneratedViaRecorder()  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.scn  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//.repeat(10)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.users(n.numInstances)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ramp(n.rampUpDuration)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.protocolConfig(httpConf)
> 
> &nbsp;&nbsp;setUp(scn1)  
> }
> 
> In the above code I can insert a ".repeat(10)" immediately after ".scn",  
> but then the type of the object that is returned by that does not suport  
> calling ".users" on it. It looks like it's returning a function that  
> expects to be called with a ChainBuilder

You're not using repeat properly.  
repeat takes 2 parameter lists: the first one is the number of iterations,  
the second one the chain to repeat (this is called currying).

> --but I don't want to override the ChainBuilder that's already defined in  
> the Simulation class that the Recorder generated. It's kind of like I want  
> the Recorder to assign that automatically-generated ChainBuilder to its own  
> val that I can then access by name, rather than accessing the  
> ScenarioBuilder (scn).

Yep, exactly. I guess you're trying not to have to manually refactor what  
you get from the Recorder?

---

<div class="post-metadata">

**Author:** ![Chris\_Price](https://avatars.discourse-cdn.com/v4/letter/c/a4c791/32.png) [@Chris\_Price](https://community.gatling.io/u/Chris_Price)\
**Post date:** [May 8, 2013, 6:35pm UTC](https://community.gatling.io/t/using-repeat-in-an-advanced-simulation-modularization-scenario/367/5 "2013-05-08T18:35:47Z")

</div>

Right; my goal was to try to leave the Recorder-generated simulations completely untouched, and then modify them programatically from my AdvancedSimulation code. For things like “.users”, “.ramp”, this appears to work fine, but I’m not seeing an easy way to do the “repeat” part w/o editing the generated code.

For now I can work around it by editing the generated code. I was just trying to determine if there was some way to do it programatically that I was missing. Seems like maybe there isn’t a way to do that right now.

If I get some more time to hack on some pull requests in the future, I might try to figure out a way to allow that if it’s something you would consider merging. I have a few other ideas about things that would be useful in making those auto-generated Simulations easier to manipulate programatically as well 🙂

But for now I can definitely get by by just editing them by hand. Thanks for putting together such an awesome piece of software!

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [May 8, 2013, 7:35pm UTC](https://community.gatling.io/t/using-repeat-in-an-advanced-simulation-modularization-scenario/367/6 "2013-05-08T19:35:12Z")

</div>

My bad. I checked the code, and there is a way actually: you have to access the content of the scenario and chop off the last element (start) and build a new Chain.

I’ll make things easier in a next version.

class AdvancedSimulation extends Simulation {

…

import io.gatling.core.structure.ChainBuilder

val scn1 = scenario(“Repeated”)  
.repeat(10) {  
new ChainBuilder(new SomeOtherSimGeneratedViaRecorder().scn.actionBuilders.dropRight(1))

}  
.users(n.numInstances)  
.ramp(n.rampUpDuration)  
.protocolConfig(httpConf)

setUp(scn1)  
}

---

<div class="post-metadata">

**Author:** ![Chris\_Price](https://avatars.discourse-cdn.com/v4/letter/c/a4c791/32.png) [@Chris\_Price](https://community.gatling.io/u/Chris_Price)\
**Post date:** [May 8, 2013, 7:51pm UTC](https://community.gatling.io/t/using-repeat-in-an-advanced-simulation-modularization-scenario/367/7 "2013-05-08T19:51:02Z")

</div>

Nice! Thanks.

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [May 8, 2013, 8:00pm UTC](https://community.gatling.io/t/using-repeat-in-an-advanced-simulation-modularization-scenario/367/8 "2013-05-08T20:00:44Z")

</div>

Please let me know if that works properly so that I add a built in in the DSL for the next release.

---

<div class="post-metadata">

**Author:** ![Chris\_Price](https://avatars.discourse-cdn.com/v4/letter/c/a4c791/32.png) [@Chris\_Price](https://community.gatling.io/u/Chris_Price)\
**Post date:** [May 8, 2013, 9:22pm UTC](https://community.gatling.io/t/using-repeat-in-an-advanced-simulation-modularization-scenario/367/9 "2013-05-08T21:22:12Z")

</div>

Hmmm… it looks like the ChainBuilder constructor also requires a second parameter: “next: ActorRef”.

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [May 9, 2013, 4:13am UTC](https://community.gatling.io/t/using-repeat-in-an-advanced-simulation-modularization-scenario/367/10 "2013-05-09T04:13:22Z")

</div>

Arf, not in 2.0  
Just put a null there (I know, that’s ugly, that’s why it was refactored in 2.0 😉 )

---

<div class="post-metadata">

**Author:** ![Chris\_Price](https://avatars.discourse-cdn.com/v4/letter/c/a4c791/32.png) [@Chris\_Price](https://community.gatling.io/u/Chris_Price)\
**Post date:** [May 9, 2013, 7:31pm UTC](https://community.gatling.io/t/using-repeat-in-an-advanced-simulation-modularization-scenario/367/11 "2013-05-09T19:31:59Z")

</div>

Tried this today, and with the null in there it compiles OK. But when I run it, it doesn’t actually submit any requests… so at the end, it dies with:

java.lang.UnsupportedOperationException: There were no requests sent during the simulation, reports won’t be generated

I tried removing the “dropRight(1)”, thinking maybe that last action in the chain is what makes it go… but got the same results.

I’ll just stick with the hand-edits of the recorder Sims for now, but would definitely take advantage of a more dynamic way to do this in the future.

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [May 9, 2013, 9:53pm UTC](https://community.gatling.io/t/using-repeat-in-an-advanced-simulation-modularization-scenario/367/12 "2013-05-09T21:53:00Z")

</div>

Weird: I just tested on both 1.5.1-SNAPSHOT and 2.0.0-SNAPSHOT and it works fine.

[https://github.com/excilys/gatling/issues/1145](https://github.com/excilys/gatling/issues/1145)

---

<div class="post-metadata">

**Author:** ![Chris\_Price](https://avatars.discourse-cdn.com/v4/letter/c/a4c791/32.png) [@Chris\_Price](https://community.gatling.io/u/Chris_Price)\
**Post date:** [May 9, 2013, 9:59pm UTC](https://community.gatling.io/t/using-repeat-in-an-advanced-simulation-modularization-scenario/367/13 "2013-05-09T21:59:35Z")

</div>

Hmmm… I was trying it against 1.5.0, not sure if that makes a difference.

So with your new commit, the syntax would be this, correct?:

val scn1 = scenario(“Repeated”)  
.repeat(10) {  
new SomeOtherSimGeneratedViaRecorder().scn  
}  
.users(n.numInstances)  
.ramp(n.rampUpDuration)  
.protocolConfig(httpConf)

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [May 9, 2013, 10:03pm UTC](https://community.gatling.io/t/using-repeat-in-an-advanced-simulation-modularization-scenario/367/14 "2013-05-09T22:03:28Z")

</div>

Not exactly, you need to wrap with exec.

Here’s my test:

class Hello extends Simulation {

val scn = scenario(“Hello”).exec(http(“Home”).get("/"))

val httpConf = httpConfig  
.baseURL(“[http://localhost:9000](http://localhost:9000)”)  
.acceptHeader(“text/html,application/xhtml+xml,application/xml;q=0.9,_/_;q=0.8”)  
.doNotTrackHeader(“1”)  
.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”)  
.shareConnections

setUp(scn.users(1).protocolConfig(httpConf))  
}

class World extends Simulation {

val hello = new Hello()

val scn = scenario(“World”).repeat(5) {  
exec(hello.scn)  
}

setUp(scn.users(1).protocolConfig(hello.httpConf))  
}

---

<div class="post-metadata">

**Author:** ![Chris\_Price](https://avatars.discourse-cdn.com/v4/letter/c/a4c791/32.png) [@Chris\_Price](https://community.gatling.io/u/Chris_Price)\
**Post date:** [May 9, 2013, 10:08pm UTC](https://community.gatling.io/t/using-repeat-in-an-advanced-simulation-modularization-scenario/367/15 "2013-05-09T22:08:36Z")

</div>

Interesting. Seems like that exec would be necessary even if I was doing the “new ChainBuilder” approach then, right? I didn’t have an exec there before so maybe that’s why it didn’t work for me.

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [May 9, 2013, 10:16pm UTC](https://community.gatling.io/t/using-repeat-in-an-advanced-simulation-modularization-scenario/367/16 "2013-05-09T22:16:40Z")

</div>

Nope, just tested with `new ChainBuilder(hello.scn.actionBuilders.dropRight(1), null)` and it worked fine for me.

---

<div class="post-metadata">

**Author:** ![Chris\_Price](https://avatars.discourse-cdn.com/v4/letter/c/a4c791/32.png) [@Chris\_Price](https://community.gatling.io/u/Chris_Price)\
**Post date:** [May 9, 2013, 10:20pm UTC](https://community.gatling.io/t/using-repeat-in-an-advanced-simulation-modularization-scenario/367/17 "2013-05-09T22:20:02Z")

</div>

Well, I’m stumped then. Maybe I’ll try again from scratch tomorrow. Thanks for the help!

---

<div class="post-metadata">

**Author:** ![Chris\_Price](https://avatars.discourse-cdn.com/v4/letter/c/a4c791/32.png) [@Chris\_Price](https://community.gatling.io/u/Chris_Price)\
**Post date:** [May 28, 2013, 9:29pm UTC](https://community.gatling.io/t/using-repeat-in-an-advanced-simulation-modularization-scenario/367/18 "2013-05-28T21:29:19Z")

</div>

At long last, I got a chance to try this again… and it works! Not sure what I’d done wrong before. Thanks for the help, _Stéphane._

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [May 28, 2013, 9:33pm UTC](https://community.gatling.io/t/using-repeat-in-an-advanced-simulation-modularization-scenario/367/19 "2013-05-28T21:33:00Z")

</div>

Great! And you’re welcome 🙂
