How to Handle a Variable Number of Parameters in Apache JMeter
Effectively dealing with parameters and requests is a key part of creating a high-performing application. Read on to learn how to use JMeter to handles params.
Join the DZone community and get the full member experience.
Join For FreeIf your request has a lot of parameters which are being passed to HTTP, creating a request to manually correlate the data will be a cumbersome process, where we can use scripting capabilities using any of the preprocessors.
I have got almost 484 parameters which have to be correlated, as shown below.
Capture all the param names and param values using a regular expression extractor and dynamically send the argument with a “key,Value” pair using Sampler with the predefined variable, as shown below:
paramname = <input type=”hidden” name=”(.+?)” value=”[a-z|0-9]+”
paramvalue = value=”([a-z|0-9]+)”
The string which is enclosed in parenthesis () will get stored in a reference name.
In order to capture all the match occurrences, we will use Match, now attributed as “-1,” as below.
Steps:
1. Capture all the param names and param values parameters as above by using a regular expression extractor.
2. Based on the param count we can construct the dynamic parameters using a preprocessor.
3. Add a JSR223 Preprocessor to the HTTP request as below, and delete the parameters from the HTTP request.
The JSR223 PreProcessor allows JSR223 script code to be applied before taking a sample.
The following JSR223 variables are set up for use by the script:
- log – (Logger) – can be used to write to the log file.
- Label – the String Label.
- FileName – the script file name (if any).
- Parameters – the parameters (as a String).
- args – the 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”);
- sampler – (Sampler)- gives access to the current sampler.
- OUT – System.out – e.g. OUT.println(“message”).
For details of all the methods available on each of the above variables, please check the Javadoc.
Using a sampler variable we can access the addArgument method, which passes a name and value, which can be constructed dynamically using the below code:
public void addArgument(String name,
String value)
//vars – (JMeterVariables) – gives read/write access to variables
//vars.get(“jmetervariable”) – gets the variable value which is in string format
int count=Integer.parseInt(vars.get(“paramname_matchNr”))
log.info “parameters count” + count
for(int i=1;i<=count;i++){
sampler.addArgument(vars.get(“paramname_” + i),vars.get(“paramvalue_” + i))
}
log.info “completed”
We can verify the request in view results tree as below.
This way we can construct a dynamic number of requests using simple variables.
Published at DZone with permission of Manojkumar Tenali. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Five Java Books Beginners and Professionals Should Read
-
How To Use Geo-Partitioning to Comply With Data Regulations and Deliver Low Latency Globally
-
A React Frontend With Go/Gin/Gorm Backend in One Project
-
Tactics and Strategies on Software Development: How To Reach Successful Software [Video]
Comments