001: /*
002: * This software is OSI Certified Open Source Software.
003: * OSI Certified is a certification mark of the Open Source Initiative.
004: * The license (Mozilla version 1.0) can be read at the MMBase site.
005: * See http://www.MMBase.org/license
006: */
007: package org.mmbase.servlet;
008:
009: import java.io.IOException;
010: import java.io.PrintStream;
011: import java.util.Date;
012: import javax.servlet.ServletException;
013: import javax.servlet.http.HttpServletRequest;
014: import javax.servlet.http.HttpServletResponse;
015:
016: /**
017: * Performance Servlet is used as a basic Servlet to test whether the installation of succeeded.
018: * It also does a very basic test to measure how fast the JVM is.
019: *
020: * @author vpro
021: * @version $Id: Performance.java,v 1.2 2007/02/10 16:22:37 nklasens Exp $
022: */
023: public class Performance extends BridgeServlet {
024:
025: public static final long INT_LOOP = 20000000;
026: public static final long STRING_LOOP = 2500000;
027: public static final long METHOD_LOOP = 10000000;
028: public static final String TEST_STRING = "test";
029:
030: /**
031: * @javadoc
032: */
033: public void init() throws ServletException {
034: super .init();
035: }
036:
037: /**
038: * Called by the server when a request is done.
039: * @javadoc
040: */
041: public synchronized void doGet(HttpServletRequest req,
042: HttpServletResponse res) throws ServletException,
043: IOException {
044: // Open a output stream so you can write to the client
045: PrintStream out = new PrintStream(res.getOutputStream());
046:
047: // Set the content type of this request
048: res.setContentType("text/html");
049:
050: // Write header to client
051: //res.writeHeaders();
052:
053: // WRITE MESSAGE TO CLIENT
054: out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
055: out
056: .println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"DTD/xhtml1-transitional.dtd\" >");
057: out
058: .println("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">");
059: out.println("<head><title>Performance</title></head>");
060: out.println("<body>");
061: out.println("<h1>Performance Servlet</h1>");
062: out.println("<p>" + getServletInfo() + "</p>");
063: long time = intloop();
064: out.println("<p><strong>Performance Test (simple loop "
065: + INT_LOOP + " times) </strong><br />");
066: out.println("takes " + time + "ms, "
067: + (((double) time) / INT_LOOP) + " ms/loop </p>");
068: time = string();
069: out.println("<p><strong>Performance Test (compare '"
070: + TEST_STRING + "' " + STRING_LOOP
071: + " times) </strong><br />");
072: out.println("takes " + time + "ms, "
073: + (((double) time) / STRING_LOOP) + " ms/loop </p>");
074:
075: out
076: .println("<p><strong>Performance Test (loop through the provided stub method "
077: + METHOD_LOOP + " times) </strong><br />");
078: out.println("takes " + time + "ms, "
079: + (((double) time) / METHOD_LOOP) + " ms/loop </p>");
080: out.println("</body>");
081: out.println("</html>");
082: }
083:
084: /**
085: * @javadoc
086: */
087: private long intloop() {
088: long begin = new Date().getTime();
089: for (int i = 0; i < 20000000; i++) {
090: ;
091: }
092: long end = new Date().getTime();
093: return end - begin;
094: }
095:
096: /**
097: * @javadoc
098: */
099: private long string() {
100: long begin = new Date().getTime();
101: String test = TEST_STRING;
102: for (int i = 0; i < 2500000; i++) {
103: test.equals(TEST_STRING);
104: }
105: long end = new Date().getTime();
106: return end - begin;
107: }
108:
109: /**
110: * @javadoc
111: */
112: protected void stub() {
113: }
114:
115: /**
116: * Info method, provides the user/server with some basic info on this Servlet
117: * @return a descriptive text
118: */
119: public String getServletInfo() {
120: return ("Performance tester, for JIT and other things");
121: }
122: }
|