4
votes

Requirement: Post response time of every sampler to Mongo DB, for that created an HttpClient java code(Class function).

Problem: Need to call the custom (reuse same code between 150+ jmx file) Java function to be called from Beanshell after every sampler.

void logTransaction(responseTime,samplerName);

Solution need: Incorporate custom java function(.c or .txt) with jmx file.

For example in Load runner we can include .h file(shared in common path) with reusable code and include in globals.h.

Is there any method or solution available with jmeter ?

4

4 Answers

2
votes

You can run same script file in JSR223 Sampler which support also Java/Beanshell languages.

Put in Script file field the script file name and e reuse it in different jmx files

1
votes

Create a JAR that contains your reusable java code and call it from JSR223 Listener checking "Cache compiled script if available".

JSR223 Listener

Code your call using default scripting language (Groovy):

def yourInstanceClass = new YourClass()
yourInstanceClass.logTransation(sampleResult.getTime(), sampleResult. getSampleLabel());
1
votes

enter image description here

Code can be reusable by saving in a java file and give the file path in JSR223.

0
votes
  1. Add the next line to user.properties file (located in the "bin" folder of your JMeter installation)

    beanshell.sampler.init=BeanShellSampler.bshrc
    
  2. Put your logTransaction function in the BeanShellSampler.bshrc file (same location, JMeter's "bin" folder)
  3. Next time you start JMeter you will be able to call the function from any Beanshell Sampler in any script

References: