KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > test > server > appserver > load > Node


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

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 JavaDoc;
20 import java.net.ConnectException JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Random JavaDoc;
25
26 import junit.framework.Assert;
27
28 public class Node implements Runnable JavaDoc {
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 JavaDoc[] mutateUrls;
36   protected final URL JavaDoc[] validateUrls;
37   protected final Random JavaDoc random = new Random JavaDoc();
38
39   public Node(URL JavaDoc mutateUrl, URL JavaDoc validateUrl, int numSessions, long duration) {
40     this(new URL JavaDoc[] { mutateUrl }, new URL JavaDoc[] { validateUrl }, numSessions, duration);
41   }
42
43   public Node(URL JavaDoc[] mutateUrls, URL JavaDoc[] 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 JavaDoc {
56     Throwable JavaDoc t = (Throwable JavaDoc) error.get();
57     if (t != null) { throw t; }
58   }
59
60   public void run() {
61     try {
62       makeRequests();
63       validate();
64     } catch (Throwable JavaDoc t) {
65       logger.error(t);
66       error.set(t);
67     }
68   }
69
70   private void validate() throws ConnectException JavaDoc, IOException JavaDoc {
71     for (int i = 0; i < sessions.length; i++) {
72       int expect = numRequests[i];
73       if (expect == 0) { throw new AssertionError JavaDoc("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         // Recording the request that was just made. This is needed for RequestCountTest.
82
numRequests[i]++;
83       }
84     }
85   }
86
87   private void makeRequests() throws Exception JavaDoc {
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 JavaDoc 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 JavaDoc 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 JavaDoc getSessionID(HttpState httpState) {
113     List JavaDoc sessionCookies = new ArrayList JavaDoc();
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