1 22 package org.jboss.ejb3.test.dd.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.ejb3.test.dd.web.interfaces.ReferenceTest; 38 import org.jboss.ejb3.test.dd.web.interfaces.StatelessSession; 39 import org.jboss.ejb3.test.dd.web.interfaces.StatelessSessionLocal; 40 import org.jboss.ejb3.test.dd.web.util.Util; 41 import org.jboss.logging.Logger; 42 import org.jboss.security.SecurityAssociation; 43 import org.jboss.security.SimplePrincipal; 44 45 52 public class SpeedServlet extends HttpServlet 53 { 54 private static final Logger log = Logger.getLogger(SpeedServlet.class); 55 56 public static final int REPEATS = 10; 57 58 public static final int ITERATIONS = 100; 59 60 protected long[] runRemoteTest(StatelessSession bean, boolean optimized) 61 throws Exception 62 { 63 ReferenceTest test = new ReferenceTest(); 64 long[] results = new long[REPEATS]; 65 for (int loop = 0; loop < REPEATS; loop++) 66 { 67 long start = System.currentTimeMillis(); 68 for (int i = 0; i < ITERATIONS; i++) 69 bean.noop(test, optimized); 70 results[loop] = System.currentTimeMillis() - start; 71 } 72 return results; 73 } 74 75 protected void displayResults(PrintWriter out, long[] results) 76 throws IOException 77 { 78 long total = 0; 79 out.print("<table><tr>"); 80 for (int i = 0; i < results.length; i++) 81 { 82 total += results[i]; 83 84 out.print("<td>" + results[i] + " ms</td>"); 85 } 86 out.println("</tr></table><br />"); 87 out.println("Total time = " + total + " ms<br />"); 88 out.println("Invocations = " + ITERATIONS * REPEATS); 89 float average = total * 1000; 90 average /= (ITERATIONS * REPEATS); 91 out.println("Average time= " + average + " micro-seconds<br />"); 92 } 93 94 private void testNaming() 95 { 96 lookup(""); 97 lookup("jmx"); 98 lookup("java:comp"); 99 lookup("java:comp/env"); 100 lookup("java:comp/env/ejb"); 101 lookup("java:comp/env/ejb/local"); 102 lookup("containers"); 103 lookup("jbosstest"); 104 lookup("jbosstest/ejbs"); 105 lookup("local"); 106 lookup("dd"); 107 lookup("dd/web"); 108 } 109 110 private void lookup(String name) 111 { 112 try { 113 log.info("lookup " + name); 114 InitialContext jndiContext = new InitialContext (); 115 javax.naming.NamingEnumeration names = jndiContext.list(name); 116 if (names != null){ 117 while (names.hasMore()){ 118 log.info(" " + names.next()); 119 } 120 } 121 } catch (Exception e){ 122 } 123 } 124 125 protected void processRequest(HttpServletRequest request, 126 HttpServletResponse response) throws ServletException , IOException 127 { 128 testNaming(); 129 130 SecurityAssociation.setPrincipal(new SimplePrincipal("jduke")); 131 SecurityAssociation.setCredential("theduke".toCharArray()); 132 133 long[] optimized = null; 134 long[] notOptimized = null; 135 try 136 { 137 InitialContext ctx = new InitialContext (); 138 139 StatelessSession bean = (StatelessSession) ctx.lookup("OptimizedEJB"); 140 optimized = runRemoteTest(bean, true); 141 142 bean = (StatelessSession) ctx.lookup("dd/web/NotOptimizedEJB"); 143 144 notOptimized = runRemoteTest(bean, false); 145 } catch (Exception e) 146 { 147 throw new ServletException ("Failed to run speed tests", e); 148 } 149 response.setContentType("text/html"); 150 PrintWriter out = response.getWriter(); 151 out.println("<html>"); 152 out.println("<head><title>SpeedServlet</title></head>"); 153 out.println("<body>"); 154 out.println("Number of invocations=" + ITERATIONS + " repeated " 155 + REPEATS + " times.<br />"); 156 out.println("<h2>ejb/OptimizedEJB</h2>"); 157 displayResults(out, optimized); 158 out.println("<h2>ejb/NotOptimizedEJB</h2>"); 159 displayResults(out, notOptimized); 160 out.println("</body>"); 161 out.println("</html>"); 162 out.close(); 163 } 164 165 protected void doGet(HttpServletRequest request, HttpServletResponse response) 166 throws ServletException , IOException 167 { 168 processRequest(request, response); 169 } 170 171 protected void doPost(HttpServletRequest request, 172 HttpServletResponse response) throws ServletException , IOException 173 { 174 processRequest(request, response); 175 } 176 } 177 | Popular Tags |