001: package org.lateralnz.demo.view;
002:
003: import java.io.IOException;
004: import java.io.PrintWriter;
005: import java.util.HashMap;
006: import java.util.Locale;
007:
008: import javax.servlet.http.HttpServlet;
009: import javax.servlet.http.HttpServletRequest;
010: import javax.servlet.http.HttpServletResponse;
011: import javax.servlet.RequestDispatcher;
012: import javax.servlet.http.HttpSession;
013: import javax.servlet.ServletConfig;
014: import javax.servlet.ServletException;
015:
016: import org.apache.log4j.Logger;
017:
018: import org.lateralnz.demo.ejb.Basic;
019: import org.lateralnz.demo.ejb.BasicHome;
020: import org.lateralnz.common.util.IOUtils;
021: import org.lateralnz.common.util.JNDIUtils;
022: import org.lateralnz.common.util.NumericUtils;
023: import org.lateralnz.common.util.ServletUtils;
024: import org.lateralnz.common.util.StringUtils;
025:
026: public class PerfServlet extends HttpServlet {
027:
028: private int rawcount;
029: private long rawtime;
030:
031: protected void processRequest(HttpServletRequest request,
032: HttpServletResponse response) throws ServletException,
033: IOException {
034: PrintWriter out = null;
035: try {
036: out = response.getWriter();
037: out.println("<html><body>\n");
038:
039: int threads = 100;
040: int iterations = 100;
041:
042: String tmp = request.getParameter("threads");
043: if (!StringUtils.isEmpty(tmp)) {
044: threads = Integer.parseInt(tmp);
045: }
046:
047: tmp = request.getParameter("iterations");
048: if (!StringUtils.isEmpty(tmp)) {
049: iterations = Integer.parseInt(tmp);
050: }
051:
052: rawtest(out, threads, iterations);
053:
054: out.println("</body></html>\n");
055: } finally {
056: if (out != null) {
057: out.flush();
058: IOUtils.close(out);
059: }
060: }
061: }
062:
063: protected void doGet(HttpServletRequest request,
064: HttpServletResponse response) throws ServletException,
065: IOException {
066: processRequest(request, response);
067: }
068:
069: /** Handles the HTTP <code>POST</code> method.
070: * @param request servlet request
071: * @param response servlet response
072: */
073: protected void doPost(HttpServletRequest request,
074: HttpServletResponse response) throws ServletException,
075: IOException {
076: processRequest(request, response);
077: }
078:
079: public String getServletInfo() {
080: return "Short description";
081: }
082:
083: private final void rawtest(final PrintWriter out,
084: final int threads, final int iterations) {
085: out.println("rawtest<br />\n");
086: rawcount = 0;
087:
088: Thread[] t = new Thread[threads];
089: for (int i = 0; i < threads; i++) {
090: t[i] = new Thread() {
091: public void run() {
092: Basic ejb = null;
093: try {
094: BasicHome home = (BasicHome) JNDIUtils
095: .getEJBHome("DEMO/Basic",
096: BasicHome.class);
097: ejb = home.create();
098: for (int i = 0; i < iterations; i++) {
099: ejb.getRandom(10000);
100: }
101: } catch (Exception e) {
102: e.printStackTrace();
103: } finally {
104: JNDIUtils.remove(ejb);
105: }
106: rawtestend(out, threads, iterations);
107: }
108: };
109: }
110:
111: rawtime = System.currentTimeMillis();
112: for (int i = 0; i < threads; i++) {
113: t[i].start();
114: }
115:
116: while (rawcount < threads) {
117: try {
118: Thread.currentThread().sleep(1000);
119: } catch (Exception e) {
120: }
121: }
122: }
123:
124: private final void rawtestend(PrintWriter out, int threads,
125: int iterations) {
126: rawcount++;
127: if (rawcount >= threads) {
128: rawtime = System.currentTimeMillis() - rawtime;
129: out.println("rawtest :: " + threads + " threads, "
130: + iterations + " iterations took " + rawtime
131: + "ms<br />\n");
132: out
133: .println("average per call - "
134: + NumericUtils
135: .format(
136: this .getClass().getName(),
137: ((double) rawtime / ((double) threads * iterations)),
138: "######0.#######")
139: + "<br />\n");
140: }
141:
142: }
143: }
|