00001
package com.quadcap.http.server22;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
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
00049
00050
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 }