My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package org.xmpp.jnodes;

import junit.framework.TestCase;
import org.junit.Ignore;
import org.xmpp.jnodes.nio.DatagramListener;
import org.xmpp.jnodes.nio.MockSocket;
import org.xmpp.jnodes.nio.SelDatagramChannel;

import java.io.IOException;
import java.net.BindException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;

public class RelayChannelTest extends TestCase {

final static String encode = "UTF-8";
final static String localIP = "127.0.0.1";
private final static ExecutorService executorService = Executors.newCachedThreadPool();

public void testDatagramChannels() {
final List<Future> futures = new ArrayList<Future>();
int max = 1;

for (int i = 0; i < max; i++) {
final int ii = i;
futures.add(executorService.submit(new Runnable() {
public void run() {
try {
socketTest(new MockSocket.ChannelProvider() {
public SelDatagramChannel open(DatagramListener datagramListener, SocketAddress address) throws IOException {
return SelDatagramChannel.open(datagramListener, address);
}

public String getName() {
return "SelDatagramChannel";
}
}, 1500 * ii + 10000, 1500 * ii + 11000);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}));
}
boolean finished = false;

while (!finished) {
try {
Thread.sleep(5000);
Thread.yield();
} catch (InterruptedException e) {
e.printStackTrace();
}
finished = true;
for (final Future f : futures) {
finished &= f.isDone();
}
}
}

@Ignore("Meant to be ran manually")
public static boolean testDatagramChannelsExternal(final int portA, final int portB) throws IOException, InterruptedException {

final SocketAddress sa = new InetSocketAddress(localIP, portA);
final SocketAddress sb = new InetSocketAddress(localIP, portB);

boolean f = true;
for (int i = 0; i < 1 && f; i++) {
f = socketTest(new MockSocket.ChannelProvider() {
public SelDatagramChannel open(DatagramListener datagramListener, SocketAddress address) throws IOException {
return SelDatagramChannel.open(datagramListener, address);
}

public String getName() {
return "SelDatagramChannel";
}
}, sa, sb);
}
return f;
}

public void socketTest(final MockSocket.ChannelProvider provider, final int socketRange, final int relayRange) throws IOException, InterruptedException {
final int num = 2;
final int packets = 10;
final int tests = 1;
final List<MockSocket> cs = new ArrayList<MockSocket>();
final List<RelayChannel> rc = new ArrayList<RelayChannel>();

for (int i = 0, j = 0, l = 0; i < num; i++, j++, l++) {
for (int t = 0; t < num; t++) {
try {
final MockSocket s = new MockSocket(localIP, socketRange + j, provider);
cs.add(s);
break;
} catch (BindException e) {
j++;
}
}
if (i % 2 == 0) {
for (int t = 0; t < num; t++) {
try {
final RelayChannel c = new RelayChannel(localIP, relayRange + l);
rc.add(c);
break;
} catch (BindException e) {
l++;
}
}
}
}

long tTime = 0;
long min = 1000;
long max = 0;
final AtomicInteger fSent = new AtomicInteger(0);

for (int h = 0; h < tests; h++) {
final List<Future> futures = new ArrayList<Future>();

final long start = System.currentTimeMillis();

for (int ii = 0; ii < packets; ii++) {
futures.add(executorService.submit(new Runnable() {
public void run() {
final AtomicInteger sent = new AtomicInteger(0);
for (int i = 0; i < num; i++) {
final MockSocket a = cs.get(i);
final MockSocket b = i % 2 == 0 ? cs.get(i + 1) : cs.get(i - 1);

final RelayChannel c = rc.get(i / 2);
final SocketAddress d = i % 2 == 0 ? c.getAddressA() : c.getAddressB();

try {
int ss = 0;
while (ss == 0) {
ss = a.getChannel().send(b.getExpectedBuffer().duplicate(), d);
if (ss == 0) {
System.out.println("Retrying Send...");
} else {
sent.incrementAndGet();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
fSent.incrementAndGet();
}
}));
}

boolean finished = false;

while (!finished) {
Thread.sleep(1);
finished = true;
for (final Future f : futures) {
finished &= f.isDone();
}
}

finished = false;
final int target = packets - 1;
while (!finished) {
Thread.sleep(5);
int a = 0;
int b = 0;
for (final MockSocket s : cs) {
if (s.getI().get() >= packets) {
b++;
} else if (s.getI().get() >= target) {
a++;
}
}
finished = a + b == num;
}

final long d = (System.currentTimeMillis() - start);
if (d > max) {
max = d;
}
if (d < min) {
min = d;
}
tTime += d;

for (final MockSocket ts : cs)
ts.getI().set(0);
}

System.out.println(provider.getName() + " -> Max: " + max + "ms, Min: " + min + "ms, Avg: " + Math.ceil(tTime / tests) + "ms");

for (final MockSocket ts : cs) {
ts.getChannel().close();
}
for (final RelayChannel r : rc) {
r.close();
}

}

public static boolean socketTest(final MockSocket.ChannelProvider provider, final SocketAddress sa, final SocketAddress sb) throws IOException, InterruptedException {
final int num = 2;
int packets = 25;
int tests = 100;
final List<MockSocket> cs = new ArrayList<MockSocket>();

for (int i = 0, j = 0, l = 0; i < num; i++, j++, l++) {
for (int t = 0; t < 50; t++) {
try {
final MockSocket s = new MockSocket(localIP, 20000 + j, provider);
cs.add(s);
break;
} catch (BindException e) {
j++;
}
}
}

long tTime = 0;
long min = 1000;
long max = 0;
boolean finished = true;

for (int h = 0; h < tests && finished; h++) {

final long start = System.currentTimeMillis();

for (int ii = 0; ii < packets; ii++)
for (int i = 0; i < num; i++) {
final MockSocket a = cs.get(i);
final MockSocket b = i % 2 == 0 ? cs.get(i + 1) : cs.get(i - 1);

final SocketAddress d = i % 2 == 0 ? sa : sb;

a.getChannel().send(b.getExpectedBuffer().duplicate(), d);
}

finished = false;
final int target = packets - 1;
int t = 0;
while (!finished && t < packets * tests) {
Thread.sleep(1);
finished = true;
for (int i = 0; i < num; i++) {
finished &= cs.get(i).getI().get() >= target;
}
t++;
}

if (finished) {
final long d = (System.currentTimeMillis() - start);
if (d > max) {
max = d;
}
if (d < min) {
min = d;
}
tTime += d;
}

for (final MockSocket ts : cs)
ts.getI().set(0);
}

if (finished) {
System.out.println(provider.getName() + " -> Max: " + max + "ms, Min: " + min + "ms, Avg: " + Math.ceil(tTime / tests) + "ms");
}

for (final MockSocket ts : cs) {
ts.getChannel().close();
}

return finished;
}

public void testSocketPerformance() {
int max = 500;
for (int j = 0; j < max; j++)
for (int i = 0; i < max; i++) {
try {
SelDatagramChannel c = SelDatagramChannel.open(null, new InetSocketAddress(localIP, i + 30000));
c.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public void testSocketPerformanceConcurrent() {
int max = 100;
int t = 10;
final List<List<SelDatagramChannel>> lists = new ArrayList<List<SelDatagramChannel>>();

for (int i = 0; i < t; i++) {
lists.add(new ArrayList<SelDatagramChannel>());
}

int r = 0;
for (int j = 0; j < max; j++)
for (int i = 0; i < max; i++) {
try {
SelDatagramChannel c = SelDatagramChannel.open(null, new InetSocketAddress(localIP, i + 20000 + (j * max)));
lists.get(r++).add(c);
if (r >= t) {
r = 0;
}
} catch (IOException e) {
e.printStackTrace();
}
}

for (int i = 0; i < t; i++) {
final int tt = t;
executorService.submit(new Runnable() {
public void run() {
for (final SelDatagramChannel c : lists.get(tt)) {
try {
c.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
}

public static void main(String args[]) {
try {
testDatagramChannelsExternal(Integer.valueOf(args[1]), Integer.valueOf(args[2]));
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

public void testRelayChannel() throws IOException {
final RelayChannel rc = RelayChannel.createLocalRelayChannel("0.0.0.0",10000,30000);

assertNotNull(rc);

}
}

Change log

r140 by barata7 on Nov 11, 2010   Diff
Public IP Resolver
Go to: 
Project members, sign in to write a code review

Older revisions

r124 by barata7 on Oct 18, 2010   Diff
Namespace and Bahavioral Changes
r86 by barata7 on Jan 6, 2010   Diff
Extra Unit Test
r73 by barata7 on Dec 8, 2009   Diff
[9] - Unit Test Fixes
All revisions of this file

File info

Size: 12037 bytes, 352 lines
Powered by Google Project Hosting