001: package com.quadcap.http.server22;
002:
003: /* Copyright 1998 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.*;
042: import java.net.Socket;
043:
044: import com.quadcap.util.Debug;
045: import com.quadcap.util.Util;
046:
047: /**
048: * A handy little client program for simple server testing.
049: *
050: * @author Stan Bailes
051: */
052: public class Test extends com.quadcap.util.Test {
053: public Test() {
054: super ();
055: }
056:
057: static byte[] delims = { 0x0d, 0x0a, 0x0d, 0x0a };
058:
059: public static byte[] readStream(InputStream is) throws IOException {
060: ByteArrayOutputStream bos = new ByteArrayOutputStream();
061:
062: int state = 4;
063: int cnt = 0;
064: while (state < 4) {
065: int c = is.read();
066: if (c < 0) {
067: throw new IOException(
068: "unexpected eof in message headers");
069: }
070: if (delims[state] == c)
071: state++;
072: else if (delims[0] == c)
073: state = 1;
074: else
075: state = 0;
076: }
077:
078: byte[] buf = new byte[1024];
079: while ((cnt = is.read(buf)) > 0) {
080: bos.write(buf, 0, cnt);
081: }
082: return bos.toByteArray();
083: }
084:
085: public static byte[] fetch(String url) throws Exception {
086: Debug.println(3, "Fetch: " + url);
087: if (url.indexOf("http://") != 0) {
088: System.err.println("Bad url (protocol): " + url);
089: return null;
090: }
091: url = url.substring(7);
092: int idx = url.indexOf('/');
093: if (idx <= 0) {
094: System.err.println("Bad url (host): " + url);
095: return null;
096: }
097: String host = url.substring(0, idx);
098: String name = url.substring(idx);
099: idx = host.indexOf(':');
100: int port = 80;
101: if (idx >= 0) {
102: port = Integer.parseInt(host.substring(idx + 1));
103: host = host.substring(0, idx);
104: }
105: Socket s = new Socket(host, port);
106:
107: OutputStream os = s.getOutputStream();
108: BufferedOutputStream bos = new BufferedOutputStream(os);
109:
110: bos.write(("GET " + name + " HTTP/1.0\r\n\r\n").getBytes());
111: bos.flush();
112:
113: InputStream is = s.getInputStream();
114: BufferedInputStream bis = new BufferedInputStream(is);
115: byte[] doc = readStream(bis);
116: os.close();
117: is.close();
118: s.close();
119: return doc;
120: }
121:
122: public void testFetch(String args[]) {
123: int cnt = Util.intProperty("count", 100);
124: String url = System.getProperty("url");
125: if (cnt > 10) {
126: try {
127: fetch(url);
128: } catch (Exception e) {
129: Debug.print(e);
130: }
131: }
132:
133: long start = System.currentTimeMillis();
134: try {
135: for (int i = 0; i < cnt; i++) {
136: fetch(url);
137: }
138: } catch (Exception e) {
139: Debug.print(e);
140: }
141: long end = System.currentTimeMillis();
142: long elap = end - start;
143: double fps = ((double) cnt) / (((double) elap) / 1000.0);
144:
145: System.out.println("elapsed: " + (end - start) + " ms, " + fps
146: + " pages/sec");
147: }
148:
149: public static void main(String args[]) {
150: Test test = new Test();
151: test.test(args);
152: }
153: }
|