Advance jMeter Tutorial
Till now we have covered basic configuration setup in jMeter
link to First Part : https://medium.com/gradeup/introduction-to-apache-jmeter-beginners-guide-466b741d33e5
Let’s learn some of the useful and important features in jMeter
Counter Variable : Counter variable is element in jMeter which helps in creating data through API.
Steps : Right click on Thread Group >>Add >>Config Element >>Counter.
Usage : Suppose there is a scenario where you have to create 100 entities with different name like “live class 01” ,”live class 02" etc.You will have to write it 100 times to reach “live class 100” we can use counter variable like “live class${variable name}”.
Setup :
Start - Initial Value make it 1.
Increment - This is how much value you want to add with the current counter value. i.e. user {1} , User{3} here increment value is 2.
Maximum - Maximum value of the counter ie if it is set to 8 then user{8} will be the last request. it is generally left blank.
Number Format - This is to change the number format if it is blank then you will get normal values like 1,2,3 etc. You can set it 00 , 000 etc. Key is to leave it as it is.
Reference Name - This is variable in which your counter value is gonna store. ie if it is Val then you have to use it using ${Val} Where-ever applicable.
Bean Shell Useful functions :
jMeter provides lot of inbuilt functions which can help to create various kind of data in terms of date ,current time stamp etc. There comes a scenario where you have to deal with date ,Time and days in your api i.e. suppose you need to add an entity where api takes current time in a specified format the you can simply use below functions.
long a = ${__timeShift(,,,,)}; — Creates the current date in milliseconds and assigns the resulting value to the variable “a” with the data type long
log.info(“Current date in millis: “ + a); — Displays the created date in the the JMeter console
String b = “${__timeShift(dd/MM/yyyy,,,,)}”; — Creates the current date, in the specified format, and assigns the resulting value to the variable “b” with the data type String.
${__timeShift(dd/MM/yyyy,,P1D,, c)}; — Creates the current date, in the specified format, plus one day. “C” is the name of the variable to which the value is assigned.
${__timeShift(dd-MM-y-HH-mm-ss,,P1DT3H4M,, c1)}; — Creates the current date, in the specified format, plus one day, 3 hours and 4 minutes. “C1” is the name of the variable to which the value is assigned.
${__timeShift(dd/MM/yyyy,,P-1D,, c2)}; — Creates the current date, in the specified format, minus one day. “C2” is the name of the variable to which the value is assigned.
${__timeShift(dd/MM/yyyy,,P365D,, c3)}; — Creates the current date, in the specified format, plus 365 days. “C3” is the name of the variable to which the resulting value is assigned.
“${__timeShift(y MMM dd HH:mm:ss,,,fr_FR, c4)}”; — Creates the current date in the specified format that will be displayed in French.
${__timeShift(dd/MM/yyyy,10/10/2010,P365D,, c5)}; — Creates a date on 10/10/2010 plus 365 days.
Days Calculation between two dates : ${__BeanShell(((${__timeShift(,,,,)}L — 1559586600000L) / 86400000L).toString())}
Suppose you have to calculate days between Today and specified date (Current TimeStamp-TimeStamp of the date /1day in milliseconds)
Debug Sampler
Steps to add : Right click on Samplers >> add samplers >> debug sampler.
This is very useful when we have several user defined variables and we want to check whither we have got values in those variables, This will display all the values which we have extracted from different components like jSon extractor , Regular Expression extractor , CSV data setup config.
This will print all the used values.
Logs
Many a times our jMeter jmx File Does not run and there are errors which we cannot trace to check the errors there is an exclamatory icon to over top right corner of the jMeter click on icon to see logs.
if you want to close it click on it once more.
Assertions in jMeter :
Assertions as the name specifies are useful to verify that we are getting the expected data.
There are many types of assertion ,Here i have covered the most used assertion Types.
Steps to add : Right click on Thread Group >>Add >>Assertions
1.Response Assertion:
It is most common used assertion ,It gives various parameters to verify the data which we get we can select the scope using options on top ie we can choose
Main Samples , Sub-Samples , Main Samples and Sub Samples etc.
Following are the parameters
a)Text Response b)Response Code c) Text etc.
We can choose multiple conditions by selecting pattern matching rules most of the use cases can be covered by using Response Assertion.
2.Duration Assertion
It basically tests the amount of time taken by API to respond
Example : if an API takes 10 ms to respond and to make assertion for this specify 10 ms in “duration to assertion” parameter ,if API takes more time than 10 ms then it will be fail.
3.Size Assertion
Size Assertion is used to verify the server response contains expected number of Bytes or not.
If an API returns 200 bytes of data then we can make a check on size and fix a value as 200 bytes now if api will return data less or greater than 200 bytes then test case will fail.
4. XML Assertion
XML Assertion is used to verify that the server response data comprises of a correct XML document or not.
5. HTML Assertion
HTML Assertion is used to verify that the response contains correct HTML syntax or not using JTidy (HTML Syntax Checker).
Logic Controllers :
Logic Controllers helps in defining the order of processing request in a Thread. It helps you to determine when to send the request to server.There are several types of controllers here i am covering most used controllers in jMeter.
Steps to add : Right Click on Thread Group -> Hover over Add -> Hover Over Logic Controllers -> Click on the required Logic Controllers
Simple Controllers : This is used for grouping and ordering of request in that controllers.
Loop Controllers : Loop controllers are used to execute number of request multiple times count as specified in loop controller settings.
If the loop count is set to 10 and number of thread request are 2 then total 2*10 = 20 will gonna execute.
Module Controllers : Module controllers are used to group the request ie suppose you require sign in for request like my purchases , my coins etc
You can put my purchases and my coins request into simple controller module -> Add module controller ->and select which controller to run here only 1 is there so by default it is gonna execute.
Include Controllers : Include controllers are used ,Where you have dependency on a particular request to help other requests to get executed
let’s take 4 requests in which login request is common to all.
a) Login ->Login.jmx (Separate jmx file) ….
->Add include controller ->Add “Login.jmx” in that ->Add all requests in the simple controller in same thread group and execute.
b)My Purchases c)My TestSeries package d)My Coins.
Interleave Controllers : It picks up and execute single request run in each loop of the thread. ie
Take a login and my purchases request , Let’s suppose you have used a loop controller with value 5 so total number of executed request will be 2*5 = 10./
When you will use interleave controller the total number of executed requests will be 2.
PreProcessors :
They are used to execute actions before the sampler requests are executed in the test scenario.
There are various types here i will explain the most used PreProcessor :
BeanShell Pre -Processor : Let’s understand this with an example ,Suppose there is an api which requires an authentication token for registration.
You can use script in java to generate random string token and put it using
vars.put(“expected token”, “generated token(using code)”).use value as ${expected token} where you have to use.
JSR223 Pre-Processor : This is exactly same as Bean Shell Pre -Processor only thing which you have to keep in mind is we can write code in ecmascript, ,groovy , java, javascript, jexl and nashorn.
HTML Link Parser : It can be used to parse a response, extract all the found links and request them further we can use it in making simple web crawler.
Lets take a website www.google.com make api hit on that to get data ->it returns data in the form of html here we can use ->add (.*)google.com(.*) in the path of HTML link parser and use it using while loop (Which will have condition for request to get stop.)
Sample Timeout PreProcessor : The Sample Timeout PreProcessor specifies the max time execution for the specified sampler. If the sampler execution takes too long, it will be interrupted.
Timers :
To introduce delay while testing api generally , jMeter provides timers to have pauses and delays while testing.
Steps :
There are several timers which are there listed below
- Constant Timer :
This element delays each request in a Thread Group for the same amount of time.
2. Uniform Random Timer :
This element is used to delay each request for a random period of time.
Total amount of delay = Random Delay Maximum + Constant Delay Offset
3. Gaussian Random Timer :
This element is used to delay each user request for a random period of time. It has a random deviation around the Constant Delay Offset based on Gaussian curve distribution.
For Example:
1.Deviation Value: 100 milliseconds
2.Constant Delay Offset Value: 300 milliseconds
Approximately 68% of the delays will be between (200–400) ms
- Constant Delay Offset — Deviation = 200 ms
- Constant Delay Offset + Deviation = 400 ms
4.BeanShell Timer :
BeanShell Timer element can be used to generate delay between each user request.
5.BSF Timer :
BSF Timer element can be used to generate delay using a BSF scripting language.
6.JSR223 Timer :
JSR223 Timer element can be used to generate delay using a JSR223 scripting language.