2013-04-08

What are the Post Processors in JMeter?

In this article we are going to see what are the post processors of JMeter. For basic idea on Jmeter, see my Previous posts or the Jmeter section under Tool tab.

What is post processor? As we know, jmeter provide request to server and get the request from the server, a post processor can be used to process those data after fetching. You may ask, why. Because, by this action, we can fetch suitable information from the response data. As, post processors process data of response data, so all post processors should be used as child of different type of samplers. Usually it works only with current samples. During execution, post processors run before assertions. So, they can't have assertion results.

There are some technique to use this. We will discuses most of them about that. Let's start with basic introduction with post processors , then let's see how to use individual of them.

How to add post processor? if we right click to a sampler and get the 9 types( with Google plug-ins, 13) of post processors.


So, these are the post processors :

1. BeanShell PostProcessor : Why we use this ?
-To run BeanShell script for getting necessary info from response data.Ex-
-To show save results from response (in file/ command prompt/ etc)
- to get data and process from response

Basic elements are :
Reset bsh.Interpreter before each call : True = Interpreter will be recreated for each sample.
Note : Important for long scripts.
Parameters :
Script file : A file containing the BeanShell script to run. The file name is stored in the script variable FileName
Script : (if no file)The BeanShell script. The return value is ignored.
see this to know how to write scripts.  I will provide a separate post on BeanShell Scripts.

Default BeanShell variables are :
log - (Logger) - Writes to the log file
ctx - ( JMeterContext ) - Gives access to the context
vars - ( JMeterVariables ) - gives read/write access to variables: vars.get(key); vars.put(key,val); vars.putObject("OBJ1",new Object());
props - (JMeterProperties - class java.util.Properties) - e.g. props.get("START.HMS"); props.put("PROP1","1234");
prev - ( SampleResult ) - Gives access to the previous SampleResult
data - (byte [])- Gives access to the current sample data
If the property beanshell.postprocessor.init is set, it will load an initialisation file, defining methods,etc for use in the BeanShell script.

2. BSF PostProcessor: Why we use this ?
 -To extract information PostProcessor allows BSF script code to be applied after taking a sample.

Basic elements are :
Language : The BSF language to be used(one of supported language)
Parameters:  parameters are stored in the variables:
     a. Parameters - string parameters as a single variable
     b. args - String array parameters, split on white-space
Script file: Path of the script file( referenced by "user.dir" System property)
Script: The script to run.

Default variables :
log - (Logger) - Write to log file
Label - String Label
Filename - script file name (if any)
Parameters - parameters (as a String)
args[] - parameters as a String array (split on whitespace)
ctx - ( JMeterContext ) - gives access to the context
vars - ( JMeterVariables ) - gives read/write access to variables: vars.get(key); vars.put(key,val);
vars.putObject("OBJ1",new Object()); vars.getObject("OBJ2");
props - (JMeterProperties - class java.util.Properties) - e.g. props.get("START.HMS"); props.put("PROP1","1234");
prev - ( SampleResult ) - gives access to the previous SampleResult (if any)
sampler - (Sampler)- gives access to the current sampler
OUT - System.out - e.g. OUT.println("message")

3. CSS/JQuery Extractor : Why we use this ?
-To extract information from a server response using a CSS/JQuery selector(syntax). Ex-
-To get desirable CSS/JQuery information from response CSS/JQuery
-To get style, data, module

Basic elements are :
Apply to : It will indicate where the extracted information will be used
Main sample only (information useable in main/used sample)
Sub-samples only (information useable in sub-samples)
Main sample and sub-samples - information useable to both.
JMeter Variable - information will be checked with the variable declared here.
Note: This variable should be present previously.

CSS/JQuery extractor Implementation: From 2.9, JSOUP and JODD syntax supported. JSOUP is default.
Reference Name : The variable name that we choose to use the extracted info.
CSS/JQuery expression : HTML purser expression to extract information. See the syntax link for JSOUP and JODD. I will make separate post for this two type of HTML pursers.

Attribute : Name of attribute depend on which syntax we are using. Ex-value. This indicates the value of the css for particulate element.
Match No : Telling which match to use. 0=Random, n=nth match, -n=
used for adding this to ForEach controller
Default Value : If no match, which value will be used.

