【发布时间】:2019-04-04 17:24:25
【问题描述】:
我需要通过 Gatling(我完全是新手!)对需要 OAuth2.0 令牌的 API 进行负载测试,但希望每个虚拟用户都使用相同的令牌。我正在检索令牌(我认为)并将其放入一个名为“访问”的变量中,但是当测试本身开始时,我不断收到“没有定义名为“访问”的属性”。
我的令牌检索如下所示(连同 httpConf,在下面使用):
class MySimulation extends Simulation {
val httpConf = http
.baseUrl("https://MyBaseUrl.Com/")
.acceptHeader("application/json")
.doNotTrackHeader("1")
.acceptLanguageHeader("en-UK,en;q=0.5")
.acceptEncodingHeader("gzip, deflate")
.userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0")
.shareConnections
val header = Map("Content-Type" -> """application/x-www-form-urlencoded""")
al auth = scenario("Retrieve Token")
.exec(http("POST OAuth Req")
.post("https://SomeTokenUrl")
.formParam("resource", "someresource")
.formParam("grant_type", "somegranttype")
.formParam("client_secret", "someclientsecret")
.formParam("client_id", "someclientid")
.headers(header).check(status.is(200)).check(jsonPath("$.access_token").find.saveAs("access")))
然后我尝试将负载测试设置为(注意:我最初确实放置了“映射”,而不是可变变体,但在某处读取默认值是不可变的,并想知道这是否是标头无法更新的原因) :
val headers_10 = scala.collection.mutable.Map("Content-Type" -> "application/json; charset=ISO-8859-1", "Authorization" -> "Bearer ${access}")
val scn = scenario("MyService Gatling test run")
.exec(http("")
.post("Myservice/api")
.headers(headers_10.toMap)
.body(StringBody("""{"SomeProperty": "Some Value"}"""))
.asJson
.check(status.is(200)))
setUp(
auth.inject(constantUsersPerSec(1) during (2 seconds)),
scn.inject(nothingFor(2 seconds),
constantUsersPerSec(10) during (10 seconds)
).protocols(httpConf))
.assertions(global.responseTime.max.lt(500))
.assertions(forAll.failedRequests.percent.lte(1))
.assertions(global.responseTime.mean.lte(100))
这个想法是令牌检索将在负载测试开始之前完成,然后负载测试场景将使用“访问”变量,但它给出了以下结果:
ERROR : Failed to build request: No attribute named 'access' is defined
我已经走到了尽头。我猜这可能与范围有关,也许变量不会延续到负载测试场景,但我在其他地方看到过似乎完全推荐该设置的示例,所以我不知道这些其他示例是部分完整的还是什么。
【问题讨论】:
标签: scala load-testing gatling jsonpath