在加特林情景之外进行Http调用

我的用例是进行一次http调用,从响应中的Location标头获取重定向URL,然后使用该URL进行负载测试。该URL是动态生成的,因此是初始的第一个http调用。请注意,测试第一个http调用不是我的测试的一部分。实现此目标的最佳方法是什么?有没有类似@BeforeMethod的东西在加特林吗?可以将加特林本身用于进行独立的http调用,还是需要使用基本的scala来实现?到目前为止,我有这个:

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)

我对加特林和标量的理解有限。因此,这是一个基本问题。

abliz13 回答:在加特林情景之外进行Http调用

您可以在Simulation的构造函数中进行所需的任何处理。

这将在启动场景之前由Gatling运行时运行。

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

(Scenario API确实提供了“ before”和“ after”钩子(请参见docs here,但是没有简单的方法可以将信息从这些钩子传递到方案配置中,如您所需要的那样。)

,

您可以在运行某些方案时将URL保存到标头或响应中的某些变量中。试试下面的代码,也许有帮助:

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)

请同时查看此帖子Gatling in scala how to get url from redirect

,

这取决于您要如何处理第一个HTTP调用。我看到两种可能性:

  1. 每个模拟一次获取重定向URL,然后在模拟中多次调用相同的重定向URL。在那种情况下,从加特林的角度来看,首次通话不会成为模拟的一部分。
  2. 在仿真中为每个用户独立获取重定向URL,然后调用一次(或N次)不同的重定向URL。在这种情况下,第一个呼叫将成为模拟的一部分,但是您可以使用group(name){...}对呼叫进行分组,这样您将获得每个分组的独立统计信息。

如果我正确理解了您的问题,那么您对第一个解决方案很感兴趣。在这种情况下,您将需要使用一些外部HTTP客户端并生成该重定向URL。当您可以放置​​任何逻辑时(尽管您无法在其中使用加特林DSL),加特林具有before{}块,但老实说,我只会在模拟类的构造函数中这样做(例如如果重定向URL作为Location标头返回,则可以使用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)))
}
本文链接:https://www.f2er.com/3139604.html

大家都在问