1 22 package org.jboss.test.web.servlets; 23 24 import java.io.IOException ; 25 import java.io.PrintWriter ; 26 import javax.naming.Context ; 27 import javax.naming.InitialContext ; 28 import javax.naming.NamingException ; 29 import javax.rmi.PortableRemoteObject ; 30 import javax.servlet.ServletConfig ; 31 import javax.servlet.ServletException ; 32 import javax.servlet.http.HttpServlet ; 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 import javax.sql.DataSource ; 36 37 import org.jboss.test.web.interfaces.ReferenceTest; 38 import org.jboss.test.web.interfaces.StatelessSession; 39 import org.jboss.test.web.interfaces.StatelessSessionHome; 40 import org.jboss.test.web.interfaces.StatelessSessionLocal; 41 import org.jboss.test.web.interfaces.StatelessSessionLocalHome; 42 import org.jboss.test.web.util.Util; 43 44 49 public class SpeedServlet extends HttpServlet 50 { 51 public static final int REPEATS = 10; 52 public static final int ITERATIONS = 100; 53 54 protected long[] runRemoteTest(StatelessSession bean, boolean optimized) 55 throws Exception 56 { 57 ReferenceTest test = new ReferenceTest(); 58 long[] results = new long[REPEATS]; 59 for (int loop = 0; loop < REPEATS; loop ++) 60 { 61 long start = System.currentTimeMillis(); 62 for (int i = 0; i < ITERATIONS; i++) 63 bean.noop(test, optimized); 64 results[loop] = System.currentTimeMillis() - start; 65 } 66 return results; 67 } 68 69 protected void displayResults(PrintWriter out, long[] results) 70 throws IOException 71 { 72 long total = 0; 73 out.print("<table><tr>"); 74 for (int i = 0; i < results.length; i++) 75 { 76 total += results[i]; 77 78 out.print("<td>" + results[i] + " ms</td>"); 79 } 80 out.println("</tr></table><br />"); 81 out.println("Total time = " + total + " ms<br />"); 82 out.println("Invocations = " + ITERATIONS * REPEATS); 83 float average = total * 1000; 84 average /= (ITERATIONS * REPEATS); 85 out.println("Average time= " + average + " micro-seconds<br />"); 86 } 87 88 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 89 throws ServletException , IOException 90 { 91 long[] optimized = null; 92 long[] notOptimized = null; 93 try 94 { 95 InitialContext ctx = new InitialContext (); 96 Context enc = (Context ) ctx.lookup("java:comp/env"); 97 StatelessSessionHome home = (StatelessSessionHome) enc.lookup("ejb/OptimizedEJB"); 98 StatelessSession bean = home.create(); 99 optimized = runRemoteTest(bean, true); 100 101 home = (StatelessSessionHome) enc.lookup("ejb/NotOptimizedEJB"); 102 bean = home.create(); 103 notOptimized = runRemoteTest(bean, false); 104 } 105 catch(Exception e) 106 { 107 throw new ServletException ("Failed to run speed tests", e); 108 } 109 response.setContentType("text/html"); 110 PrintWriter out = response.getWriter(); 111 out.println("<html>"); 112 out.println("<head><title>SpeedServlet</title></head>"); 113 out.println("<body>"); 114 out.println("Number of invocations=" + ITERATIONS + " repeated " + REPEATS + " times.<br />"); 115 out.println("<h2>ejb/OptimizedEJB</h2>"); 116 displayResults(out, optimized); 117 out.println("<h2>ejb/NotOptimizedEJB</h2>"); 118 displayResults(out, notOptimized); 119 out.println("</body>"); 120 out.println("</html>"); 121 out.close(); 122 } 123 124 protected void doGet(HttpServletRequest request, HttpServletResponse response) 125 throws ServletException , IOException 126 { 127 processRequest(request, response); 128 } 129 130 protected void doPost(HttpServletRequest request, HttpServletResponse response) 131 throws ServletException , IOException 132 { 133 processRequest(request, response); 134 } 135 } 136 | Popular Tags |