gatling
421a28afa5ae
Loop as long as with duration limit
michalwiacek
8 days ago
Motivation: It would be good to add another type of loop statement where we iterate over the loop as long as condition is satisfied, but no longer than specified amount of time Modifications: Add two additional loop statement: asLongAsDuring, doWhileDuring Result: option to loop asLongAs condition is satisfied, but no longer than specified amount of time

25 26 27 28 29 30 31 32 33 34 25 26 27 28 29 30 31 32 33 34 35 36
case object ForeachLoopType extends LoopType("foreach", false, false) case object DuringLoopType extends LoopType("during", true, false) case object ForeverLoopType extends LoopType("forever", false, false) case object AsLongAsLoopType extends LoopType("asLongAs", false, false) case object DoWhileType extends LoopType("doWhile", false, true)
case object AsLongAsDuringLoopType extends LoopType("asLongAsDuring", true, false) case object DoWhileDuringType extends LoopType("doWhileDuring", true, true)
/** * @constructor create a new Loop * @param condition the function that determine the condition * @param loopNext chain that will be executed if condition evaluates to true

62 63 64 65 66 67 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
def doWhile(condition: Expression[Boolean], counterName: String = UUID.randomUUID.toString)(chain: ChainBuilder): B = loop(condition, chain, counterName, exitASAP = false, DoWhileType) private def loop(condition: Expression[Boolean], chain: ChainBuilder, counterName: String = UUID.randomUUID.toString, exitASAP: Boolean, loopType: LoopType): B = exec(new LoopBuilder(condition, chain, counterName, exitASAP, loopType))
private def continueCondition(condition: Expression[Boolean], duration: Expression[Duration], counterName: String) = (session: Session) => for { durationValue <- duration(session) conditionValue <- condition(session) } yield nowMillis - session.loopTimestampValue(counterName) <= durationValue.toMillis && conditionValue def asLongAsDuring( condition: Expression[Boolean], duration: Expression[Duration], counterName: String = UUID.randomUUID.toString, exitASAP: Boolean = true )(chain: ChainBuilder): B = loop(continueCondition(condition, duration, counterName), chain, counterName, exitASAP, AsLongAsDuringLoopType) def doWhileDuring( condition: Expression[Boolean], duration: Expression[Duration], counterName: String = UUID.randomUUID.toString, exitASAP: Boolean = true )(chain: ChainBuilder): B = loop(continueCondition(condition, duration, counterName), chain, counterName, exitASAP, DoWhileDuringType)
}

85 86 87 88 89 90 91 92 93 94 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
exec(noop) } .doWhile("${condition}", "counterName") { exec(noop) }
.asLongAsDuring("${condition}", 10 seconds) { exec(noop) } .asLongAsDuring("${condition}", 10 seconds, "counterName") { exec(noop) } .doWhileDuring("${condition}", 10 seconds) { exec(noop) } .doWhileDuring("${condition}", 10 seconds, "counterName") { exec(noop) }
.repeat(iterations, "counter") { // What will be repeated ? // First request to be repeated exec { session => println("iterate: " + session("counter"))

117 118 119 120 121 122 123 124 125 126 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
doWhile(condition, counterName) { myChain } //#doWhile
//#asLongAsDuring asLongAsDuring(condition, duration, counterName) { myChain } //#asLongAsDuring //#doWhileDuring doWhileDuring(condition, duration, counterName) { myChain } //#doWhileDuring
//#forever forever(counterName) { myChain } //#forever
About FluentSend Feedback