Reputation: 11
I'm using a loop of beanShell samplers starting threads and, for each thread, a python script is executed, that returns a particular string. I want the thread to fail if the returning string contains a specific error.
Here my code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
Runtime r = Runtime.getRuntime();
Process p = r.exec("python myscript.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);
response.append("\n");
}
b.close();
if (response.toString().contains("ERROR-TRAFFIC")){
SampleResult.setResponseCode("500");
SampleResult.setSuccessful(false);
}
Using this code, if condition is correctly executed when the error is present, but in the traffic results I don't see any thread failure. Any hints ??
Upvotes: 1
Views: 222
Reputation: 168217
Check jmeter.log file for any suspicious entries
Check if your response
variable contains the line you're looking for, you can print it to i.e. the aforementioned jmeter.log
file with the following line of code:
log.info(response.toString());
and see what is being returned
Since JMeter 3.1 it's recommended to use JSR223 Sampler and Groovy language for scripting mainly for performance reasons. Moreover Groovy has some "syntax sugar" so your code can be shortened to something like:
String response = "python myscript.py".execute().text
if (response.contains("ERROR-TRAFFIC")){
SampleResult.setResponseCode("500");
SampleResult.setSuccessful(false);
}
More information on Groovy scripting in JMeter: Apache Groovy: What Is Groovy Used For?
Upvotes: 0