4. Debug PostProcessor : Why we use this ?
-To create a subSample with the details of the previous Sampler properties, JMeter variables, properties and/or System Properties.
-To use for debugging , effects can be seen with View Result Tree Listener in response data.
Basic elements are :
JMeter Properties : Enables showing JMeter Properties(default False)
JMeter Variables : Enables showing JMeter Variables (default False)
Sampler Properties : Enables showing Sampler Properties(default True)
System Properties : Enables showing System Properties (default False)
 
5. JDBC PostProcessor : Why we use this ?
-To Run a SQL statement just after a sample. Ex-
-To reset state of database that might be changed for testing.
-To validate db changing after testing
Basic elements are :
Variable Name : The variable of jmeter which will get the result of SQL.
SQL Query :
Query Type : Type of Query in combo box

Query : Query as text
Parameter values :  The value will get data from a jmeter variable.
Parameter : Will define what type of parameter inputed from jmeter variable.
variable : Name of the variable that we insert as parameter.
Result variable name : If we assign the result with new jmeter variable.

6. JSR223 PostProcessor: This is same as BSF PostProcessor but allows JSR223 script to run

7. Regular Expression Extractor:  Why we use this ?
-To get our necessary information from response data using Perl regular expression.(work like as Regix) I will make a different post for the regular expression Ex-
- To get header items like token, Session ID, JSON id, cookie id , any spatial variable or information passed.
-To use the founded information for next requests
Basic elements are
Apply to : It will indicate where the extracted information will be used
Main sample only (information useable in main/used sample)
Sub-samples only (information useable in sub-samples
Main sample and sub-samples - information useable to both.
JMeter Variable - information will be checked with the variable declared here.
Note: This variable should be present previously.
Response Field to check :
Body - Check the response body (no headers)
Body (unescaped) - Check the response body(with all Html escape codes replaced
Note : It has performance issues, better to avoid this

Body as a Document - Check various type of documents via Apache Tika (like as View Result Tree's[a listener] doc view).
Headers - Check the headers only( so, should be used with http sample only)
URL : Find in URL
Response Code - Check HTML code. like - 200
Response Message - Check HTML response message like -OK
Reference Name : The variable name that we choose to use the extracted info.
Regular Expression : Perl format expression to extract information(more info will be in separate post)
Template : It indicates which group result will use as string. $2$ means the 2nd group's found string will be used .
 Match No : Indicates which match to use from the group. 0=randome, n(any number) =nth Match, -n=(used for adding this to ForEach controller)
 Default Value : If no match, which value will be used.
Note : Use this for debug(i use my name to find results)

8. Result Status Action Handler : Why we use this ?
- To stop the thread or the whole test if the relevant sampler failed.
Actions are : 
Continue - Continue test by  ignoring error
Start next thread loop - Stop current iteration on error(following the sampler) and restarts the loop to next iteration
Stop Thread - Current thread exits on error
Stop Test - Entire test is stopped at the end of any current samples.
Stop Test Now - The entire test is stopped abruptly(immediately) .
Note: These are same condition like as every type of thread group.

9. XPath Extractor
: Why we use this ?
- To extract info from structured response. Ex-
-to get a table's nth field's value(using query)
-to get attribute's name(using query)

Basic elements are :
Apply to : It will indicate where the extracted information will be used
Main sample only (information useable in main/used sample)
Sub-samples only (information useable in sub-samples
Main sample and sub-samples - information useable to both.
JMeter Variable - information will be checked with the variable declared here.
Note: This variable should be present previously.
Use Tidy (tolerant parser) :
-check this for HTML response, it will be converted to valid XHTML using Tidy
-Un check this for XHTML and XML responses.(like - RSS Feed)
Quiet : Sets the Tidy Quiet flag(enabled with tidy)
Report Errors : If a Tidy error occurs, then set the Assertion accordingly(enabled with tidy)
Show warnings : Sets the Tidy showWarnings option(enabled with tidy) 
Use Namespaces : (if tidy is not selected)
-XML parser will use namespace resolution
Note : Namespaces declared on the root element will be recognized
Validate XML : Check the document against its schema.(if tidy is not selected)
Ignore Whitespace :Ignore Element Whitespace.(if tidy is not selected)
Fetch External DTDs : If selected, external DTDs are fetched(if tidy is not selected)
If "Return entire XPath fragment instead of text content?" is selected, the fragment will be returned rather than the text content.
Reference Name : The variable name that we choose to use the extracted info.
XPath Query : Desire XPath query . I will provide different post for this.
Default Value : If no match, which value will be used.

If we install Jmeter Plugins, we will get those additional. Detail will be given shortly.

1.jp@gc - Inter-Thread Communication PostProcessor
2. jp@gc - XML Format Post Processor
3. jp@gc - JSON Path Extractor
4. jp@gc - JSON Format Post Processor

Highlights :
-Post-Processors are used as a child of a sample
-Post-Processors are run before Assertions

Note : jmeter Plugin install instructions are here 

2013-02-24

What are the Timers in JMeter?

In this following article we are going to know about what is Jmeter Timers. The main purpose of the article will be, how can we use the timers, Why and where we use it.

By the name we can understand what is the timers. When we need to insert time between test steps or process(or sample) , we use this.
Timers are only processed in conjunction with a sampler. It is preprocessed to sampler.

- Timers are processed before each sampler(for single or multiple timers)
- A timer which is not in the same scope as a sampler will not be processed at all.
-If we need time after a sampler, we need to add to the next sampler

How to use
-Add the timer as a child element of any sampler
-Add the time parameter/script/conditions in the timer
-When we need to thread delay, use timers under thread.
-Some timers can be remotely invoked or scripted.

Types  : 
1. BeanShell Timer : It is for generating a standard delay. It supports  ThreadListener and TestListener methods. Important parameters are
a. Reset bsh.Interpreter before each call : When selected, the interpreter will be recreated for each sample. It can be helpful for long running scheduled  scripts
b. Parameters are passed to the BeanShell script
c. Script file contains the BeanShell script.File name is stored in the script variable FileName.
d. BeanShell script ,(returns values in milliseconds to wait)
 See this link as for more. I haven't use that.

2. BSF Timer : Making delay with using a BSF scrip.
See this link for more. I haven't used that.

3. Constant Throughput Timer : It aims to generate constant load to server. For that it changes the load parameters. It introduces variable pauses, calculated to keep the total throughput (samples/min) [As near value as possible]. We need to set the sample/min parameter along with thread to be affected. It is maintained by changing value by
a. A counter variable ,
b. A JavaScript/ BeanShell function to change value,
c.A remote BeanShell server to change a JMeter property 

-It try to keep the throughput, so there may some fluctuation .
-As it is implemented thread wise, add this under the test plan (not thread group)
-Use this for a big load generation over a limited resourced server.

4. Constant Timer : It will keep hold(pause) the step for some time. Values are provided in Milliseconds.
-Best practice to use with ajax or JS call.
-Good to simulate real life scenarios.

5. Gaussian Random Timer : When we need to pause threads for random amount of time, we use this. It has 2 parameter, a. Deviation, means this value will be difference between two random pause. b. Constant Delay Offset, means this value is constant delay after each random pause. All are in Milliseconds
-Best practice to see the behavior after any release( primary load checkup test case)
-Good for random run-time regression(health checkup) scenario. 

6.  JSR223 Timer : It generate a delay using a JSR223 script. See detail of BSF timer.

7. Poisson Random Timer : It pauses each thread for a random amount of time, with most of the time intervals occurring near a particular value.
-The total delay = Poisson distributed value +the offset value.
-Used for purposes same as gaussian random timer

8. Synchronizing Timer : It blocks thread for synchronizing the load. (it block threads until X number of threads have been blocked, and they are all released at once). "Number of Simultaneous Users to Group by" parameter means number of threads to release at once
-Used for generating large user instance at a same time
-Used for constant Max supported user hit
-Used for making a stress test environment for max support testing.

9. Uniform Random Timer : It is used for uniformly random delay(well, see, random can be uniform..:)... ). Random Delay Maximum provides max milliseconds of pause(random). Constant Delay Offset is the added milliseconds with random delay.
-Good for random run-time regression(health checkup) scenario.

10. jp@gc - Throughput Shaping Timer : (please install Google plugins to get that) By adding single or multiple row, it will show the graph of delay generation. Parameters under a row are, Start Rate Per Second, End Rate Per Second, Duration(of full delay time)



Thanks...:).. Please add comments on more uses of Timers.,