1 22 package org.jboss.test.bench.servlet; 23 24 import java.net.URL ; 25 import java.util.ArrayList ; 26 27 import javax.servlet.http.HttpServletRequest ; 28 29 public class FullTester { 30 org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(getClass()); 31 int maxClients; 32 33 HttpServletRequest req; 34 35 public static final int nbClients[] = { 1, 10, 50, 100, 200, 500 }; 37 public int depth; 38 public int nbTests = 0; 39 int nbCalls; 40 41 ArrayList testNames = new ArrayList (); 42 ArrayList testResults = new ArrayList (); 43 44 public FullTester(HttpServletRequest req) { 45 46 maxClients = Integer.parseInt(req.getParameter("maxClients")); 47 nbCalls = Integer.parseInt(req.getParameter("nbCalls")); 48 49 this.req = req; 50 51 depth = nbClients.length; 52 for (int i = 0; i< nbClients.length; i++) if (nbClients[i] > maxClients) { 53 depth = i; 54 break; 55 } 56 } 57 58 public String getTestName(int i) { 59 return (String )testNames.get(i); 60 } 61 62 public float getTestResult(int i, int j) { 63 return ((float[])testResults.get(i))[j]; 64 } 65 66 public void test() { 67 try { 68 if (req.getParameter("servlet") != null) { 69 float[] result = testURL("http://localhost:8080/bench/servlet/SimpleServlet?dest=none"); 70 testNames.add("Servlet alone"); 71 testResults.add(result); 72 nbTests++; 73 74 } 75 if (req.getParameter("servlet2SL") != null) { 76 float[] result = testURL("http://localhost:8080/bench/servlet/SimpleServlet?dest=SL"); 77 testNames.add("Servlet calling stateless session"); 78 testResults.add(result); 79 nbTests++; 80 81 } 82 if (req.getParameter("servlet2Entity") != null) { 83 float[] result = testURL("http://localhost:8080/bench/servlet/SimpleServlet?dest=Entity"); 84 testNames.add("Servlet calling entity"); 85 testResults.add(result); 86 nbTests++; 87 } 88 } catch (Exception e) { 89 log.debug("failed", e); 90 } 91 } 92 93 public float[] testURL(String url) throws Exception { 94 95 Thread [] threads = new Thread [maxClients]; 96 float[] result = new float[depth]; 97 98 99 class Worker extends Thread { 100 String url; 101 int loops; 102 103 public Worker(int loops, String url) { 104 this.loops = loops; 105 this.url = url; 106 } 107 108 public void run() { 109 for (int i=0; i<loops; i++) { 110 try { 111 URL theUrl = new URL (url); 112 Object obj = theUrl.getContent(); 113 114 } catch (Exception e) { 115 } 116 } 117 } 118 } 119 120 for (int i = 0; i < depth; i++) { 121 122 log.debug("Calling url " + url + " with " + nbClients[i] + " clients"); 123 124 int loops = nbCalls / nbClients[i]; 125 126 for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) { 127 Worker worker = new Worker(loops, url); 128 threads[threadNumber] = worker; 129 } 130 131 long start = System.currentTimeMillis(); 132 133 for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) { 134 threads[threadNumber].start(); 135 } 136 137 for (int threadNumber = 0; threadNumber < nbClients[i]; threadNumber++) { 138 try { 139 threads[threadNumber].join(); 140 } catch (InterruptedException e) { 141 } 143 } 144 145 long stop = System.currentTimeMillis(); 146 147 result[i] = ((float)(stop-start)) / (loops * nbClients[i]); 148 149 } 150 151 return result; 152 } 153 154 } 155 | Popular Tags |