001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.web.servlets;
023:
024: import java.io.IOException;
025: import java.io.PrintWriter;
026: import javax.naming.Context;
027: import javax.naming.InitialContext;
028: import javax.naming.NamingException;
029: import javax.rmi.PortableRemoteObject;
030: import javax.servlet.ServletConfig;
031: import javax.servlet.ServletException;
032: import javax.servlet.http.HttpServlet;
033: import javax.servlet.http.HttpServletRequest;
034: import javax.servlet.http.HttpServletResponse;
035: import javax.sql.DataSource;
036:
037: import org.jboss.test.web.interfaces.ReferenceTest;
038: import org.jboss.test.web.interfaces.StatelessSession;
039: import org.jboss.test.web.interfaces.StatelessSessionHome;
040: import org.jboss.test.web.interfaces.StatelessSessionLocal;
041: import org.jboss.test.web.interfaces.StatelessSessionLocalHome;
042: import org.jboss.test.web.util.Util;
043:
044: /** A servlet that accesses an EJB and tests the speed of optimized versus non-optimized invocations.
045:
046: @author Adrian.Brock@HappeningTimes.com
047: @version $Revision: 57211 $
048: */
049: public class SpeedServlet extends HttpServlet {
050: public static final int REPEATS = 10;
051: public static final int ITERATIONS = 100;
052:
053: protected long[] runRemoteTest(StatelessSession bean,
054: boolean optimized) throws Exception {
055: ReferenceTest test = new ReferenceTest();
056: long[] results = new long[REPEATS];
057: for (int loop = 0; loop < REPEATS; loop++) {
058: long start = System.currentTimeMillis();
059: for (int i = 0; i < ITERATIONS; i++)
060: bean.noop(test, optimized);
061: results[loop] = System.currentTimeMillis() - start;
062: }
063: return results;
064: }
065:
066: protected void displayResults(PrintWriter out, long[] results)
067: throws IOException {
068: long total = 0;
069: out.print("<table><tr>");
070: for (int i = 0; i < results.length; i++) {
071: total += results[i];
072:
073: out.print("<td>" + results[i] + " ms</td>");
074: }
075: out.println("</tr></table><br />");
076: out.println("Total time = " + total + " ms<br />");
077: out.println("Invocations = " + ITERATIONS * REPEATS);
078: float average = total * 1000;
079: average /= (ITERATIONS * REPEATS);
080: out
081: .println("Average time= " + average
082: + " micro-seconds<br />");
083: }
084:
085: protected void processRequest(HttpServletRequest request,
086: HttpServletResponse response) throws ServletException,
087: IOException {
088: long[] optimized = null;
089: long[] notOptimized = null;
090: try {
091: InitialContext ctx = new InitialContext();
092: Context enc = (Context) ctx.lookup("java:comp/env");
093: StatelessSessionHome home = (StatelessSessionHome) enc
094: .lookup("ejb/OptimizedEJB");
095: StatelessSession bean = home.create();
096: optimized = runRemoteTest(bean, true);
097:
098: home = (StatelessSessionHome) enc
099: .lookup("ejb/NotOptimizedEJB");
100: bean = home.create();
101: notOptimized = runRemoteTest(bean, false);
102: } catch (Exception e) {
103: throw new ServletException("Failed to run speed tests", e);
104: }
105: response.setContentType("text/html");
106: PrintWriter out = response.getWriter();
107: out.println("<html>");
108: out.println("<head><title>SpeedServlet</title></head>");
109: out.println("<body>");
110: out.println("Number of invocations=" + ITERATIONS
111: + " repeated " + REPEATS + " times.<br />");
112: out.println("<h2>ejb/OptimizedEJB</h2>");
113: displayResults(out, optimized);
114: out.println("<h2>ejb/NotOptimizedEJB</h2>");
115: displayResults(out, notOptimized);
116: out.println("</body>");
117: out.println("</html>");
118: out.close();
119: }
120:
121: protected void doGet(HttpServletRequest request,
122: HttpServletResponse response) throws ServletException,
123: IOException {
124: processRequest(request, response);
125: }
126:
127: protected void doPost(HttpServletRequest request,
128: HttpServletResponse response) throws ServletException,
129: IOException {
130: processRequest(request, response);
131: }
132: }
|