Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Http call outside of a scenario in gatling

My use case is to make an http call, get the redirect url from the Location header in the response, and use this url to perform my load testing. This url is dynamically generated and hence the initial first http call. Note that testing the first http call is not part of my test. What is the best way to achieve this ? Is there something like a @BeforeMethod equivalent here in gatling ? Can gatling itself be used to make the standalone http call or do I need to use basic scala to achieve this ? So far I have this :

val httpConfig = http
  .inferHtmlResources()
  .acceptHeader("*/*")
  .acceptEncodingHeader("gzip, deflate")
  .acceptLanguageHeader("en-US,en;q=0.5")
  .header("Authorization", "Negotiate " + token)
  .doNotTrackHeader("1")
  .userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:51.0) Gecko/20100101 Firefox/51.0")

val scn = scenario("My Tests").exec(http("Health check")
  .get("https://example-server.com")
  .check(status.is(200)))

setUp(
  scn.inject(atOnceUsers(10))
).protocols(httpConfig)

My understanding of gatling and scala is limited. Hence this basic question.

like image 383
Gautam Avatar asked Jan 25 '23 15:01

Gautam


1 Answers

You can do any processing you need in the constructor of your Simulation.

This will be run by the Gatling runtime just before it starts the scenario.

i.e.

class MyTestWithDynamicTarget extends Simulation {

  val targetUrl = loadTarget()

  val scn = scenario("My Tests")
    .exec(http("Health check")
      .get(targetUrl)
      .check(status.is(200)))

  setUp(
    scn.inject(atOnceUsers(10))
  ).protocols(httpConfig)

  /**
   * Fetch the current location of the service under test, which is returned
   * in the "Location" header of an HTTP request
   */
  def loadTarget() = {
    ??? // see e.g. https://stackoverflow.com/questions/2659000/java-how-to-find-the-redirected-url-of-a-url
  }
}

(The Scenario API does offer "before" and "after" hooks (see docs here) but there is no easy way to pass info from those hooks into the scenario configuration, as you need to do here.)

like image 148
Rich Avatar answered Jan 30 '23 04:01

Rich