4

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 Answers 4

2

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

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

enter image description here

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

0
  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:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.