In doing Dubbo jmeter pressure measurement, the jar package needs to be placed in the JMeter Lib/ext directory, but the JMeter will automatically load the directory Lib directory and the Lib/ext directory, so that the boot into these directories after the jar package will not be loaded.
JMeter master--slave/client mode, as the JMeter Client,jmeter-server service has been started, when the new jar package into the client, cannot read, It is therefore necessary for the client's jmeter to dynamically load these newly placed jar packages.
Solution Reference: http://blog.csdn.net/kekadm/article/details/51783240
Following the previous article, "Jmeter+h2database Dynamic Deployment jar package to the proxy side" to achieve the test business jar Package dynamic deployment, do not restart the agent-side Jmeter, the jar changes can not automatically load into Jmeter memory, so still can not implement a boot, The purpose of the dynamic update.
Because the JMeter will automatically load the jar package in the Lib directory at boot time, the updated jar package in the directory cannot be loaded into memory without rebooting. Therefore, to implement the dynamic loading of a class, you must implement the overloading of the business class in the JMeter test class.
That is, in the JMeter test class, The Setuptest () method is used to customize the code to implement the load of the business class.
Here is a simple example to illustrate the implementation process:
Write the business class to be tested: Transdemo.java
Package Perftest.jmeter.trans;
Public class Transdemo {
Public String Action () {
Stringstr = "action1st.";
System. out. println (str);
return str;
}
Public voidinit () {
System. out. println ("Testingstart ...");
}
Public voidEnd () {
System. out. println ("testingover!!!!");
}
}
Export it as Perftest-trans.jar and place the package outside of the Jmeter/lib directory. such as: C:/perftest-trans.jar ( cannot be placed in the Jmeter/lib directory ).
Write JMeter test class: Transdemoactions.java
Package perftest.jemter.action;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import Java.lang.reflect.Method;
import Java.net.URL;
import Java.net.URLClassLoader;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import Org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import Org.apache.jmeter.samplers.SampleResult;
Public class Transdemoactions extends abstractjavasamplerclient{
sampleresultresult= null;
Public Class<?>trans = null;
Arguments params= null;
Method methodinit= null;
Method methodaction= null;
Method methodend= null;
Myclassloader classloader= null;
Object newtrans= null;
/**
* Custom Class Loading method
* @param Jarpath
* @param classpath
*/
Public void loadclass (string[] jarpath,stringclasspath) {
Url[] urls= new url[] {};
ClassLoader = new myclassloader (URLs,null);
Try {
for (String Jar:jarpath) {
Classloader.addjar (new File (Jar.trim ()). Touri (). Tourl ());
System. out. println ("Load jar file:" +jar.trim ());
}
trans = Classloader.loadclass (Classpath);
System. out. println ("Load class file:" +classpath);
Methodinit = Trans.getdeclaredmethod ("init");
Methodaction = Trans.getdeclaredmethod ("action");
Methodend = Trans.getdeclaredmethod ("End");
Boolean accessible = Methodinit.isaccessible ();
if (accessible = =false) {
Methodinit.setaccessible (true);
}
Newtrans = Trans.newinstance ();
} catch (Exceptione) {
E.printstacktrace ();
}
}
/**
* Custom JMeter external parameters
*/
Public Arguments Getdefaultparameters () {
Arguments params= New Arguments ();
Params.addargument ("Trans_jarpath", "C:/perftest-trans.jar");
Params.addargument ("Trans_classpath", "Perftest.jmeter.trans.TransDemo");
returnparams;
}
Public void setuptest (Javasamplercontext arg0) {
Try {
LoadClass (Arg0.getparameter ("Trans_jarpath"). Split (","), Arg0.getparameter ("Trans_classpath"));
Call Testdemo's init () method through reflection,
Methodinit.invoke (Newtrans);
}catch(Illegalaccessexception e) {
E.printstacktrace ();
}catch(IllegalArgumentException e) {
E.printstacktrace ();
}catch(InvocationTargetException e) {
E.printstacktrace ();
}
}
@Override
Public Sampleresult Runtest (JavaSamplerContextarg0) {
Try {
Methodaction.invoke (Newtrans);
}catch(Illegalaccessexception e) {
E.printstacktrace ();
}catch(IllegalArgumentException e) {
E.printstacktrace ();
}catch(InvocationTargetException e) {
E.printstacktrace ();
}
returnresult;
}
Public void teardowntest (Javasamplercontext arg0) {
Try {
Methodend.invoke (Newtrans);
}catch(Illegalaccessexception e) {
E.printstacktrace ();
}catch(IllegalArgumentException e) {
E.printstacktrace ();
}catch(InvocationTargetException e) {
E.printstacktrace ();
}catch (Exceptione) {
E.printstacktrace ();
}
}
/**
* Custom internal classes implement dynamic load class
* @author
*
*/
Static Class Myclassloader extends urlclassloader {
Public Myclassloader (url[]urls) {
Super (URLs);
}
Public Myclassloader (Url[]urls, ClassLoader parent) {
Super (urls,parent);
}
Public void addjar (url url) {
this. Addurl (URL);
}
}
}
Export it as Perftest-actons.jar and put it under <jmeterpath>/lib/ext, which can be automatically discovered when JMeter starts: Perftest.jemter.action.TestDemoActons.
Start JMeter, set up test plans and add thread groups and "Java requests"
The thread group size is set to 1, and the thread cycle number is set to 2: That is, a virtual user is iterating 2 times.
Run Tests
Run the test to see the output of the SYSTEM.OUT.PRINTLN:
Modify the action () method of the Transdemo of the business class under test
Public String Action () {
Print output characters are modified here
String str = "Actionsecond.";
System. out. println (str);
return str;
}
Re-package the Transdemo.class as Perftest-trans.jar and overwrite the previous C:/perftest-trans.jar
Re-run the test
If you perform the test again without shutting down and restarting JMeter, you can see that the output information has been changed to "action seconde." :
Summary:
So to implement the dynamic loading of a class, you must use URLClassLoader in the JMeter test class abstractjavasamplerclient (Java request) to implement the re-loading of the business class being tested and invoke the business method using the Invoke () method.
Since then, the Jmeter+h2database dynamic deployment jar package to the agent side and this article can be used to implement the real-time update of the tested business (JAR) package and the hot load of the Business test class (Class) for many agents in distributed testing.
No longer need to restart the 100+ station JMeter agent side and worry.
Unloading/dynamic loading of jar packages for Dubbo jmeter pressure measurement