001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: /*
006: * Created on Feb 16, 2004
007: *
008: * To change the template for this generated file go to
009: * Window - Preferences - Java - Code Generation - Code and Comments
010: */
011: package org.vfny.geoserver;
012:
013: import java.io.BufferedReader;
014: import java.io.File;
015: import java.io.FileOutputStream;
016: import java.io.FileReader;
017: import java.io.IOException;
018: import java.io.PrintStream;
019: import java.net.HttpURLConnection;
020: import java.net.URL;
021: import java.util.Date;
022:
023: /**
024: * ThreadedBatchTester purpose.
025: *
026: * <p>
027: * Description of ThreadedBatchTester ...
028: * </p>
029: *
030: * @author dzwiers, Refractions Research, Inc.
031: * @author $Author: cholmesny $ (last modification)
032: * @version $Id: ThreadedBatchTester.java 6326 2007-03-15 18:36:40Z jdeolive $
033: */
034: public class ThreadedBatchTester extends Thread {
035: private static int runs = 100;
036: private static URL url;
037: private static boolean isPost;
038: private static String req = "";
039: private static File log;
040: private static long start;
041: private static int finished = 0;
042: private static int wait = 2;
043: private static String postUrl = "http://localhost:8080/geoserver/wfs";
044:
045: public ThreadedBatchTester() {
046: }
047:
048: public void run() {
049: try {
050: start = new Date().getTime();
051:
052: Thread[] threads = new Thread[runs];
053:
054: if (isPost) {
055: url = new URL(postUrl);
056: for (int i = 0; i < runs; i++)
057: threads[i] = new TestPostThread(url, req);
058: } else {
059: if (url == null) {
060: url = new URL(req);
061: }
062:
063: for (int i = 0; i < runs; i++)
064: threads[i] = new TestGetThread(url);
065: }
066:
067: for (int i = 0; i < runs; i++) {
068: threads[i].start();
069: //If the wait time is not set at a few milliseconds then
070: //we get these BindExceptions, or ConnectExceptions.
071: //I'm not sure if it's really a problem with the servlet
072: //container or this client code... Seems like there should
073: //be someway to tell the threads to maybe try to wait for a
074: //bit?
075: sleep(wait);
076: }
077:
078: while (finished < runs) {
079: sleep(1);
080: }
081:
082: PrintStream os = System.out;
083:
084: if ((log != null) && (log.getAbsoluteFile() != null)) {
085: log = log.getAbsoluteFile();
086: os = new PrintStream(new FileOutputStream(log));
087: }
088:
089: generateOutput(threads, os);
090: } catch (Exception e) {
091: e.printStackTrace();
092: usage();
093: }
094: }
095:
096: public static synchronized void threadDone() {
097: finished++;
098: }
099:
100: public static void main(String[] args) {
101: try {
102: loadArgs(args);
103:
104: ThreadedBatchTester tester = new ThreadedBatchTester();
105: tester.run();
106: } catch (Exception e) {
107: e.printStackTrace();
108: usage();
109: }
110: }
111:
112: private static void generateOutput(Thread[] threads, PrintStream os) {
113: int good = 0;
114:
115: for (int i = 0; i < runs; i++) {
116: switch (((TestGetThread) threads[i]).getResult()) {
117: case HttpURLConnection.HTTP_OK:
118: good++;
119:
120: default:
121: }
122: }
123:
124: os.println(good + "/" + runs + " Tests 'OK' ("
125: + ((good * 1.0) / (runs * 1.0)) + ")\n");
126:
127: for (int i = 0; i < runs; i++) {
128: TestGetThread tpt = (TestGetThread) threads[i];
129:
130: int result = tpt.getResult();
131: if (tpt.getTime2() == null) {
132: os.print(result + " Could not connect\n");
133: } else if (tpt.getTime3() == null) {
134: os.print(result + " Could not complete read\n");
135: } else {
136: os.print(tpt.getResult() + ", ");
137: os.print(tpt.getTime1().getTime() + ", ");
138:
139: if (tpt.getTime2() != null) {
140: os.print(tpt.getTime2().getTime() + ", ");
141: }
142:
143: if (tpt.getTime3() != null) {
144: os.print(tpt.getTime3().getTime() + ", ");
145: double time = (tpt.getTime3().getTime() - tpt
146: .getTime1().getTime()) / 1000.0;
147: os.print("time (s): " + time + "\n");
148: } else {
149: os.print("null\n");
150: }
151: }
152: }
153:
154: long end = new Date().getTime();
155: os.println(good + "/" + runs + " Tests 'OK' ("
156: + ((good * 1.0) / (runs * 1.0)) + ")\n");
157:
158: os.println("Total time (s): " + ((end - start) / 1000.0)
159: + " (start: " + start + ", end: " + end + ")");
160: }
161:
162: private static void loadArgs(String[] args) throws IOException {
163: if (args.length == 0) {
164: return;
165: }
166:
167: int i = 0;
168:
169: //Is this weird nested structure necessary? ch
170: while (i < args.length) {
171: String key = args[i++];
172:
173: if ("-n".equals(key) && (i < args.length)) {
174: String val = args[i++];
175: runs = Integer.parseInt(val);
176: } else {
177: if ("-r".equals(key) && (i < args.length)) {
178: String val = args[i++];
179: File f = new File(val);
180: FileReader fr = new FileReader(f);
181: BufferedReader br = new BufferedReader(fr);
182: String t = "";
183: //does this ready loop work here? It doesn't for
184: //the threads... ch
185: while (br.ready())
186: t += br.readLine();
187:
188: req = t;
189: //this also will get a null for the URL, need to
190: //specify that somewhere... ch
191: } else {
192: if ("-u".equals(key) && (i < args.length)) {
193: String val = args[i++];
194: url = new URL(val);
195: } else {
196: if ("-l".equals(key) && (i < args.length)) {
197: String val = args[i++];
198: log = new File(val);
199: } else {
200: if ("-p".equals(key)) {
201: isPost = true;
202: } else { // usage
203: if ("-w".equals(key) && i < args.length) {
204: wait = Integer.parseInt(args[i++]);
205: } else {
206: usage();
207: }
208: }
209: }
210: }
211: }
212: }
213: }
214: }
215:
216: static void usage() {
217: System.out.println("USAGE:\n");
218: System.out
219: .println("ThreadedBatchTester [-p][-n][-w] [-r | -u]");
220: System.out
221: .println("-n\t Optional\t Number of duplicate requests to create and run.");
222: System.out
223: .println("-p\t Optional\t Number of duplicate requests to create and run.");
224: System.out
225: .println("-r\t Optional\t Mutually Exclusive with -u\t The file containing the request to execute.");
226: System.out
227: .println("-u\t Optional\t Mutually Exclusive with -r\t The URL to execute.");
228: System.out.println("-l\t Optional\t The Log file.");
229: System.out
230: .println("-w\t Optional\t Amount of time to wait between dispatching requests (in ms)");
231: }
232: }
|