1 4 package com.tctest.server.appserver.simulation; 5 6 import org.apache.commons.httpclient.HttpClient; 7 8 import EDU.oswego.cs.dl.util.concurrent.PooledExecutor; 9 import EDU.oswego.cs.dl.util.concurrent.SynchronizedBoolean; 10 import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt; 11 12 import com.tc.test.server.appserver.AppServerResult; 13 import com.tc.test.server.appserver.unit.AbstractAppServerTestCase; 14 import com.tc.test.server.util.HttpUtil; 15 16 import java.io.IOException ; 17 import java.io.PrintWriter ; 18 import java.net.URL ; 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import javax.servlet.http.HttpServlet ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 import javax.servlet.http.HttpSession ; 27 28 public final class LoadSimulationPrototype extends AbstractAppServerTestCase { 29 30 private static final int STICKY = 70; 31 private static final int REQUEST_ITERATIONS = 1000; 32 private static final SynchronizedBoolean[] stickyResults = new SynchronizedBoolean[REQUEST_ITERATIONS]; 33 private final PooledExecutor pool = new PooledExecutor(5); 34 private final AppServerResult[] containers = new AppServerResult[2]; 35 private final SessionWrapper[] sessions = new SessionWrapper[100]; 36 private final SynchronizedInt done = new SynchronizedInt(0); 37 private final List errors = new ArrayList (); 38 39 public void setUp() throws Exception { 40 super.setUp(); 41 for (int i = 0; i < sessions.length; i++) { 42 sessions[i] = new SessionWrapper(HttpUtil.createHttpClient()); 43 } 44 45 for (int i = 0; i < stickyResults.length; i++) { 46 stickyResults[i] = new SynchronizedBoolean(false); 47 } 48 49 } 50 51 public void testSessions() throws Exception { 52 53 startDsoServer(); 54 55 for (int i = 0; i < containers.length; i++) { 56 containers[i] = startAppServer(true); 57 } 58 59 pool.setKeepAliveTime(1000); 60 61 for (int i = 0; i < REQUEST_ITERATIONS; i++) { 62 pool.execute(new Requestor(i)); 63 } 64 65 pool.shutdownAfterProcessingCurrentlyQueuedTasks(); 66 pool.awaitTerminationAfterShutdown(); 67 68 assertEquals(REQUEST_ITERATIONS, done.get()); 70 71 synchronized (errors) { 72 for (Iterator i = errors.iterator(); i.hasNext();) { 73 Throwable t = (Throwable ) i.next(); 74 t.printStackTrace(); 75 } 76 if (errors.size() > 0) { 77 fail("requests encountered errors"); 78 } 79 } 80 81 tallyResults(); 82 } 83 84 private void reportError(Throwable t) { 85 synchronized (errors) { 86 errors.add(t); 87 } 88 } 89 90 private void tallyResults() { 91 int stickyCount = 0; 92 for (int i = 0; i < stickyResults.length; i++) { 93 if (stickyResults[i].get()) stickyCount++; 94 } 95 96 double result = ((double) stickyCount / (double) stickyResults.length) * 100; 97 98 System.out.println("ORIGINAL STICKYNESS = " + STICKY + "%"); 99 System.out.println("ACTUAL STICKYNESS = " + result + "%"); 100 101 assertTrue(true); 102 } 103 104 private int getRandom(int seed) { 105 return new Long (Math.round(Math.floor(seed * Math.random()))).intValue(); 106 } 107 108 public static final class StickySessionServlet extends HttpServlet { 109 110 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { 111 HttpSession session = request.getSession(true); 112 String instance = System.getProperty("app_instance"); 113 String session_instance = (String ) session.getAttribute("instance"); 114 115 boolean isSticky = true; 116 if (session_instance != null && !session_instance.equals(instance)) isSticky = false; 117 session.setAttribute("instance", instance); 118 119 response.setContentType("text/html"); 120 PrintWriter out = response.getWriter(); 121 out.println(isSticky); 122 System.out.println(session.getId() + ": " + isSticky); 123 } 124 } 125 126 private class Requestor implements Runnable { 127 128 private final int iterationIndex; 129 130 private Requestor(int iteration) { 131 iterationIndex = iteration; 132 } 133 134 public void run() { 135 try { 136 int randomSession = getRandom(sessions.length); 137 int randomValue = getRandom(100); 138 if (!(STICKY >= randomValue + 1)) { 139 int prevPort = sessions[randomSession].getContainerPort(); 140 int newPort; 141 do { 142 newPort = containers[getRandom(containers.length)].serverPort(); 143 sessions[randomSession].setContainerPort(newPort); 144 } while (newPort == prevPort); 145 } 146 147 URL url = createUrl(sessions[randomSession].getContainerPort(), 148 LoadSimulationPrototype.StickySessionServlet.class); 149 stickyResults[iterationIndex].set(HttpUtil.getBoolean(url, sessions[randomSession].getSession())); 150 } catch (Throwable t) { 151 reportError(t); 152 } finally { 153 done.increment(); 154 } 155 } 156 } 157 158 private class SessionWrapper { 159 160 private HttpClient session; 161 private int containerPort = -1; 162 163 private SessionWrapper(HttpClient session) { 164 this.session = session; 165 } 166 167 private synchronized HttpClient getSession() { 168 return session; 169 } 170 171 private synchronized void setContainerPort(int containerPort) { 172 this.containerPort = containerPort; 173 } 174 175 private synchronized int getContainerPort() { 176 if (containerPort == -1) { 177 containerPort = containers[getRandom(containers.length)].serverPort(); 178 } 179 return containerPort; 180 } 181 } 182 } 183 | Popular Tags |