Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JMeter - Run a python script before calling each HTTP request sampler

I am new to Jmeter. My HTTP request sampler call looks like this

Path= /image/**image_id**/list/
Header =  "Key" : "Key_Value"

Key value is generated by calling a python script which uses the image_id to generate a unique key.

Before each sampler I wanted to generate the key using python script which will be passed as a header to the next HTTP Request sampler.

I know I have to used some kind of preprocessor to do that. Can anyone help me do it using a preprocessor in jmeter.

like image 915
user1788294 Avatar asked Dec 25 '22 07:12

user1788294


1 Answers

I believe that Beanshell PreProcessor is what you're looking for.

Example Beanshell code will look as follows:

import java.io.BufferedReader;
import java.io.InputStreamReader;

Runtime r = Runtime.getRuntime();
Process p = r.exec("/usr/bin/python /path/to/your/script.py");
p.waitFor();
BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
StringBuilder response = new StringBuilder();
while ((line = b.readLine()) != null) {
    response.append(line);

}

b.close();
vars.put("ID",response.toString());

The code above will execute Python script and put it's response into ID variable.

You will be able to refer it in your HTTP Request as /image/${ID}/list/

See How to use BeanShell: JMeter's favorite built-in component guide for more information on Beanshell scripting in Apache JMeter and a kind of Beanshell cookbook.

You can also put your request under Transaction Controller to exclude PreProcessor execution time from load report.

like image 143
Dmitri T Avatar answered Dec 31 '22 14:12

Dmitri T