6

I have two scenarios 1st "getAssets" scenario will fetch all asset IDs and save it in a list, 2nd scenario "fetchMetadata" will iterate those IDs.

I have to execute "getAssets" scenario only once to fetch all the IDs, and "fetchMetadata" scenario till given time duration only after completion of 1st scenario.

How can I execute both scenarios in Chain (Sequentially)?

Here is the code but it doesn't execute scenarios in sequence.

import java.util.concurrent.ThreadLocalRandom
import scala.concurrent.duration._
import io.gatling.core.Predef._
import io.gatling.http.Predef._

class getAssetsMetadata extends Simulation {
    val getAssetURL = System.getProperty("getAssetURL", "https://performancetesting.net")
    val username = System.getProperty("username", "performanceuser")
    val password = System.getProperty("password", "performanceuser")
    val limit = Integer.getInteger("limit", 1000).toInt
    val userCount = Integer.getInteger("userCount", 100).toInt
    val duration = Integer.getInteger("duration",1).toInt //in minutes

    var IdList: Seq[String] = _   

    val httpProtocol = http
        .basicAuth(username, password)
        .baseURL(getAssetURL)
        .contentTypeHeader("""application/vnd.v1+json""")

    def getAssets = exec(http("List of Assets")
            .get(s"""/api/assets;limit=$limit""")
            .check(jsonPath("$.assets[*].id").findAll.transform {v => IdList = v; v }.saveAs("IdList"))
            )

    def fetchMetadata = exec(_.set("IdList", IdList))
        .exec(http("Metadata Request")
            .get("""/api/assets/${IdList.random()}/metadata""")
            )

    // Chain scenarios      
    var scn1 = scenario("Scenario 1")
            .exec(getAssets)
    var scn2 = scenario("Scenario 2")       
            .exec(fetchMetadata)
    setUp(scn1.inject(atOnceUsers(1)), scn2.inject(constantUsersPerSec(userCount) during(duration minutes))).protocols(httpProtocol)
    }
Peter
  • 855
  • 2
  • 15
  • 35
  • I've edited my answer with 'def' but it didnt work. it executes both requests concurrently i think. As request 1st getAssets takes around 55 seconds to get response data mean while 2nd request starts executing. Any help how do I execute them in sequence or in chain? Thanks. – Peter May 19 '17 at 14:34
  • I've tried but its not running in sequence. could you please check above code? I've modified it. – Peter Jun 05 '17 at 09:59
  • Try: `val chainedScenario = scenario("chainedScenario").exec(getAssets).exec(fetchMetadata)` – Jeffrey Chung Jun 05 '17 at 12:26
  • 2
    But I want to execute 'getAssets' scenario only ONCE and 'fetchMetadata' multiple times. is it possible in your code? – Peter Jun 06 '17 at 05:07
  • @Peter I have the same condition one test should run only once and others multiple times, Did you manage to resolve this? – Sandeep Purohit Apr 23 '19 at 09:18
  • @SandeepPurohit I managed to do it in hacky way using private val initStarted = new AtomicBoolean(false) private val initCompleteLatch = new CountDownLatch(1) If you need complete example, feel free to create new question (this one is closed). – lukyer Jul 26 '19 at 10:19

0 Answers0