JMeter: Performance Testing Server-side Java 
by Jeff Linwood

Listing One
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** A slow servlet for the JMeter test article
 * Uses String concatenation to get a slow servlet.
 * @author Jeff Linwood
 */

public class SlowJMeterExample extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
  {
                //Tell the browser we're sending it html
        response.setContentType("text/html");
                String myhtml = new String("");
                String addon = new String("<html>");
                myhtml = myhtml.concat(addon);
                addon = "<head>";
                myhtml = myhtml.concat(addon);
                addon = "<title>";
                myhtml = myhtml.concat(addon);
                addon = "Slow Example for JMeter";
                myhtml = myhtml.concat(addon);
                addon = "</title>";
                myhtml = myhtml.concat(addon);
                addon = "</head>";
                myhtml = myhtml.concat(addon);
                addon = "<body><h1>";
                myhtml = myhtml.concat(addon);
                myhtml = myhtml.concat("JMeter Slow Example Test");
                myhtml = myhtml.concat("</H1>");
                for (int ctr = 0; ctr < 512; ctr++) {
            myhtml = myhtml.concat(String.valueOf(ctr));
            myhtml = myhtml.concat(" ");
                }
        myhtml = myhtml.concat("</body>");
        myhtml = myhtml.concat("</html>");
        PrintWriter out = response.getWriter();
        out.println(myhtml);
    }
}


Listing Two
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
    "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
    <servlet>
      <servlet-name>SlowExample</servlet-name>
      <servlet-class>SlowJMeterExample</servlet-class>
    </servlet>

    <servlet>
      <servlet-name>FastExample</servlet-name>
      <servlet-class>FastJMeterExample</servlet-class>
    </servlet>
    
    <servlet-mapping>
      <servlet-name>SlowExample</servlet-name>
      <url-pattern>/SlowExample</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
      <servlet-name>FastExample</servlet-name>
      <url-pattern>/FastExample</url-pattern>
    </servlet-mapping>
</web-app>

Listing Three
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** A fast servlet for the JMeter test article
 * Uses StringBuffer appends to be faster than the Slow servlet.
 * @author Jeff Linwood
 */

public class FastJMeterExample extends HttpServlet {
   public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
                //Tell the browser we're sending it html
        response.setContentType("text/html");
                StringBuffer myhtml = new StringBuffer();
                String addon = new String("<html>");
                myhtml.append(addon);
                addon = "<head>";
                myhtml.append(addon);
                addon = "<title>";
                myhtml.append(addon);
                addon = "Fast Example for JMeter";
                myhtml.append(addon);
                addon = "</title>";
                myhtml.append(addon);
                addon = "</head>";
                myhtml.append(addon);
                addon = "<body><h1>";
                myhtml.append(addon);
                myhtml.append("JMeter Fast Example Test");
                myhtml.append("</H1>");
                for (int ctr = 0; ctr < 512; ctr++) {
            myhtml.append(ctr);
            myhtml.append(" ");
                }
        myhtml.append("</body>");
        myhtml.append("</html>");
        PrintWriter out = response.getWriter();
        out.println(myhtml.toString());
    }
}

Listing Four
# Sample data created by null
#       URL                    Milliseconds
http://localhost:80/jmeter/FastExample  0
http://localhost:80/jmeter/FastExample  10
http://localhost:80/jmeter/FastExample  0
http://localhost:80/jmeter/FastExample  10
http://localhost:80/jmeter/FastExample  0
http://localhost:80/jmeter/FastExample  0
http://localhost:80/jmeter/FastExample  10





1


