HOW TO randomize website test using JMeter?

Apache JMeter LogoDo you want to check if all the links on your website work correctly? Moreover, do you want to check how your website behaves when users click on various URLs and open pages in a different order? This is possible with Apache JMeter, but it isn’t easy to use for the first time. I had a lot of problems creating an appropriate test plan, but finally I made it. In this post I will share the results of my work with you.

A few words about Apache JMeter

Apache JMeter is an open source Java desktop application designed as a load testing tool for analyzing and measuring the performance of a variety of services, with a focus on web applications. JMeter can test performance both on static and dynamic resources like:

  • HTTP/HTTPS, FTP, mail(POP3 and IMAP) servers,
  • web services(SOAP),
  • databases via JDBC,
  • LDAP,
  • JMS,
  • and files, servlets, Perl scripts, Java objects, and ActiveMQ queries.

JMeter has more test features: variable parametrization, assertions (response validation), per thread cookies, configuration variables and a variety of reports.

My test scenario

I would like to check if all pages on my website work properly and see how long it takes a page to load. I want to simulate a situation when each user has it own random path through pages. Moreover, I want to set the depth of the path. On each page a user must choose arbitrarily one link from the set of all links. My main problem This issue seems to be easy, but I had problems making this test random. When I find all links on the page I want to choose randomly one of them. JMeter has such element as Random Variable and function __Random() to generate a random number. To do this properly I must set minimum and maximum values. In my case min value is fixed – 0. But max value I have to set dynamically for each page and this value equals the number of links. I tried to set a max value as variable in JMeter convention: ${number_of_links}. But this solution doesn’t work in Random Variable as well as in function __Random(). I didn’t know how to solve this problem. But I found some hints on this topic. I have solved my problem and now I will show you how I did it.

Make a test plan step by step

Below I will describe how you can make test plan in 8 steps. But first you must download and run Apache JMeter. So let’s start:

1. Click right button on Test Plan and add one Thread Group. Here you can set test parameters like the number of threads (users), ramp-up period and loop count.

step_12. Add Config Element -> User Defined Variables to Thread Group. In this element you can set initial values for variables: link_1, random_number and path_depth. I used name “link_1” because further you will be using the list of links named link_n. More about this later. User default variables3. Add Logic Controller -> Loop Controller to Thread Group. In this loop you will send requests to page on each depth level. You have to set field “Loop Count” : ${path_depth}. 4. Add Sampler -> HTTP Request to Loop Controller. In this sampler you must set a server name, port and page path. In your case, the path should be a random link. As I mentioned before you have to use the list of links. For this reason you have to use nested variable. First, I tried to set ${link_${random_number}}. But this notation is not permitted. You must use function:

${__V(link_${random_number})}

More about this notation in JMeter Manual.

HTTP Request

5. Add Post Processors -> XPath Extracor to HTTP Request. You should set Reference Name as “link” and when XPath query will have multiple matches, each element will have the name “link_n”. To receive all links you must check ‘Use Tidy’ option and set XPath query. In your case it is:

//a/@href

XPath Extractor

6. A very IMPORTANT STEP. Add Post Processors -> BeanShell PostProcessor to HTTP Request. Here you can put  BeanShell code which randomizes your test. If you don’t want to know what is BeanSheel, you can assume that his is a simple Java code which generates a random number and sets it as JMeter variable.

import java.util.*;

r = new Random();
max_string = vars.get("link_matchNr");

max = Integer.parseInt(max_string) ;
random_number = r.nextInt(max) + 1;

vars.put("random_number",random_number.toString());

You have to add this code to “Script” text area. 7. In each iteration you should check if the page has any links. You can do this by adding Logic Controller -> If Controller to Loop Controller. Set the condition:

${link_matchNr} == 0

and add to this controller Sampler -> Test Action and choose action : “Stop Now”. This Controller and Test Action will stop test for user when the page has no link. If Controller and Test Action8. This is the last step in which you can see the results of the test. Add Listener -> Aggregate Report to Thread Group. In this report you will see the results of performance measurement. Of course you can add other reports as well. Aggregate report

