001: /*
002: * <copyright>
003: *
004: * Copyright 2003-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.lib.aggagent.test;
028:
029: import java.io.InputStream;
030: import java.io.OutputStream;
031: import java.net.MalformedURLException;
032: import java.net.URL;
033: import java.net.URLConnection;
034:
035: public class AggressiveClient implements Runnable {
036: private static String DEFAULT_URL = "http://localhost:5555/$Target/test.psp";
037: private static int CYCLES_PER_RUN = 250;
038: private static int NUMBER_OF_THREADS = 20;
039:
040: private Object lock = new Object();
041: private String targetUrl = null;
042: private int requests = 0;
043: private int responses = 0;
044: private int badResponses = 0;
045: private int errors = 0;
046: private int calls = 0;
047: private int activeThreads = 0;
048:
049: public AggressiveClient(String t) {
050: targetUrl = t;
051: if (targetUrl == null || targetUrl.length() == 0)
052: targetUrl = DEFAULT_URL;
053: }
054:
055: public AggressiveClient() {
056: this (DEFAULT_URL);
057: }
058:
059: public void start() {
060: new Thread(this ).start();
061: }
062:
063: private void report() {
064: report(null);
065: }
066:
067: private void report(String response) {
068: StringBuffer buf = new StringBuffer();
069: buf.append("calls = " + calls + "; requests = " + requests
070: + "; responses = " + responses + " (bad = "
071: + badResponses + "); errors = " + errors);
072: if (response != null)
073: buf.append("; got \"" + response + "\"");
074: System.out.println(buf.toString());
075: }
076:
077: private void updateActivityLog(String response) {
078: synchronized (lock) {
079: requests++;
080: calls++;
081: if (response != null) {
082: responses++;
083: if (!response.equals("Thanks."))
084: badResponses++;
085: }
086: // report();
087: }
088: }
089:
090: private void updateFailureLog() {
091: synchronized (lock) {
092: errors++;
093: calls++;
094: // report();
095: }
096: }
097:
098: private void updateCallLog() {
099: synchronized (lock) {
100: calls++;
101: // report();
102: }
103: }
104:
105: public void run() {
106: try {
107: URL url = new URL(targetUrl);
108: synchronized (lock) {
109: activeThreads++;
110: }
111: for (int i = 0; i < CYCLES_PER_RUN; i++) {
112: try {
113: URLConnection conn = url.openConnection();
114: conn.setDoOutput(true);
115: conn.setDoInput(true);
116:
117: // transmit the data
118: OutputStream out = conn.getOutputStream();
119: out.write(REQUEST_BYTES);
120: out.close();
121:
122: String response = null;
123:
124: // read the response
125: try {
126: InputStream in = conn.getInputStream();
127: StringBuffer buf = new StringBuffer();
128: int b = -1;
129: while ((b = in.read()) != -1)
130: buf.append((char) b);
131:
132: in.close();
133: response = buf.toString();
134: } catch (Exception bogus) {
135: bogus.printStackTrace();
136: }
137:
138: updateActivityLog(response);
139: continue;
140: } catch (Exception failure) {
141: updateFailureLog();
142: continue;
143: } catch (Error err) {
144: updateCallLog();
145: throw err;
146: }
147: }
148: synchronized (lock) {
149: activeThreads--;
150: if (activeThreads == 0)
151: report();
152: }
153: } catch (MalformedURLException mfe) {
154: }
155: }
156:
157: public static void main(String[] argv) {
158: String arg = null;
159: if (argv.length > 0)
160: arg = argv[0];
161: scenario_1(arg);
162: }
163:
164: private static void scenario_2(String u) {
165: for (int i = 0; i < NUMBER_OF_THREADS; i++)
166: new AggressiveClient(u).start();
167: }
168:
169: private static void scenario_1(String u) {
170: AggressiveClient c = new AggressiveClient(u);
171: for (int i = 0; i < NUMBER_OF_THREADS; i++)
172: c.start();
173: }
174:
175: private static String STANDARD_REQUEST = "<sentence>" + "<subject>"
176: + "<article>the</article>" + "<noun>ants</noun>"
177: + "<prep_phrase>" + "<prep>in</prep>" + "<object>"
178: + "<proper_noun>France</proper_noun>" + "</object>"
179: + "</prep_phrase>" + "</subject>" + "<predicate>"
180: + "<verb>live</verb>" + "<adverb>mainly</adverb>"
181: + "<prep_phrase>" + "<prep>on</prep>" + "<object>"
182: + "<article>the</article>" + "<noun>plants</noun>"
183: + "</object>" + "</prep_phrase>" + "</predicate>"
184: + "</sentence>";
185: private static byte[] REQUEST_BYTES = STANDARD_REQUEST.getBytes();
186: }
|