1 4 package com.tctest.performance.http.load; 5 6 import org.apache.commons.httpclient.HostConfiguration; 7 import org.apache.commons.httpclient.HttpClient; 8 import org.apache.commons.httpclient.HttpConnectionManager; 9 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; 10 import org.apache.commons.httpclient.params.HttpConnectionManagerParams; 11 import org.apache.commons.lang.SerializationUtils; 12 13 import EDU.oswego.cs.dl.util.concurrent.PooledExecutor; 14 import EDU.oswego.cs.dl.util.concurrent.ThreadFactory; 15 16 import java.io.BufferedOutputStream ; 17 import java.io.File ; 18 import java.io.FileOutputStream ; 19 import java.net.InetAddress ; 20 import java.text.DateFormat ; 21 import java.util.Date ; 22 import java.util.concurrent.LinkedBlockingQueue ; 23 import java.util.zip.GZIPOutputStream ; 24 25 public abstract class AbstractHttpLoadTest { 26 27 private static final String REPORT = "report"; 28 private static final String RESULTS_DIR = "results"; 29 30 private final LinkedBlockingQueue workQ; 31 private final HttpLoadClient loadClient; 32 private final int duration; 33 private final String workingDir; 34 private final boolean printReport; 35 private final RequestCounter counter; 36 protected final TestProperties testProperties; 37 38 protected AbstractHttpLoadTest(String [] args) { 39 validateArgs(args); 40 this.printReport = (args.length == 3 && args[2].equals(REPORT)); 41 this.workQ = new LinkedBlockingQueue (100); 42 this.duration = Integer.parseInt(args[0]); 43 this.workingDir = args[1]; 44 this.testProperties = new TestProperties(this.workingDir); 45 this.counter = new RequestCounter(); 46 this.loadClient = (printReport || duration == 0) ? null : makeInstance(); 47 } 48 49 public HttpLoadClient makeInstance() { 50 HttpConnectionManager connMgr = new MultiThreadedHttpConnectionManager(); 51 HttpConnectionManagerParams params = connMgr.getParams(); 52 params.setMaxTotalConnections(testProperties.getHosts().length * testProperties.getThreadCount()); 53 params.setConnectionTimeout(30 * 10000); 54 params.setTcpNoDelay(true); 55 String [] hosts = testProperties.getHosts(); 56 for (int i = 0; i < hosts.length; i++) { 57 String host = hosts[i]; 58 HostConfiguration hostConfig = new HostConfiguration(); 59 String [] parts = host.split(":"); 60 hostConfig.setHost(parts[0], Integer.valueOf(parts[1]).intValue(), "http"); 61 params.setMaxConnectionsPerHost(hostConfig, testProperties.getThreadCount()); 62 } 63 connMgr.setParams(params); 64 65 PooledExecutor requestExecutor = new PooledExecutor(testProperties.getThreadCount()); 66 requestExecutor.setThreadFactory(new LoadTestThreadFactory(connMgr)); 67 requestExecutor.setKeepAliveTime(1000); 68 requestExecutor.waitWhenBlocked(); 69 return new HttpLoadClient(workQ, requestExecutor, testProperties.getHosts(), testProperties.getSessionsCount(), 70 testProperties.getStickyRatio(), counter); 71 } 72 73 protected abstract WorkItem[] generateWarmUpWorkItems(); 74 75 protected abstract WorkItem generateWorkItem(long endtime); 76 77 protected abstract WorkItem[] generateFinishWorkItems(); 78 79 protected void warmUp() throws InterruptedException { 80 WorkItem[] items = generateWarmUpWorkItems(); 81 System.out.println("WARMING UP " + items.length + " SESSIONS"); 82 addWorkItems(workQ, items); 83 counter.waitForCount(items.length); 84 } 85 86 protected void execute() throws Exception { 87 if (printReport) { 88 HttpResponseAnalysisReport.printReport(resultsDir(), getClass().getSimpleName(), duration); 89 return; 90 } 91 Thread worker = new Thread () { 92 public void run() { 93 try { 94 warmUp(); 95 DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM); 96 String time = df.format(new Date (System.currentTimeMillis())); 97 System.out.println("BEGINNING TIMED LOAD: " + time); 98 long endTime = System.currentTimeMillis() + (duration * 1000); 99 while (endTime > System.currentTimeMillis()) { 100 WorkItem item = generateWorkItem(endTime); 101 workQ.put(item); 102 } 103 System.out.println(df.format(new Date (System.currentTimeMillis()))); 104 finished(); 105 } catch (InterruptedException e) { 106 e.printStackTrace(); 107 System.exit(0); 108 } 109 } 110 }; 111 worker.setPriority(Thread.MAX_PRIORITY); 112 worker.start(); 113 114 final String host = InetAddress.getLocalHost().getHostName(); 115 final StatsCollector collector = loadClient.getCollector(); 116 resultsDir().mkdir(); 117 118 Thread statsWriter = new Thread () { 119 private int count = 0; 120 121 public void run() { 122 try { 123 run0(); 124 } catch (Exception e) { 125 e.printStackTrace(); 126 } 127 } 128 129 private void run0() throws Exception { 130 File outFile = new File (resultsDir(), HttpResponseAnalysisReport.RESULTS_FILE_PREFIX + "." + host); 131 FileOutputStream fout = new FileOutputStream (outFile); 132 GZIPOutputStream gzOut = new GZIPOutputStream (new BufferedOutputStream (fout)); 133 134 while (true) { 135 ResponseStatistic stat = collector.takeStat(); 136 137 if (stat == null) { 138 break; 139 } 140 141 gzOut.write(SerializationUtils.serialize(stat)); 142 count++; 143 } 144 145 gzOut.close(); 146 147 boolean renamed = outFile.renameTo(new File (outFile.getParent(), outFile.getName() + "." + count + ".gz")); 148 if (!renamed) { throw new RuntimeException ("rename failed: " + outFile + ", count " + count); } 149 } 150 }; 151 statsWriter.start(); 152 153 loadClient.execute(); 154 155 statsWriter.join(); 156 worker.join(); 157 } 158 159 protected void finished() throws InterruptedException { 160 System.out.println("COOLING DOWN"); 161 final WorkItem[] items = generateFinishWorkItems(); 162 addWorkItems(workQ, items); 163 counter.waitForCount(items.length); workQ.put(new StopWorkItem()); 166 } 167 168 protected void validateArgs(String [] args) { 169 if (args.length < 2) { 170 System.out.println("Usage:"); 171 System.out.println(" <duration in seconds> <working dir path> [report]"); 172 System.exit(0); 173 } 174 } 175 176 protected final File resultsDir() { 177 return new File (workingDir + File.separator + RESULTS_DIR); 178 } 179 180 protected static void addWorkItems(LinkedBlockingQueue q, WorkItem[] items) throws InterruptedException { 181 for (int i = 0; i < items.length; i++) { 182 q.put(items[i]); 183 } 184 } 185 186 private static class LoadTestThreadFactory implements ThreadFactory { 187 188 private final HttpConnectionManager connMgr; 189 190 public LoadTestThreadFactory(HttpConnectionManager connMgr) { 191 this.connMgr = connMgr; 192 } 193 194 public Thread newThread(Runnable run) { 195 return new LoadTestThread(run, connMgr); 196 } 197 198 } 199 200 static class LoadTestThread extends Thread { 201 202 private final HttpClient httpClient; 203 204 LoadTestThread(Runnable run, HttpConnectionManager connMgr) { 205 super(run); 206 httpClient = new HttpClient(connMgr); 207 } 208 209 HttpClient getHttpClient() { 210 return httpClient; 211 } 212 213 } 214 215 } 216 | Popular Tags |