jMeter

jmeter get thread loop count

We can use the getIteration() function to get the thread loop count in properties and also in shell

in property

${__BeanShell(vars.getIteration();,)}
in shell
vars.getIteration();

log.info("\n\nloop : "+ vars.getIteration() );

 

By bm on May 9, 2016 | jMeter | A comment?

jMeter get current directory

import org.apache.jmeter.gui.GuiPackage;
import org.apache.commons.io.FilenameUtils;

String testPlanFile = GuiPackage.getInstance().getTestPlanFile();
String testPlanFileDir = FilenameUtils.getFullPathNoEndSeparator(testPlanFile).replace('\\', '/');

vars.put("currentDIR", testPlanFileDir+"/");

 

By bm on May 5, 2016 | jMeter | A comment?

jMeter foreach controller

BeanShell Sampler

1

String filePath = "path\\to\\your\\csv\\userdata.csv";

BufferedReader fbr = new BufferedReader(new FileReader(filePath));
String user;
int counter = 1;
while ((user = fbr.readLine()) != null) {
   vars.put("user_" + counter, user);
   counter++;
}
fbr.close();

ForEach Controller

2

HTTP Request

3

 

View Results Tree, csv input file and result

 

5

 

By bm on | jMeter | A comment?

jMeter get current Thread Number

In shell we can use

int tpos = ctx.getThreadNum();

In properties

${__threadNum}

 

By bm on May 4, 2016 | jMeter | A comment?

jMeter get current UNIX time stamp

In config we can use this

${__javaScript(new Date().getTime();)}

In Shell we can use the default java function

 time = System.currentTimeMillis();

 

 

online tool for converting time : http://www.epochconverter.com/

By bm on May 2, 2016 | jMeter | A comment?

How to use simple JSON with jMeter

Copy past json-simple.jar file in to jmeter lib folder (you can download jar file from http://www.java2s.com/Code/Jar/j/Downloadjsonsimple11jar.htm)

Example JSON

{
    "users": {
        "name": "myname",
        "age": "20"
    }
}

Shell

String response = prev.getResponseDataAsString();
JSONParser parser = new JSONParser();

try {
    Object obj = parser.parse(response);
    JSONObject jsonObject = (JSONObject) obj;
    JSONObject user= (JSONObject) jsonObject.get("user");

    log.info("Name : "+ user.name);

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} catch (ParseException e) {
    e.printStackTrace();
}

 

Iterating through JSON Object

String response = prev.getResponseDataAsString();
JSONParser parser = new JSONParser();

try{
    Object obj = parser.parse(response);
    JSONObject jsonObject = (JSONObject) obj;
    JSONObject message = (JSONObject) jsonObject.get("message");

    Set keys = message.keySet();
    Iterator a = keys.iterator();
    while(a.hasNext()) {
        String key = (String)a.next();
        // loop to get the dynamic key
        String value = (String)jsonObject.get(key);

        log.info("key : "+key);
        log.info(" value :"+value);
    }

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} catch (ParseException e) {
    e.printStackTrace();
}

 

Iterating through JSON Array

String response = prev.getResponseDataAsString();
JSONParser parser = new JSONParser();

try {

    Object obj = parser.parse(response);
    JSONObject jsonObject = (JSONObject) obj;
    
    JSONArray  changesArray = (JSONArray) jsonObject.get("changes");
    
    int le= changesArray .size();
    for(int i = 0 ; i < le; i++){
       log.info("\n"+ changesArray .get(i));
    }

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} catch (ParseException e) {
    e.printStackTrace();
}

 

By bm on April 29, 2016 | jMeter | 1 comment