Running Java class with JMeter (Bean Shell)

2019-08-05 03:04发布

I have written a Java Class for use in JMeter, packaged the project as a .jar file and moved that file into the lib/ext folder in the jmeter directory. I have seen documentation on how to proceed but they give contradictory answers.

The first way is to use the BeanShell Sampler to import my package and class, create an object of the class and run the methods that way. I have used this method using example classes with more simple file structures than that of class I want to run. The example classes work with the following BeanShell script.

import tools.JmeterTools;
JmeterTools jt = new JmeterTools();
jt.foo();

When I try to use this method for the class I want to run, it states that the variable declaration is an error and the Class cannot be found. I assume this is because I do not understand what to import exactly, as the file structure in my project is a little odd.

The second uses the BeanShell PreProcessor to add the jar to the class path. This method I have not been able to get to work at all, but have read many accounts of others finding success. It works as follows:

addClassPath("directory path to jar\lib\ext\foo.jar");
JMeterTest jtm = new JMeterTest();
jmt.test();

Would anyone have any knowledge of which way would work better or any ideas on how to fix the import?

The import I have been using in the BeanShell script is the following:

import client.JMeterTest;

The package line at the top of my class is the following

import com.x.foo.client;

3条回答
叼着烟拽天下
2楼-- · 2019-08-05 03:50

For anyone who has this issue in the future. The answers given by others are correct. It wasn't working for me because I had forgotten that Maven does not package files in the test directory when a jar is made.

This link may help if anyone ever does this in the future. Generate test-jar along with jar file in test package

查看更多
Ridiculous、
3楼-- · 2019-08-05 03:53

You need to have your jar file in JMETER_HOME/lib folder.

lib/ext is for JMeter extensions/plugins etc.

Once you have placed your jar, you might have to restart JMeter.

查看更多
【Aperson】
4楼-- · 2019-08-05 03:57

Running external classes from Beanshell should work fine given the following preconditions met

  1. Your test with dependencies is located in JMeter classpath.
  2. JMeter restart is required to pick new libraries up
  3. You need to provide full package name plus full class name (or wildcard) for import.

    Either

    import com.x.foo.client.JMeterTest;
    

    or

    import com.x.foo.client.*;
    
  4. And finally it is recommended to use JSR223 Sampler and use "groovy" as a language. Beanshell interpreter has severe performance issues so use it for something very "light" like variable amendment, converting variable to property, etc. For generating the real load use JSR223 and groovy as it implements Compilable interface and hence you can achieve performance similar to native Java code. See Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For! guide for detailed explanation, benchmarking and instructions on installation of groovy scripting engine support.

查看更多
登录 后发表回答