1 5 package com.tc.test.server.appserver.load; 6 7 import org.apache.commons.httpclient.Cookie; 8 import org.apache.commons.httpclient.HttpClient; 9 import org.apache.commons.httpclient.HttpState; 10 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; 11 12 import EDU.oswego.cs.dl.util.concurrent.SynchronizedRef; 13 14 import com.tc.logging.TCLogger; 15 import com.tc.logging.TCLogging; 16 import com.tc.test.server.util.HttpUtil; 17 import com.tc.util.concurrent.ThreadUtil; 18 19 import java.io.IOException ; 20 import java.net.ConnectException ; 21 import java.net.URL ; 22 import java.util.ArrayList ; 23 import java.util.List ; 24 import java.util.Random ; 25 26 import junit.framework.Assert; 27 28 public class Node implements Runnable { 29 protected static final TCLogger logger = TCLogging.getLogger(Node.class); 30 protected final HttpClient client; 31 protected final HttpState[] sessions; 32 protected final long duration; 33 protected final int numRequests[]; 34 protected final SynchronizedRef error = new SynchronizedRef(null); 35 protected final URL [] mutateUrls; 36 protected final URL [] validateUrls; 37 protected final Random random = new Random (); 38 39 public Node(URL mutateUrl, URL validateUrl, int numSessions, long duration) { 40 this(new URL [] { mutateUrl }, new URL [] { validateUrl }, numSessions, duration); 41 } 42 43 public Node(URL [] mutateUrls, URL [] validateUrls, int numSessions, long duration) { 44 MultiThreadedHttpConnectionManager connMgr = new MultiThreadedHttpConnectionManager(); 45 connMgr.getParams().setConnectionTimeout(120 * 1000); 46 this.client = new HttpClient(connMgr); 47 48 this.mutateUrls = mutateUrls; 49 this.validateUrls = validateUrls; 50 this.sessions = createStates(numSessions); 51 this.duration = duration; 52 this.numRequests = new int[sessions.length]; 53 } 54 55 public void checkError() throws Throwable { 56 Throwable t = (Throwable ) error.get(); 57 if (t != null) { throw t; } 58 } 59 60 public void run() { 61 try { 62 makeRequests(); 63 validate(); 64 } catch (Throwable t) { 65 logger.error(t); 66 error.set(t); 67 } 68 } 69 70 private void validate() throws ConnectException , IOException { 71 for (int i = 0; i < sessions.length; i++) { 72 int expect = numRequests[i]; 73 if (expect == 0) { throw new AssertionError ("No requests were ever made for client " + i); } 74 HttpState httpState = sessions[i]; 75 client.setState(httpState); 76 77 for (int u = 0; u < validateUrls.length; u++) { 78 int actual = HttpUtil.getInt(validateUrls[u], client); 79 Assert.assertEquals(getSessionID(httpState), expect, actual); 80 logger.info("validated value of " + expect + " for client " + i + " on " + validateUrls[u]); 81 numRequests[i]++; 83 } 84 } 85 } 86 87 private void makeRequests() throws Exception { 88 final int numURLS = mutateUrls.length; 89 90 int session = 0; 91 final long end = System.currentTimeMillis() + duration; 92 while (System.currentTimeMillis() <= end) { 93 HttpState httpState = sessions[session]; 94 client.setState(httpState); 95 URL mutateUrl = numURLS == 1 ? mutateUrls[0] : mutateUrls[random.nextInt(mutateUrls.length)]; 96 97 final long start = System.currentTimeMillis(); 98 try { 99 int newVal = HttpUtil.getInt(mutateUrl, client); 100 numRequests[session]++; 101 Assert.assertEquals(getSessionID(httpState), numRequests[session], newVal); 102 session = (session + 1) % sessions.length; 103 ThreadUtil.reallySleep(random.nextInt(5) + 1); 104 } catch (Exception e) { 105 logger.error("Elapsed time for failed request was " + (System.currentTimeMillis() - start) + " millis, url = " 106 + mutateUrl); 107 throw e; 108 } 109 } 110 } 111 112 private String getSessionID(HttpState httpState) { 113 List sessionCookies = new ArrayList (); 114 Cookie[] cookies = httpState.getCookies(); 115 for (int i = 0; i < cookies.length; i++) { 116 Cookie cookie = cookies[i]; 117 if (cookie.getName().toLowerCase().indexOf("jsessionid") >= 0) { 118 sessionCookies.add(cookie.getName() + "=" + cookie.getValue() + " at path " + cookie.getPath()); 119 } 120 } 121 122 if (sessionCookies.isEmpty()) { return "no session cookie yet"; } 123 return sessionCookies.toString(); 124 } 125 126 private static HttpState[] createStates(int numSessions) { 127 HttpState[] rv = new HttpState[numSessions]; 128 for (int i = 0; i < numSessions; i++) { 129 rv[i] = new HttpState(); 130 } 131 return rv; 132 } 133 } 134 | Popular Tags |