Simulating complex user behavior

Simulating user login

To load test certain pages on your website you may require to create a session for an user account on that site.

 -- Authorize user
 local response = http.request_batch({
     {"POST", "http://test.loadimpact.com/login.php", data="login=test_user&password=123"}
 })
 -- Get User ID and Session ID from cookies
 local uid, sid = response[1].cookies["uid"], response[1].cookies["sid"]
 if uid  "bad" or sid  "bad" then
     log.error("authorization failed")
     return -- Login failed, nothing to do
 end
 -- Get user's message list
 response = http.request_batch({
     {"GET", "http://test.loadimpact.com/my_messages.php"}
 })


Randomize user behaviour

If you do not want to write a complex scenario for your clients, you may be able to approximate it by picking URLs randomly.

 -- List URLs we want to randomly load
 local urls =
 {
     "http://test.loadimpact.com";
     "http://test.loadimpact.com/news.php";
     "http://test.loadimpact.com/contacts.php";
 }
 -- Get a random URL from the list - note that the Lua length operator is #
 local random_url = urls[math.random(#urls)]
 http.request_batch({
     {"GET", random_url}
 })


Weights

If you know that your average user visits some pages more often than another, you may use that knowledge to improve the load profile approximation. The simplest way to do this is to mention the same URL several times in the list:

 -- URLs we want to randomly load
 local INDEX = "http://test.loadimpact.com"
 local NEWS = "http://test.loadimpact.com/news.php"
 local CONTACTS = "http://test.loadimpact.com/contacts.php"
 -- Index page is seven times more popular than contacts.
 -- News page is three times more popular than contacts.
 local urls =
 {
     INDEX, INDEX, INDEX, INDEX, INDEX, INDEX, INDEX, -- x7
     NEWS, NEWS, NEWS, -- x3
     CONTACTS -- x1
 }
 -- Get a random URL from the list
 http.request_batch({
     {"GET", urls[math.random(#urls)]}
 })

 

See also:

 

Feedback and Knowledge Base