如何在VS负载测试之前设置JWT令牌 [英] How to set up the JWT token before the VS loadtest
问题描述
我正在尝试运行Visual Studio负载测试,但是在运行之前,我必须调用登录webAPI生成JWT令牌,然后在所有webAPI Web测试中使用此令牌.
I am trying to run a Visual Studio Load Test, but before it runs I have to call a login webAPI to generate a JWT token and then use this token in all of the webAPI web tests.
我只需要调用登录名一次即可生成令牌.在我所有其他的webAPI调用中都使用了相同的令牌值.在真正触发负载测试之前,如何将这一步骤放入负载测试中?
I only need to call the login to generate the token once. The same token value is used in all my other webAPI calls. How can I put this step into my load test before I really trigger the load test?
推荐答案
负载测试的测试组合"部分可以指定一个初始化"测试,该测试可以进行登录并收集令牌. $LoadTestUserContext
是负载测试的上下文参数(CP)之一.这旨在保留针对同一虚拟用户(VU)执行的测试之间的值.登录测试可以将令牌保存到$LoadTestUserContext
中,其他测试可以从中访问它.
The "Test mix" part of a load test can specify an "initialisation" test that can do the login and collect the token. One of the context parameters (CP) of a load test is the $LoadTestUserContext
. This is intended for holding values between tests executed for the same virtual user (VU). The login test can save the token into the $LoadTestUserContext
and the other tests can access it from there.
请注意,每个VU都有自己的$LoadTestUserContext
.如果该方案中的新用户百分比"很高,则将多次执行针对新VU的初始化测试(即登录测试).
Note that each VU has its own $LoadTestUserContext
. If the "percentage of new users" in the scenario is high then the initialisation test for a new VU (i.e. the login test) will be executed many times.
如果所有VU应该使用相同的令牌值,那么另一种可能性是通过将场景的迭代属性设置为1
来创建一次执行登录测试的场景.还要将恒定负载设置为1
.该测试可能具有PostRequest
或PostWebTest
插件,该插件将令牌值保存到某个类的static
变量中.然后,第二种情况将运行所有实际负载测试用例,其延迟开始时间"属性可能只有几秒钟,足以运行登录测试.这些测试的插件可以将static
值写入测试的CP中.
If all the VUs should use the same token value then another possibility is to create a scenario that executes the login test once, by setting the iterations property of that scenario to 1
; also set a constant load of 1
. This test could have a PostRequest
or a PostWebTest
plugin that saves the token value into a static
variable of some class. A second scenario then runs all of the real load test cases, its "delay start time" property could be a few seconds, long enough to run the login test. A plugin of these tests could write the static
value into a CP of the test.
这篇关于如何在VS负载测试之前设置JWT令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!