KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > servlet > performance


1 /*
2  * This software is OSI Certified Open Source Software.
3  * OSI Certified is a certification mark of the Open Source Initiative.
4  * The license (Mozilla version 1.0) can be read at the MMBase site.
5  * See http://www.MMBase.org/license
6  */

7 package org.mmbase.servlet;
8
9 import java.io.IOException JavaDoc;
10 import java.io.PrintStream JavaDoc;
11 import java.util.Date JavaDoc;
12 import javax.servlet.ServletException JavaDoc;
13 import javax.servlet.http.HttpServletRequest JavaDoc;
14 import javax.servlet.http.HttpServletResponse JavaDoc;
15
16 /**
17  * Performance Servlet is used as a basic Servlet to test whether the installation of succeeded.
18  * It also does a very basic test to measure how fast the JVM is.
19  *
20  * @rename Performance
21  * @author vpro
22  * @version $Id: performance.java,v 1.15 2005/05/14 14:04:44 nico Exp $
23  */

24 public class performance extends BridgeServlet {
25
26     public static final long INT_LOOP = 20000000;
27     public static final long STRING_LOOP = 2500000;
28     public static final long METHOD_LOOP = 10000000;
29     public static final String JavaDoc TEST_STRING = "test";
30
31     /**
32      * @javadoc
33      */

34     public void init() throws ServletException JavaDoc {
35         super.init();
36     }
37
38     /**
39      * Called by the server when a request is done.
40      * @javadoc
41      */

42     public synchronized void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws ServletException JavaDoc, IOException JavaDoc {
43         // Open a output stream so you can write to the client
44
PrintStream JavaDoc out = new PrintStream JavaDoc(res.getOutputStream());
45
46         // Set the content type of this request
47
res.setContentType("text/html");
48
49         // Write header to client
50
//res.writeHeaders();
51

52         // WRITE MESSAGE TO CLIENT
53
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
54         out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"DTD/xhtml1-transitional.dtd\" >");
55         out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">");
56         out.println("<head><title>Performance</title></head>");
57         out.println("<body>");
58         out.println("<h1>Performance Servlet</h1>");
59         out.println("<p>" + getServletInfo() + "</p>");
60         long time = intloop();
61         out.println("<p><strong>Performance Test (simple loop "+INT_LOOP+" times) </strong><br />");
62         out.println("takes "+time+"ms, "+(((double)time)/INT_LOOP)+" ms/loop </p>");
63         time = string();
64         out.println("<p><strong>Performance Test (compare '"+TEST_STRING+"' "+STRING_LOOP+" times) </strong><br />");
65         out.println("takes "+time+"ms, "+(((double)time)/STRING_LOOP)+" ms/loop </p>");
66
67         out.println("<p><strong>Performance Test (loop through the provided stub method "+METHOD_LOOP+" times) </strong><br />");
68         out.println("takes "+time+"ms, "+(((double)time)/METHOD_LOOP)+" ms/loop </p>");
69         out.println("</body>");
70         out.println("</html>");
71     }
72
73     /**
74      * @javadoc
75      */

76     private long intloop() {
77         long begin = new Date JavaDoc().getTime();
78         for (int i = 0; i < 20000000; i++) {
79             ;
80         }
81         long end = new Date JavaDoc().getTime();
82         return end - begin;
83     }
84
85     /**
86      * @javadoc
87      */

88     private long string() {
89         long begin = new Date JavaDoc().getTime();
90         String JavaDoc test = TEST_STRING;
91         for (int i = 0; i < 2500000; i++) {
92             test.equals(TEST_STRING);
93         }
94         long end = new Date JavaDoc().getTime();
95         return end - begin;
96     }
97
98     /**
99      * @javadoc
100      */

101     private long method() {
102         long begin = new Date JavaDoc().getTime();
103         for (int i = 0; i < METHOD_LOOP; i++) {
104             stub();
105         }
106         long end = new Date JavaDoc().getTime();
107         return end - begin;
108     }
109
110     /**
111      * @javadoc
112      */

113     protected void stub() {
114     }
115
116     /**
117      * Info method, provides the user/server with some basic info on this Servlet
118      * @return a descriptive text
119      */

120     public String JavaDoc getServletInfo() {
121         return ("Performance tester, for JIT and other things");
122     }
123 }
124
125
Popular Tags