Running Java class with JMeter (Bean Shell)

大兔子大兔子 提交于 2019-12-24 13:12:52

问题


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;

回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/30537211/running-java-class-with-jmeter-bean-shell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!