We can use the getIteration() function to get the thread loop count in properties and also in shell
${__BeanShell(vars.getIteration();,)}
vars.getIteration(); log.info("\n\nloop : "+ vars.getIteration() );
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+"/");
BeanShell Sampler
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
HTTP Request
View Results Tree, csv input file and result
In shell we can use
int tpos = ctx.getThreadNum();
In properties
${__threadNum}
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/
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(); }