1 7 package org.mmbase.servlet; 8 9 import java.io.IOException ; 10 import java.io.PrintStream ; 11 import java.util.Date ; 12 import javax.servlet.ServletException ; 13 import javax.servlet.http.HttpServletRequest ; 14 import javax.servlet.http.HttpServletResponse ; 15 16 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 TEST_STRING = "test"; 30 31 34 public void init() throws ServletException { 35 super.init(); 36 } 37 38 42 public synchronized void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException , IOException { 43 PrintStream out = new PrintStream (res.getOutputStream()); 45 46 res.setContentType("text/html"); 48 49 52 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 76 private long intloop() { 77 long begin = new Date ().getTime(); 78 for (int i = 0; i < 20000000; i++) { 79 ; 80 } 81 long end = new Date ().getTime(); 82 return end - begin; 83 } 84 85 88 private long string() { 89 long begin = new Date ().getTime(); 90 String test = TEST_STRING; 91 for (int i = 0; i < 2500000; i++) { 92 test.equals(TEST_STRING); 93 } 94 long end = new Date ().getTime(); 95 return end - begin; 96 } 97 98 101 private long method() { 102 long begin = new Date ().getTime(); 103 for (int i = 0; i < METHOD_LOOP; i++) { 104 stub(); 105 } 106 long end = new Date ().getTime(); 107 return end - begin; 108 } 109 110 113 protected void stub() { 114 } 115 116 120 public String getServletInfo() { 121 return ("Performance tester, for JIT and other things"); 122 } 123 } 124 125 | Popular Tags |