I have gone through the bean shell scripting in jmeter but i did not find any example of that, how it is useful in jmeter and which way.means reading the sampler values etc. Can any one explain bean shell scripting in Jmeter with example.In beanshell post/pre processor script where we write the script. I am struggling with this what is the actual usage of it .Please explain with this .it would be great help for me or others as well for understanding the usage of it.
问题:
回答1:
If you look into "Script" section of Beanshell Post Processor you'll see the following:
Script(variables: ctx, vars, props, prev, data, log)
ctx - stands for JMeterContext, provides access to JMeter Context API (see JavaDoc for details). Example usage:
int threadNum = ctx.getThreadNum(); // get current thread number
vars - stands for JMeterVariables. Using
vars
you can get/set variable values.String myvar = vars.get("myvar"); // get ${myvar} variable value and store it to myvar string myvar = myvar + "something"; // append "something" to myvar vars.put("myvar", myvar); // put new value into ${myvar} variable
props - stands for JMeter Properties. Basically the same as variables, but variables visibility is limited to current thread group only and properties are "global"
prev - shorthand to previous SampleResult. Seems to be exactly what you're looking for. You can get/set start time, end time, execution time, latency, URL, response code, response message, etc. See JavaDoc for comprehensive information. Example usage:
String code = prev.getResponseCode(); String message = prev.getResponseMessage();
data - byte array containing parent sampler response data
String samplerData = new String(data); System.out.println(samplerData);
log - can be used to print something to jmeter.log file
log.info("This line has been written by Beanshell Post Processor");
See How to use BeanShell: JMeter's favorite built-in component guide for more details and real-life examples.
回答2:
If you want to perform computations between requests, Beanshell will help you to achieve it in jmeter. We have Beanshell Sampler, Beashell Pre Processor and Beanshell Post Processor. For an example create a thread group and add a beanshell sampler as in figure. Under script enter
var a=1;
var b=2;
var c=a+b;
log.info("sum="+c);
and run with log viewer enabled(Options menu> Log Viewer).
You can call java methods of a jar (should be in jmeter_folder/lib/ext) using beanshell script.
Beashell Pre Processor are used to perform computations and send the values along with the request. Suppose if you want to encrypt the username and password before being sent. You can provide credentials, encrypt it using beanshell/java methods and set it as variables in beanshell script (vars.put("variablename",variablevalue)
) . You can add the variable to request as http://test.com?parameter=${variablename}
.
Similarly Beashell PostProcessors are used to process the response. Suppose you want to decrypt a value from the response, extract the value (using regular expression extractor) and decrypt using beanshell script.
回答3:
For example I use JMeter to create a Customer. If the response message is Created
, set result to Pass
; Otherwise set the result to Fail
, failure message to Note:Creation failed
. The steps are:
- Assemble the http request using
TTP Request Sampler
. - Add a
BSF Assertion Sampler
under it. - Find methods I need to use from http://jmeter.apache.org/api/index.html. Since I need to manipulate the
Http Request Sampler
, I go directly to packageorg.apache.jmeter.protocol.http.sampler
. If you are familiar with those methods, skip this step.
- Finish the BSF sampler. The
prev
stands for previous sample result
回答4:
You can use the BeanShell (or better: the JSR223 PreProcessor/PostProcessor/Sampler) scripting engine to calculate parameters you need for your test. I use this for several different kinds of operations:
- Selecting a random file to upload from a directory
- Calculating hmac keys for upload/download authorization (necessary for Swift)
- Setting variables for a specific environment (based on a parameter)
Here an example script to select a random file and write the specifics of the file to variables, that you can access in the next steps:
File folder = new File(vars.get("image_path"));
File[] imageFiles = folder.listFiles(new FileFilter() {
public boolean accept(File pathname) {
return !pathname.isHidden();
}
});
Random rnd = new Random();
File selected = imageFiles[rnd.nextInt(imageFiles.length)];
String file = selected.getAbsolutePath();
String extension = file.substring(file.lastIndexOf('.')+1);
String mimetype = URLConnection.guessContentTypeFromName(file);
vars.put("CURRENT_FILE", file);
vars.put("FILE_EXT", extension.toUpperCase());
vars.put("MIME_TYPE", mimetype);