Read a file, load a few 100 ids and randomly choose one for each request

Viewed 22

I want to make a load test that will read a file and randomly choose a number 1 - 100 and that is the id the request will use.

in my script I have...

String[] split = new String[0];
try(
      BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("/Users/tyeman/programming/jmeter-test/src/ids.txt"), StandardCharsets.UTF_8))){
  String line;
  while((line=br.readLine())!=null){
      split=line.split(",");
      
  }
  ads = Arrays.asList(split);
} catch (IOException e) {
  throw new RuntimeException(e);
}

for(String id : ads){
  log.info("id " + id);
  vars.put("ids", id);
}

in my http request I have a post request with a body of

{   "id": "${id}",
    "time": "${timeStamp}"
}

I have a Foreach controller but when I put the http-request, scripts etc under it I get

enter image description here

2022-09-19 16:41:24,717 WARN o.a.j.c.TransactionController: Could not fetch SamplePackage
2022-09-19 16:41:24,717 WARN o.a.j.c.TransactionController: Could not fetch SamplePackage
2022-09-19 16:41:24,718 INFO o.a.j.t.JMeterThread: Thread finished: ThreadGroup 1-8
2022-09-19 16:41:24,718 ERROR o.a.j.JMeter: Uncaught exception in thread Thread[ThreadGroup 1-8,6,main]
java.lang.StackOverflowError: null

But if I everything under the transaction controller enter image description here

I do not get any errors but I do not think the ForEach controller is working correctly.

enter image description here

In the end I want the test to choose a random id from my known id file in order to get a successful response from the endpoint.

1 Answers

I don't think your code is working correctly, at least I believe you need to change this block:

for(String id : ads){
    log.info("id " + id);
    vars.put("ids", id);
}

to something like:

int counter = 1

for (String id : ads) {
    log.info("id " + id);
    vars.put("ids_" + counter, id);
    counter++;
}

Unfortunately I'm not telepathic enough to say what else is wrong without seeing how does your ids.txt file look like

Also be aware of __Random() function which can return a random number in the specified range. If you prefer to use the Random CSV Data Set Config plugin (this guy needs to be installed using JMeter Plugins Manager)

Related