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
/*
* 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.SocketException;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedByInterruptException;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Arrays;

public class PingTest {
@Test
public void testSocketWriteReadLatency() throws IOException, InterruptedException {
EchoService es = new EchoService(9999);

SocketChannel sc = SocketChannel.open(new InetSocketAddress("localhost", 9999));

ByteBuffer bb = ByteBuffer.allocateDirect(1 * 1024);

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

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

es.stop();
close(sc);
Arrays.sort(times);
System.out.printf("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 {
final ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind(new InetSocketAddress("localhost", 9999));

SocketChannel sc = SocketChannel.open(new InetSocketAddress("localhost", 9999));
configure(sc);
SocketChannel sc2 = ssc.accept();
configure(sc2);
close(ssc);

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);
bb2.flip();
sc2.write(bb2);

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

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

@Test
public void testSocketLatency() throws IOException, InterruptedException {
final ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind(new InetSocketAddress("localhost", 9999));
Thread server = new Thread(new Runnable() {
public void run() {
SocketChannel sc2 = null;
try {
sc2 = ssc.accept();
configure(sc2);
sc2.configureBlocking(false);
close(ssc);
ByteBuffer bb2 = ByteBuffer.allocateDirect(4096);
while (!Thread.interrupted()) {
bb2.clear();
do {
sc2.read(bb2);
} while (bb2.position() == 0);
bb2.flip();
sc2.write(bb2);
}
} catch (ClosedByInterruptException ignored) {
} catch (ClosedChannelException ignored) {
} catch (IOException e) {
e.printStackTrace();
} finally {
close(sc2);
}
}
});
server.start();
SocketChannel sc = SocketChannel.open(new InetSocketAddress("localhost", 9999));
configure(sc);
ByteBuffer bb = 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(32);
sc.write(bb);


bb.clear();
sc.read(bb);
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 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 {
final ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind(new InetSocketAddress("localhost", 9999));

Thread server = new Thread(new Runnable() {
@Override
public void run() {
SocketChannel sc2 = null;
try {
sc2 = ssc.accept();
configure(sc2);
close(ssc);
ByteBuffer bb2 = ByteBuffer.allocateDirect(4096);
while (!Thread.interrupted()) {
bb2.clear();
sc2.read(bb2);
bb2.flip();
sc2.write(bb2);
}
} catch (ClosedByInterruptException ignored) {
} catch (ClosedChannelException ignored) {
} catch (IOException e) {
e.printStackTrace();
} finally {
close(sc2);
}
}
});
server.start();
SocketChannel sc = SocketChannel.open(new InetSocketAddress("localhost", 9999));
configure(sc);

ByteBuffer bb = ByteBuffer.allocateDirect(4096);

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

bb.position(0);
bb.limit(1024);
sc.write(bb);

bb.clear();
sc.read(bb);
}
server.interrupt();
server.join();
long time = System.nanoTime() - start;
close(sc);
System.out.printf("Threaded 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(SocketChannel sc) throws SocketException {
sc.socket().setTcpNoDelay(true);
}
}

Change log

r66 by peter.lawrey on Oct 30, 2011   Diff
Performance comparison of using recyclced
objects
Go to: 
Project members, sign in to write a code review

Older revisions

r58 by peter.lawrey on Aug 30, 2011   Diff
Beerware licensing.
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: 7333 bytes, 223 lines
Powered by Google Project Hosting