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
/*
* Copyright (c) 2011. Peter Lawrey
*
* "THE BEER-WARE LICENSE" (Revision 128)
* As long as you retain this notice you can do whatever you want with this stuff.
* If we meet some day, and you think this stuff is worth it, you can buy me a beer in return
* There is no warranty.
*/

package com.google.code.java.core.socket;

import org.junit.Test;

import java.io.Closeable;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
* Requires Java 7.
*/
public class AsyncPingTest {
@Test
public void testSocketWriteReadLatency() throws IOException, InterruptedException, ExecutionException {
final AsynchronousServerSocketChannel ssc =
AsynchronousServerSocketChannel.open().bind(new InetSocketAddress("localhost", 9999));

AsynchronousSocketChannel sc = AsynchronousSocketChannel.open();
Future<Void> connected = sc.connect(new InetSocketAddress("localhost", 9999));
Future<AsynchronousSocketChannel> accepted = ssc.accept();
AsynchronousSocketChannel sc2 = accepted.get();
configure(sc2);
close(ssc);
connected.get();

ByteBuffer bb = ByteBuffer.allocateDirect(4096);
ByteBuffer bb2 = ByteBuffer.allocateDirect(4096);

long times[] = new long[1000 * 1000];
for (int i = -10000; i < times.length; i++) {
long start = System.nanoTime();
bb.position(0);
bb.limit(1024);
sc.write(bb);

bb2.clear();
sc2.read(bb2).get();

bb2.flip();
sc2.write(bb2);

bb.clear();
sc.read(bb).get();
long end = System.nanoTime();
long err = System.nanoTime() - end;
long time = end - start - err;
if (i >= 0)
times[i] = time;
}

close(sc);
close(sc2);
Arrays.sort(times);
System.out.printf("Async Socket latency was 1/50/99%%tile %.1f/%.1f/%.1f us%n",
times[times.length / 100] / 1e3,
times[times.length / 2] / 1e3,
times[times.length - times.length / 100 - 1] / 1e3
);
}

@Test
public void testSocketWriteReadThroughput() throws IOException, InterruptedException, ExecutionException {
final AsynchronousServerSocketChannel ssc =
AsynchronousServerSocketChannel.open().bind(new InetSocketAddress("localhost", 9999));

AsynchronousSocketChannel sc = AsynchronousSocketChannel.open();
Future<Void> connected = sc.connect(new InetSocketAddress("localhost", 9999));
Future<AsynchronousSocketChannel> accepted = ssc.accept();
AsynchronousSocketChannel sc2 = accepted.get();
configure(sc2);
close(ssc);
connected.get();

ByteBuffer bb = ByteBuffer.allocateDirect(4096);
ByteBuffer bb2 = ByteBuffer.allocateDirect(4096);

long start = System.nanoTime();
int runs = 1000 * 1000;
for (int i = 0; i < runs; i++) {
bb.position(0);
bb.limit(1024);
sc.write(bb);

bb2.clear();
sc2.read(bb2).get();
bb2.flip();
sc2.write(bb2);

bb.clear();
sc.read(bb).get();
}
long time = System.nanoTime() - start;

close(sc);
close(sc2);
System.out.printf("Async Socket Throughput was %,d K/s%n", runs * 1000000L / time);
}

@Test
public void testSocketLatency() throws IOException, InterruptedException, ExecutionException {
final AsynchronousServerSocketChannel ssc =
AsynchronousServerSocketChannel.open().bind(new InetSocketAddress("localhost", 9999));

AsynchronousSocketChannel sc = AsynchronousSocketChannel.open();
Future<Void> connected = sc.connect(new InetSocketAddress("localhost", 9999));

Thread server = new Thread(new Runnable() {
public void run() {
AsynchronousSocketChannel sc2 = null;
try {
Future<AsynchronousSocketChannel> accepted = ssc.accept();
sc2 = accepted.get();
close(ssc);
ByteBuffer bb1 = ByteBuffer.allocateDirect(4096);
ByteBuffer bb2 = ByteBuffer.allocateDirect(4096);
Future<Integer> lastWrite = null;
ByteBuffer bb = bb1;
while (!Thread.interrupted()) {
bb.clear();
Future<Integer> integerFuture = sc2.read(bb);
while (!integerFuture.isDone()) {
if (Thread.interrupted()) return;
}
bb.flip();
// wait for the previous write.
if (lastWrite != null) lastWrite.get();
lastWrite = sc2.write(bb);
// swap the buffers
bb = (bb == bb1) ? bb2 : bb1;
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} finally {
close(sc2);
}
}
});
server.start();

ByteBuffer bb = ByteBuffer.allocateDirect(4096);
long times[] = new long[1000 * 1000];

connected.get();
for (int i = -10000; i < times.length; i++) {
long start = System.nanoTime();
bb.position(0);
bb.limit(32);
sc.write(bb).get();


bb.clear();
sc.read(bb).get();
long end = System.nanoTime();
long err = System.nanoTime() - end;
long time = end - start - err;
if (i >= 0)
times[i] = time;
}
server.interrupt();
close(sc);
Arrays.sort(times);
System.out.printf("Threaded Async Socket Latency for 1/50/99%%tile %.1f/%.1f/%.1f us%n",
times[times.length / 100] / 1e3,
times[times.length / 2] / 1e3,
times[times.length - times.length / 100 - 1] / 1e3
);
}

@Test
public void testSocketThreadedThroughput() throws IOException, InterruptedException, ExecutionException {
int messageSize = 512;

final AsynchronousServerSocketChannel ssc =
AsynchronousServerSocketChannel.open().bind(new InetSocketAddress("localhost", 9999));

AsynchronousSocketChannel sc = AsynchronousSocketChannel.open();
Future<Void> connected = sc.connect(new InetSocketAddress("localhost", 9999));

Thread server = new Thread(new Runnable() {
public void run() {
AsynchronousSocketChannel sc2 = null;
try {
Future<AsynchronousSocketChannel> accepted = ssc.accept();
sc2 = accepted.get();
close(ssc);
ByteBuffer bb1 = ByteBuffer.allocateDirect(4096);
ByteBuffer bb2 = ByteBuffer.allocateDirect(4096);
Future<Integer> lastWrite = null;
ByteBuffer bb = bb1;
while (!Thread.interrupted()) {
bb.clear();
Future<Integer> integerFuture = sc2.read(bb);
while (!integerFuture.isDone()) {
if (Thread.interrupted()) return;
}
bb.flip();
// wait for the previous write.
if (lastWrite != null) lastWrite.get();
lastWrite = sc2.write(bb);
// swap the buffers
bb = (bb == bb1) ? bb2 : bb1;
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} finally {
close(sc2);
}
}
});
server.start();

ByteBuffer bb1 = ByteBuffer.allocateDirect(4096);
ByteBuffer bb2 = ByteBuffer.allocateDirect(4096);
ByteBuffer bb = ByteBuffer.allocateDirect(4096);

connected.get();

long start = System.nanoTime();
int runs = 1000 * 1000;
Future<Integer> lastRead = null;
Future<Integer> lastWrite = null;
for (int i = 0; i < runs; i += 2) {
bb1.position(0);
bb1.limit(messageSize);
if (lastWrite != null) lastWrite.get();
Future<Integer> write1 = sc.write(bb1);

bb2.position(0);
bb2.limit(messageSize);
write1.get();
lastWrite = sc.write(bb2);

if (lastRead != null) lastRead.get();
bb.clear();
lastRead = sc.read(bb);
}
server.interrupt();
server.join();
long time = System.nanoTime() - start;
close(sc);
System.out.printf("Threaded Async Socket Throughput was %,d K/s%n", runs * 1000000L / time);
}

static void close(Closeable sc2) {
if (sc2 != null) try {
sc2.close();
} catch (IOException ignored) {
}
}


static void configure(AsynchronousSocketChannel sc) throws IOException {
sc.setOption(StandardSocketOptions.TCP_NODELAY, true);
}
}

Change log

r58 by peter.lawrey on Aug 30, 2011   Diff
Beerware licensing.
Go to: 
Project members, sign in to write a code review

Older revisions

r45 by peter.lawrey on Aug 1, 2011   Diff
Added a ping test for Java 7 Async
Sockets
r34 by peter.lawrey on Jul 27, 2011   Diff
Test ping over loopback latency
All revisions of this file

File info

Size: 8676 bytes, 275 lines
Powered by Google Project Hosting