My tips

When I was testing my application, I didn’t know what was currently tested and I didn’t have independent values for each page in my report. Fortunately, I solved this issues in the following way:

  1. To see what is being currently tested I added Listeners -> View Results Tree to Thread Group. It is a great report type. You can see what each Sampler looks like, particularly HTTP Requests or Debug Samplers. For each Sampler you can see its result, request and response data. You can even render HTML sites or XML tree.
  2. To have independent values for each page you have to give each page a different name. I can do this in two ways:
    • When I want to see the whole URL I set HTTP Request name as: ${__V(link_${random_number})}
    • When I want to see URL without query string I set HTTP Request name as: ${__BeanShell(link = vars.get(“link_”+vars.get(“random_number”)); String[] str=link.split(“[?]”); return str[0];)}

View Result Tree

Summary

JMeter worked fine for me. This tool helped me measure my website performance as well as check if it works correctly. It was pleasure to use Apache JMeter and I will certainly use it in the  future. END NOTE: Don’t try to test websites with large number of threads(users) which aren’t yours. This kind of testing could be recognized as Denial of Service attack. Share your opinion and experience with us below or meet us on Twitter: @GOYELLO.

Thanks to Karol Świder for helping me write this blog post.

Aspire Blog Team

Aspire Systems is a global technology services firm serving as a trusted technology partner for our customers. We work with some of the world's most innovative enterprises and independent software vendors, helping them leverage technology and outsourcing in our specific areas of expertise. Our services include Product Engineering, Enterprise Solutions, Independent Testing Services and IT Infrastructure Support services. Our core philosophy of "Attention. Always." communicates our belief in lavishing care and attention on our customers and employees.

20 comments

  1. It looks good,I have learn a recruit!
    Recently,I found an excellent online store, the “http://www.air-jordan-18.com are completely various, good quality and cheap price,it’s worth buying!

  2. Hhe article's content rich variety which make us move for our mood after reading this article. surprise, here you will find what you want! Recently, I found some wedsites which commodity is research-laboratory colorful of fashion. Such as that worth you to see. Believe me these websites won’t let you down.

  3. Hewlett-Packard Co.'s standards of businessconduct suggest that employees pose themselves a simple test to decide whether an action is appropriate: Desupra.com is a leader in the development ,manufacture and sales of brand sport supra shoessupra shoes. We are a professional shoes and sneaker Industrial company.As a famous brand shoes supra skytopsupra skytopmarketing company. We've already have hounds of housands supra footwear.ofloyal customerssupra footwear.chenmiaomiao0809

  4. Hi,
    Can you tell me how to access user defined variable inside an attached file? I created a Http req. sampler, method POST, created a variable myvar which is a Random Variable. When I use ${myvar} to populate a header it works but when I use it inside the file I am sending as the POST body it is not replaced.

  5. Hi could you help me in my project, I have many text file which I have to pass one by one with Http request, one text file for one time as a POST parameter. So How can I implement it in JMeter?  

  6. Man, this is awesome! Very close what I’m need to do! And, of course, I was in trouble 🙂
    Thank you for sharing!

  7. I am using following syntax to get value but it is not working , same expression working for a HTTP request but not working for another.

    ${__V(STU_IDsub${index})}

    I am evaluating this value in a while loop

    ‘index’ is a counter variable

    ‘STU_IDsub’ is a variable having actual value

    when I use this expression in HTTP Request and use ‘View Results Tree’ it is evaluating in’STU_IDsub1′ or ‘STU_IDsub2’ and so on…

    while actual value evaluated in DebugSampler are

    STU_IDsub1= 36, STU_IDsub2=46 and so on which are correct values, I need thesevalues(36,46…) not STU_IDsub1,STU_IDsub2

    what I am doing wrong?

    what is the other way to get ‘STU_IDsub’ ‘s nth value?

  8. nice article, but damn I’m unlucky: ERROR – jmeter.util.BeanShellTestElement: Cannot find BeanShell: java.lang.ClassNotFoundException: bsh.Interpreter

  9. Pingback: tu bu

Comments are closed.