KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > 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.test.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.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 /** A servlet that accesses an EJB and tests the speed of optimized versus non-optimized invocations.
45
46 @author Adrian.Brock@HappeningTimes.com
47 @version $Revision: 37406 $
48 */

49 public class SpeedServlet extends HttpServlet JavaDoc
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 JavaDoc
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 JavaDoc out, long[] results)
70        throws IOException JavaDoc
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 JavaDoc request, HttpServletResponse JavaDoc response)
89         throws ServletException JavaDoc, IOException JavaDoc
90     {
91         long[] optimized = null;
92         long[] notOptimized = null;
93         try
94         {
95             InitialContext JavaDoc ctx = new InitialContext JavaDoc();
96             Context JavaDoc enc = (Context JavaDoc) 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 JavaDoc e)
106         {
107             throw new ServletException JavaDoc("Failed to run speed tests", e);
108         }
109         response.setContentType("text/html");
110         PrintWriter JavaDoc 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 JavaDoc request, HttpServletResponse JavaDoc response)
125         throws ServletException JavaDoc, IOException JavaDoc
126     {
127         processRequest(request, response);
128     }
129
130     protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
131         throws ServletException JavaDoc, IOException JavaDoc
132     {
133         processRequest(request, response);
134     }
135 }
136
Popular Tags