Jul 4, 2007

Coding/Debuging Hessian Service with JMeter in Eclipse

1. What is needed,

* Eclipse SDK
* JMeter
* Java Hessian library
* The testing service's interface(s) and related classes (such as exceptions, DTOs etc)

2. Write the Java Request Sample driver code

* Create a class that extends org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient

There are 4 methods that need to override:

1) getDefaultParameters()

This method is where you set parameters that you would like to get in JMeter Java Reqeust GUI. You do this by instantiating a org.apache.jmeter.config.Arguments and calling its addArgument method for each parameter. For example:

public Arguments getDefaultParameters() {
Arguments args = new Arguments();
args.addArgument("Service URL", "");
args.addArgument("User Name", "");
args.addArgument("Password", "");
return args;
}

2) setupTest()

Where you do any initialization (only once per thread) such as reading in the parameters, creating the Hessian proxy, etc. For example:

public void setupTest(JavaSamplerContext context) {
url = context.getParameter("Service URL");
try {
mySvc = (mySvcClassName)
proxyFactory.create(mySvcClassName.class, url);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Error getting hessian proxy", e);
}
}

3) runTest()

JMeter will invoke it for each iteration of the test. This is where you would put code that invokes the service's methods and evaluate its result. The method returns a "org.apache.jmeter.samplers.SampleResult" object which is what you use to communicate the results to JMeter. For example:

public SampleResult runTest(JavaSamplerContext context) {
SampleResult result = new SampleResult();
result.setSampleLabel("Call Hessian SVC");
result.setDataType(SampleResult.TEXT);
result.sampleStart();
try {
String custId = mySvc.authenticate(context.getParameter("User Name"), context.getParameter("Password"));
if (custId == null) {
result.setSuccessful(false);
result.setResponseMessage("user authentication failed");
result.setResponseData("Returned customer id is null".getBytes());
}
else {
result.setSuccessful(true);
result.setResponseCodeOK();
result.setResponseMessage("Authentication successful");
result.setResponseData(("customer id: " + custId).getBytes());
}
}
catch (Throwable e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
result.setSuccessful(false);
result.setResponseMessage("Unexpected exception");
result.setResponseData(sw.toString().getBytes());
}

result.sampleEnd();
return result;
}

In the example above, the method setSuccessful indicates if the test passed or failed. The setting of the responseData is used for displaying whatever information you want in the response data tab of JMeter's "View Results Tree" listener.

4) teardownTest()

Where you do cleanup. Typically there is nothing you need to do here.

3. Create/Copy needed jars to JMeter lib

Create ("Export | Java | Jar file") a jar file that includes the class you wrote above and together with jar(s) of the interface (and related classes) of the service you want to test. Put these jars file in your JMeter's lib/ext folder.
And copy the Hessian jar file and any other helper jar files (eg. database jars) you need under JMeter's lib or lib/ext folder.

4. Debugging Java Request Sample driver code in Eclipse

1) Add external JMeter jar ApacheJMeter_core.jar, ApacheJMeter_java.jar under jmeter_home/lib/ext/ and hessian-3.0.17.jar to current project build path; And add other jars as needed;

2) Browse into ApacheJMeter_core.jar: "org.apache.jmeter | NewDriver.class", right-click and select "Debug As | Debug...", on right panel,

a) In tab "Arguments" change "Working directory" from default to jmeter_home/bin/;
b) In tab "Classpath | User Entries", remove default classpath, and add "Add External JARs..." jmeter_home/bin/ApacheJMeter.jar;
c) "Apply" to save the setting

3) Click Debug/Run on Eclipse toolbar to debug/run JMeter from Eclipse

4) When "Source not found", click "Edit Source Lookup Path... | Add... | Java Project", select the project which contain your code

Additionally: Want to see JMeter code during debug, just download the source from http://apache.osuosl.org/jakarta/jmeter/source/, unzip it and add source of the jmeter source directory and check true of "Search subfolders" :-)

5. Run in JMeter without Eclipse

* Copy the service api and test JARs from above to JMeter's lib or lib/ext
* Launch JMeter
* Add a "Thread Group" under "Test Plan"
* "Add >> Sampler >> Java Request" to the Thread Group
* On the right-hand Java Request panel, select your class in the drop-down java classname list
* In "Send Parameters with Request", supply the appropriate values for the parameters you've specified in the "getDefaultParameters" method of your class
* "Add >> Listener >> Summary Report" to the Thread Group
* "Add >> Listener >> View Results Tree" to the Thread Group
* "Run >> Start".
* Select the Summary Report node in the test plan to see statistics on the run.
* Select the View Results Tree node to see the result of each execution. This is where you'll see any data you've set in the responseData within your runTest method. In above example: customer id.

6 comments:

Chandru said...

Thanks a lot. I got clear idea how to implement Java Request In JMeter. I have spent around 4 hours for exploring this info in net. Finally I got it from ur site and it's very clear, once again Thanks a lot for your useful information.

Sergius said...

Hi Henry,

thanks a lot for great your post. It was very helpful for me.

Henry Wu said...

Glad it's still helpful even after 5 years of posting :D

harish kadamudi said...

HELLO,I've written a Spring web application as a hessian server.I've a simple java app as a client.how to configure hessian client in Jmeter.

harish kadamudi said...

I didn't get this point..
2) Browse into ApacheJMeter_core.jar: "org.apache.jmeter | NewDriver.class", right-click and select "Debug As | Debug...", on right panel,

wj said...

Hi Henry,
I didn't get this point..TOO
2) Browse into ApacheJMeter_core.jar: "org.apache.jmeter | NewDriver.class", right-click and select "Debug As | Debug...", on right panel,

There's no NewDriver.class under org.apache.jmeter, I use jmeter 9