Menu

[r717]: / branches / Release-5-3-3 / php-java-bridge / unsupported / TestServ.java  Maximize  Restore  History

Download this file

101 lines (90 with data), 2.8 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import java.io.*;
import java.net.*;
import java.util.*;
/**
* This program checks if your operating system contains John Nagle's
* TCP/IP "ack delay" bug fix. If not, the second test will run up to
* 10 times slower
*
* Type java TestServ to run this test.
*/
public class TestServ implements Runnable {
static final int SIZE=8192;
static final int PORT=9767;
private String getID(byte[] b, int n) {
String str = (new String(b, 0, n));
//System.out.println(str);
int idx = str.lastIndexOf("=", 0); idx+=2;
int idx2= str.indexOf("\"", idx);
return str.substring(idx, idx2);
}
public void run() {
try {
doRun();
} catch (Exception e) {
e.printStackTrace();
}
}
public void doRun() throws Exception {
int n;
ServerSocket ss = new ServerSocket(PORT, 1, InetAddress.getByName("127.0.0.1"));
while(true) {
byte[] b = new byte[SIZE];
Socket s = ss.accept();
InputStream in = s.getInputStream();
in.read(b, 0, 1); //options
n = in.read(b, 0, 51);
OutputStream out = s.getOutputStream();
byte[] x = ("<N i=\""+getID(b, n)+"\"/>").getBytes();
out.write(x, 0, x.length);
s.close();
}
}
private static void runTest1() throws Exception {
for(int i=0; i<200; i++) {
Socket s = new Socket("127.0.0.1", PORT);
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
out.write(("@<I v=\"0\" m=\"lastException\" p=\"P\" i=\"136070284\"></I>").getBytes());
byte[] b = new byte[1024];
in.read(b);
s.close();
}
}
// same as above, buf send two packets
private static void runTest2() throws Exception {
for(int i=0; i<200; i++) {
Socket s = new Socket("127.0.0.1", PORT);
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
out.write('@');
out.write(("<I v=\"0\" m=\"lastException\" p=\"P\" i=\"136070284\"></I>").getBytes());
byte[] b = new byte[1024];
in.read(b);
s.close();
}
}
public static void main(String _s[]) throws Exception {
Thread t = new Thread(new TestServ());
t.start();
Thread.sleep(100);
long T1 = System.currentTimeMillis();
runTest1();
long T2 = System.currentTimeMillis();
runTest2();
long T3 = System.currentTimeMillis();
System.out.println("The following test demonstrates a bug in the BSD kernel:");
System.out.println("Both tests transfer the same amount of data.");
System.out.println("But the second test splits the packet after the first byte.");
System.out.println("Test1: "+(T2-T1));
System.out.println("Test2: "+(T3-T2));
short c = (short)((T3-T2)/(T2-T1));
System.out.println("Test2/Test1: "+c);
if(c>1) {
System.out.println("Test failed");
System.exit(1);
}
System.out.println("Test okay");
System.exit(0);
}
}