Using JMeter to Performance Test Web Services
Pages: 1, 2, 3, 4, 5

Other Features

As mentioned, JMeter has HTTP, FTP, and LDAP samplers. Creating these requests is a straightforward task and is well described in the JMeter manual; it usually involves creating a Thread Group, adding the sampler and the timer and the listener. We would like to mention some of the features that go beyond these standard samplers and that may need some non-standard configuration steps.

Scripting

Custom tests and scripting, particularly support for Java requests and BeanShell scripts, is another feature of JMeter that gives more flexibility to the load test developer. We can create a scripted test and compile it as a Java class and pass it to JMeter to run. To use the BeanShell scripting feature it, you need to download the BeanShell jar and place it under the /lib directory so JMeter will be able to pick it up at runtime. You can use the beanshell API from Java sampler, or create a BeanShell sampler that either reads the script from a file or processes the commands typed in a text box. The code sample below illustrates the BeanShell assertion added to an HTTP sampler. You can analyze and control the test execution based on the response from an HTTP request. A good scenario would be to create a test plan with a thread group and a loop starting the load test at a scheduled time. The request will hit the Web server and pick up its response:

print("HTTP return code is: " + ResponseCode);   
print("HTTP return message is: " + ResponseMessage);   
if (SampleResult.isSuccessful())
{
     print("Success");
     SampleResult.setStopTest(true);
     SampleResult.setStopThread(true);
}

The response can be logged with BeanShell assertions added to the test plan right after the HTTP sampler, and if the Web server response matches certain criteria, that is, if it is successful or it contains an expected string, the script will either stop the test or thread or continue execution.

JMS applications

To use JMeter to load test JMS applications, download the ActiveMQ jar and copy it into the /lib directory. As in the previous test first we create a thread group and add a JMS Point-to-Point sample. The JMS Point-to-Point dialog needs to be populated with the following parameters: QueueConnection Factory, JNDI Name Request queue, and JNDI Name Receive queue. For example we can use the parameters from an installed JMS module on the WebLogic examples server; in this case they are weblogic.examples.jms.QueueConnectionFactory and weblogic.examples.jms.exampleQueue. We can also create a custom JMS connection factory and queue. For WebLogic server we also need to add weblogic.jndi.WLInitialContextFactory as an Initial Context Factory value and the Provider URL, which normally looks like t3://hostname:7001, for example t3://localhost:7001.

Custom samplers

Many test developers sooner or later face the question: What if I have to create a test plan and JMeter does not provide necessary samplers or listeners. For example, you may need to test Enterprise JavaBeans applications. You can still use JMeter's Java Request, which essentially is a Java class to which we can add any logic we need. Let's create a simple Java sampler that will send the request to the application server and call the session bean. First, let us create a package mytest and a Java class called EJBTest.java:

package mytest;

import mybeans.LoginBeanRemoteHome;
import mybeans.LoginBeanRemote;

import java.io.Serializable;
import javax.naming.Context; 
import javax.naming.InitialContext; 
import javax.rmi.PortableRemoteObject; 
import java.util.Hashtable;
import javax.naming.*;
import javax.rmi.*;
import java.rmi.RemoteException;
import javax.ejb.CreateException;

import org.apache.jmeter.protocol.java.sampler.*;
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;
import org.apache.jmeter.testelement.TestElement;

public class EJBTest implements JavaSamplerClient , 
                    Serializable {

  public EJBTest() {
  }
  public void setupTest(JavaSamplerContext context) {
  }
  public Arguments getDefaultParameters() {
    Arguments params = new Arguments();
    return params;
  }
  public SampleResult runTest(JavaSamplerContext context) {
    SampleResult results = new SampleResult();
    try{                     
      Context env = getInitialContext("t3://localhost:7001");
      LoginBeanRemoteHome home = (LoginBeanRemoteHome)
      PortableRemoteObject.narrow(
      env.lookup("ejb.LoginBeanRemoteHome"),
        LoginBeanRemoteHome.class);
        LoginBeanRemote bean = (LoginBeanRemote) home.create();
        bean.login("TestUser","TestPassword");
        results.setSuccessful(true);
    }
    catch (NamingException ne){
      ne.printStackTrace();
    results.setSuccessful(false);
    }
    catch (RemoteException re){
      re.printStackTrace();
    results.setSuccessful(false);
    }
    catch (CreateException ce){
      ce.printStackTrace();
    results.setSuccessful(false);
    }
    return results;
  }
  static Context getInitialContext(String url) throws NamingException { 
    Hashtable env = new Hashtable<String,String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, 
      "weblogic.jndi.WLInitialContextFactory");
        env.put(Context.PROVIDER_URL, url);
    return new InitialContext(env);
  }
  public void teardownTest(JavaSamplerContext context) {
  }
}

To use this code, compile it to create a jar file myEJBTest.jar, copy the jar to the JMeter classpath (such as the lib directory), or add the classpath location to the jmeter.properties file - something like:

user.classpath=d:\jakarta-jmeter-2.1.1\lib\ext\myEJBTest.jar

Restart JMeter, add a Java Request element to a test plan, and choose mytest.EJBTest from the dropdown menu. By default JMeter provides a "SleepTest" and "JavaTest." Now we can add the thread group, loop, and listeners to our test plan and run it.

Pages: 1, 2, 3, 4, 5

Next Page ยป