4

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.

Aliaksandr Belik
  • 12,725
  • 6
  • 64
  • 90
user1788294
  • 1,823
  • 4
  • 24
  • 30
  • A simpler approach I tend to use is to run use the script beforehand to create a file with parameters that is read via the JMeter CSV handler resulting in a `${imgid}` variable in your sampler URL. – rsp Jul 24 '14 at 10:44

3 Answers3

5

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.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
3

A possible solution posted by Eugene Kazakov here:

JSR223 sampler has good possibility to write and execute some code, just put jython.jar into /lib directory, choose in "Language" pop-up menu jython and write your code in this sampler.

Sadly there is a bug in Jython, but there are some suggestion on the page.

More here.

Dominik Antal
  • 3,281
  • 4
  • 34
  • 50
  • i would not recomand jython library as its very bugy, it worked fine for me with low number of threads, but when running a load test with many threads it just kept getting deadlocks and crashed my tests – omriman12 Jun 11 '22 at 04:04
2

You can use a BSF PreProcessor.

First download the Jython Library and save to your jmeter's lib directory.

On your HTTP sampler add a BSF PreProcessor, choose as language Jython and perform your needed magic to obtain the id, as an example I used this one:

import random
randImageString = ""
for i in range(16):
    randImageString = randImageString + chr(random.randint(ord('A'),ord('Z')))

vars.put("randimage", randImageString)

Note the vars.put("randimage",randImageString") which will insert the variable available later to jmeter.

Now on your test you can use ${randimage} when you need it:

HTTP Sampler with parameters

Now every Request will be different changing with the value put to randimage on the Python Script.

alphamikevictor
  • 595
  • 7
  • 17
  • 1
    i would not recomand jython library as its very bugy, it worked fine for me with low number of threads, but when running a load test with many threads it just kept getting deadlocks and crashed my tests – omriman12 Jun 11 '22 at 04:04