I need help in converting a scala code to java

I’m new to Gatling and initially used Scala for the load test but now want to go ahead with Java as the compiler is much faster. And therefore I want to convert the below Scala code that I had written earlier to Java.

Scenarios in Loop


import io.gatling.http.Predef.*
import io.gatling.core.Predef.*
import io.gatling.core.structure.PopulationBuilder

import scala.collection.immutable.ArraySeq
import scala.collection.mutable
import scala.concurrent.duration.*
import scala.language.postfixOps

class personalDetails extends Simulation {

  // Protocol
  val httpProtocol = http.baseUrl("https://morpheus.uat.creditsaison.xyz")
    .contentTypeHeader("application/json")
    .header("requestingSub", "5317f163-448d-48f2-8f30-900cdd669288")
    .authorizationHeader("Basic QGRNMU46UEAkJFcwckQ=")

  var users = 100
  var i = 0

  // Scenario
  def scnList() = {
    var scnList = List.empty[PopulationBuilder]

    for (_ <- 0 to 2) yield {
      val injectionProfile = users match {
        case 100 => rampUsers(5).during(1)
        case 200 => rampUsers(10).during(1)
	case 300 => rampUsers(15).during(1)
      }

      val scn = scenario(s"Personal Details ${users} Users")
        .exec(
          http("Personal Details API")
            .post("/api/v1/appForm")
            .check(status.is(424))
            .body(RawFileBody("./src/test/resources/requestBody/personaldetails.json")).asJson
        )
        .inject(injectionProfile)
      
      scnList :+= scn
      users += 100
    }
    scnList
  }

  // Setup
  setUp(
    scnList(): _*
  ).protocols(httpProtocol)
}

You may want to try ChatGPT, I think it’s usable in your situation

I have already tried it but the code that it is generating is not working

Hi @sunil.karki,

I have already tried it but the code that it is generating is not working

What did you try and what is “not working”?
Is it specifically with gatling, or is it common code from scala and java (and a better question for one or the other forum)?

Cheers!

Hi @sunil.karki,

Sorry, but it doesn’t make sense in the topic.
Your working scala code doesn’t contain any nothingFor.

So, it isn’t the issue you mentioned.

If your issue is with chaining OpenInjectionSteps, this is definitely an another issue.

Do you know that injectOpen method can take either several OpenInjectionSteps (variadic) or a List<OpenInjectionStep>? Perhaps do you need the later as your injectionProfile variable.

Usually, we advise people to split the scenario definition (what a single user should do) from the injection profile.

Something like:

  // Scenario
  private static ScenarioBuilder scn = scenario("Personal Details")
      .exec(http("Personal Details API")
          .post("/api/v1/appForm")
          .check(status().is(503))
          .body(RawFileBody("./requestBody/personaldetails.json")).asJson());


  // Setup
  {
    setUp(scn.injectOpen(
        rampUsers(5).during(1),
        rampUsers(10).during(1),
        rampUsers(15).during(1)
    )).protocols(httpProtocol);
  }

Does that make sense in your use case?

Cheers!

@sbrevet Thanks for the guidance !!
I’ll try using the List of OpenInjectionStep

And we need scenarios to be executed separately and same should be listed in the report for analysis that’s why we are using loop for generating scenarios.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.