Behaviour table

To get even more flexibility, instead of using a table of URL strings, you may use a table of Lua functions, where each function would describe the behavior of one “kind” of your website’s clients.

You may code whole user stories this way:

 -- URLs we want to load
 local INDEX = "http://test.loadimpact.com"
 local NEWS = "http://test.loadimpact.com/news.php"
 local CONTACTS = "http://test.loadimpact.com/contacts.php"
 -- This client gets index and then closes the browser
 local get_index = function()
     http.request_batch({
         {"GET", INDEX}
     })
 end
 -- This one gets index and then goes to read the news
 local get_index_then_news = function()
     http.request_batch({
         {"GET", INDEX}
     })
     -- Sleep a bit
     client.sleep(math.random(1, 15))
     -- Go read news
     http.request_batch({
         {"GET", NEWS}
     })
 end
 -- This one refreshes the news page few times
 -- because he can't wait for the new product announcement,
 -- and then goes to contacts to find sales' phone number.
 local refresher = function()
   local number_of_refreshes = math.random(1, 10)
   -- Come on! I can't wait for the new iThing announcement!
   for i = 1, number_of_refreshes do
       -- Is it ready?!
       http.request_batch({
          {"GET", news}
       })
       -- Sleep a bit
       client.sleep(math.random(1, 5))
   end
   -- Sleep more
   client.sleep(math.random(5, 15))
   -- I should phone them!
   http.request_batch({
       {"GET", CONTACTS}
   })
 end
 -- List of our scenarios (scenario-functions)
 local scenarios =
 {
     get_index,
     get_index_then_news,
     refresher,
 }
 -- Pick random scenarios function...
 local scenario = scenarios[math.random(#scenarios)]
 -- ...and execute it
 scenario()

 

Feedback and Knowledge Base