4

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.

Gautam
  • 1,862
  • 9
  • 16

3 Answers3

5

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.)

Rich
  • 15,048
  • 2
  • 66
  • 119
2

It depends on how do you want to handle this first HTTP call. I see two possibilities:

  1. Get redirect URL once per simulation and then make multiple calls to the same redirect URL in simulation. In that case first call won't be part of simulation from Gatling's perspective.
  2. Get redirect URL independently for each user in simulation and then call different redirect URL once (or N-times). In this case the first call will be part of simulation but you can group calls with group(name){...} so you will get separate statistics for each group.

If I'm understanding your question properly you are interested in the first solution. In that case you will need to use some external HTTP client and generate that redirect URL. Gatling has before{} block when you can put any logic (although you cant use Gatling DSL there) but honestly I would just do that in the constructor of simulation class e.g. if redirect url is returned as Location header you could get it with Apache HTTP Client:

import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.HttpClients

class RedirectSimulation extends Simulation {

  val redirectUrl = HttpClients.createDefault
    .execute(new HttpGet("http://redirectgenerator/getRedirect"))
    .getLastHeader("Location")
    .getValue

 val scn = scenario("Test redirect url")
   .exec(
     http("Get response").get(redirectUrl)
   )

 setUp(scn.inject(atOnceUsers(10)))
}
Mateusz Gruszczynski
  • 1,421
  • 10
  • 18
1

You can save the URL to some variable from the header or from a response while running some scenario. Try the below code maybe it helps:

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)))

  val scnForGetLocation = scenario("GetLocationHeader").exec(http("Location")
  .get("https://example-server.com")
  .check(status.is(200))  
  .check(header("Location").saveAs("url")))

  val testOne = scenario("testOne").exec(http("testOne") //Your actual test
  .get("${url}")
  .check(status.is(200)))

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

Please check this post as well Gatling in scala how to get url from redirect

Vaibhav_Sharma
  • 546
  • 1
  • 9
  • 22