Quadcap Embeddable Server

com/quadcap/http/server22/Test.java

Go to the documentation of this file.
00001 package com.quadcap.http.server22; 00002 00003 /* Copyright 1998 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.*; 00042 import java.net.Socket; 00043 00044 import com.quadcap.util.Debug; 00045 import com.quadcap.util.Util; 00046 00047 /** 00048 * A handy little client program for simple server testing. 00049 * 00050 * @author Stan Bailes 00051 */ 00052 public class Test extends com.quadcap.util.Test { 00053 public Test() { 00054 super(); 00055 } 00056 00057 static byte[] delims = { 0x0d, 0x0a, 0x0d, 0x0a }; 00058 00059 public static byte[] readStream(InputStream is) throws IOException { 00060 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 00061 00062 int state = 4; 00063 int cnt = 0; 00064 while (state < 4) { 00065 int c = is.read(); 00066 if (c < 0) { 00067 throw new IOException("unexpected eof in message headers"); 00068 } 00069 if (delims[state] == c) state++; 00070 else if (delims[0] == c) state = 1; 00071 else state = 0; 00072 } 00073 00074 byte[] buf = new byte[1024]; 00075 while ((cnt = is.read(buf)) > 0) { 00076 bos.write(buf, 0, cnt); 00077 } 00078 return bos.toByteArray(); 00079 } 00080 00081 public static byte[] fetch(String url) throws Exception { 00082 Debug.println(3, "Fetch: " + url); 00083 if (url.indexOf("http://") != 0) { 00084 System.err.println("Bad url (protocol): " + url); 00085 return null; 00086 } 00087 url = url.substring(7); 00088 int idx = url.indexOf('/'); 00089 if (idx <= 0) { 00090 System.err.println("Bad url (host): " + url); 00091 return null; 00092 } 00093 String host = url.substring(0, idx); 00094 String name = url.substring(idx); 00095 idx = host.indexOf(':'); 00096 int port = 80; 00097 if (idx >= 0) { 00098 port = Integer.parseInt(host.substring(idx+1)); 00099 host = host.substring(0, idx); 00100 } 00101 Socket s = new Socket(host, port); 00102 00103 OutputStream os = s.getOutputStream(); 00104 BufferedOutputStream bos = new BufferedOutputStream(os); 00105 00106 bos.write(("GET " + name + " HTTP/1.0\r\n\r\n").getBytes()); 00107 bos.flush(); 00108 00109 InputStream is = s.getInputStream(); 00110 BufferedInputStream bis = new BufferedInputStream(is); 00111 byte[] doc = readStream(bis); 00112 os.close(); 00113 is.close(); 00114 s.close(); 00115 return doc; 00116 } 00117 00118 public void testFetch(String args[]) { 00119 int cnt = Util.intProperty("count", 100); 00120 String url = System.getProperty("url"); 00121 if (cnt > 10) { 00122 try { 00123 fetch(url); 00124 } catch (Exception e) { 00125 Debug.print(e); 00126 } 00127 } 00128 00129 long start = System.currentTimeMillis(); 00130 try { 00131 for (int i = 0; i < cnt; i++) { 00132 fetch(url); 00133 } 00134 } catch (Exception e) { 00135 Debug.print(e); 00136 } 00137 long end = System.currentTimeMillis(); 00138 long elap = end - start; 00139 double fps = ((double)cnt) / (((double)elap) / 1000.0); 00140 00141 System.out.println("elapsed: " + (end - start) + " ms, " + 00142 fps + " pages/sec"); 00143 } 00144 00145 00146 public static void main(String args[]) { 00147 Test test = new Test(); 00148 test.test(args); 00149 } 00150 }