Jmeter. BeanShell PostProcessor

偶尔善良 提交于 2019-11-30 02:23:01

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.

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.

wenwen

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:

  1. Assemble the http request using TTP Request Sampler.
  2. Add a BSF Assertion Sampler under it.
  3. 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 package org.apache.jmeter.protocol.http.sampler. If you are familiar with those methods, skip this step.

  1. Finish the BSF sampler. The prev stands for previous sample result

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);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!