KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > dd > web > servlets > SpeedServlet


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.test.dd.web.servlets;
23
24 import java.io.IOException JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import javax.naming.Context JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.naming.NamingException JavaDoc;
29 import javax.rmi.PortableRemoteObject JavaDoc;
30 import javax.servlet.ServletConfig JavaDoc;
31 import javax.servlet.ServletException JavaDoc;
32 import javax.servlet.http.HttpServlet JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35 import javax.sql.DataSource JavaDoc;
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 /**
46  * A servlet that accesses an EJB and tests the speed of optimized versus
47  * non-optimized invocations.
48  *
49  * @author Adrian.Brock@HappeningTimes.com
50  * @version $Revision: 37459 $
51  */

52 public class SpeedServlet extends HttpServlet JavaDoc
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 JavaDoc
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 JavaDoc out, long[] results)
76          throws IOException JavaDoc
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 JavaDoc name)
111    {
112       try {
113          log.info("lookup " + name);
114          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
115          javax.naming.NamingEnumeration JavaDoc names = jndiContext.list(name);
116          if (names != null){
117             while (names.hasMore()){
118                log.info(" " + names.next());
119             }
120          }
121       } catch (Exception JavaDoc e){
122       }
123    }
124
125    protected void processRequest(HttpServletRequest JavaDoc request,
126          HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc
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 JavaDoc ctx = new InitialContext JavaDoc();
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 JavaDoc e)
146       {
147          throw new ServletException JavaDoc("Failed to run speed tests", e);
148       }
149       response.setContentType("text/html");
150       PrintWriter JavaDoc 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 JavaDoc request, HttpServletResponse JavaDoc response)
166          throws ServletException JavaDoc, IOException JavaDoc
167    {
168       processRequest(request, response);
169    }
170
171    protected void doPost(HttpServletRequest JavaDoc request,
172          HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc
173    {
174       processRequest(request, response);
175    }
176 }
177
Popular Tags