I’m making some load test using Siege and Apache benchmarking tool. I count the number of calls for the Servlet and for the method runAlgo inside processRequest.
I use this code :
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "Servlet", urlPatterns = {"/test"})
public class Servlet extends HttpServlet {
static int count=0;
static int count2=0;
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("Process request : "+count2++);
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
// TODO output your page here
out.println("<html>");
out.println("<head>");
out.println("<title>Result</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Result : " + runAlgo() + "</h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
private ArrayList<String> runAlgo() {
System.out.println("algo : "+count++);
//the algo code
}
}
I have 2 problems :
First, the method runAlgo() is called more often than processRequest, it seems strange because runAlgo is only called here!
Then the other problem concern the benchmark, if I send for example 200 requests I have never in the log a count of 200, but the tools said that all transactions had been successful (server responded with a return code < 400)
Have you some explanations for this comportment?
I use apache-tomcat-7.0.11 and jdk1.7.0_02
You have to use volatile keyword because count and count2 are used by a lot of threads