Is it possible to persist WebTestContext across ASP.NET load test session?

时光总嘲笑我的痴心妄想 提交于 2019-12-24 01:49:16

问题


I want to load test an enterprise Web application (which I did not build), using a Visual Studio 2010 Ultimate Load Test. I want each virtual user to log in at the beginning, and log out at the end of their run of random tests. I can properly configured the load test to do so. However, there is a complication. The session key is injected into the URL, like this:

http://ProductName/(S(ilv3lp2yhbqdyr2tbcj5mout))/System/Container.aspx

I converted the Visual Studio WebTests to coded tests, and then retrofit them with code that uses the session-specific URL. This works fine. What I need to do is persist this session encoded URL across the various tests that specific virtual user runs, starting with the login WebTest class, to the logout WebTest class.

The individual WebTest classes are capable of logging in and out at the beginning and end of each test. However, this is not an accurate representation of normal use. This application emulates a mainframe terminal, and never cuts the connection or session between Web browser requests. Each session is one long, interactive HTTP request, just like a mainframe terminal interacts with, for example, an IBM AS400. Usert typically log in to the mainframe at the beginning of day, and (should) log out at the end of day. Likewise, this Web application maintains the HTTP request until the user logs out, or the IIS session timeout occurs. Therefore, it is important I keep the same session in the URL, between all tests, to ensure memory leaks and other nasty bugs don't accumulating.

Please share your thoughts!


回答1:


Problem 1: persist the session id across test iterations

You can store data in the 'user context' which is persistent across test iterations. It is found in the WebTestContext having the name '$LoadTestUserContext'. (But note that this context parameter only appears in load test runs, not in standalone web test runs)

// within WebTestPlugin.PreRequest() or MyExtractionRule.Extract()
// e is the corresponding eventargs object...
LoadTestUserContext userContext = (LoadTestUserContext)e.WebTest.Context["$LoadTestUserContext"];
...
// setting the value in the user context (i.e. in the extraction rule)
userContext["sessionId"] = "(extracted session id)";
...
// getting the value from the user context (i.e. in WebTestPlugin PreWebTest event)
e.WebTest.Context["sessionId"] = userContext["sessionId"];

You'll have to add a WebTestPlugin (that fetches the value from the user context into the web test context) to all of your web tests to make the value available across all tests.

Problem 2: Login/Logout only at start and end of load test

  • extract the login and logout functionality into their own separate tests (remember that the logout test also needs the WebTestPlugin that fetches the stored sessionId)
  • in the Load Test, the Edit Test Mix dialog lets you specify an Initialize and Terminate test: set these to the Login and Logout tests you just created
  • in the Load Test Scenario, set "Percentage of New Users" to 0.

Some additional explanation of the "Percentage of New Users" setting

The "Percentage of New Users" setting is poorly named and does not indicate its full behaviour.

  • When a "New User" starts a test iteration, it takes a new $WebTestUserId (and gets a new fresh user context, which you don't want)
  • When a non-"New User" starts a test iteration, it keeps the same old $WebTestUserId (and the old user context, which you do want)

So far so good. But the unexpected part is this:

  • Each "New User" executes the following during a load test:

    Initialize > web test iteration > Terminate

  • A non-"New User" executes the following for the entire duration of the load test:

    Initialize > iteration1 > iteration2 > ... > iterationN > Terminate

In other words, "New Users" are constantly logging in and out (which you don't want). Non-"New Users" only login and logout once in the entire load test, and continually run test iterations for the duration (which you do want).



来源:https://stackoverflow.com/questions/11125385/is-it-possible-to-persist-webtestcontext-across-asp-net-load-test-session

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!