|
1 /* |
|
2 * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 package org.openjdk.bench.java.net; |
|
24 |
|
25 import java.io.IOException; |
|
26 import java.net.InetAddress; |
|
27 import java.net.InetSocketAddress; |
|
28 import java.net.StandardProtocolFamily; |
|
29 import java.nio.channels.UnixDomainSocketAddress; |
|
30 import java.nio.ByteBuffer; |
|
31 import java.nio.channels.ClosedChannelException; |
|
32 import java.nio.channels.ServerSocketChannel; |
|
33 import java.nio.channels.SocketChannel; |
|
34 import java.nio.file.*; |
|
35 import java.util.concurrent.TimeUnit; |
|
36 import java.util.concurrent.atomic.AtomicInteger; |
|
37 |
|
38 import org.openjdk.jmh.annotations.*; |
|
39 |
|
40 /** |
|
41 * Tests sending a 128 byte message on a second, to a thread which |
|
42 * echo's it back and received by the original thread. |
|
43 * Benchmark is performed for "inet" channels over TCP/IP |
|
44 * and "unix" domain channels. |
|
45 */ |
|
46 @BenchmarkMode(Mode.Throughput) |
|
47 @OutputTimeUnit(TimeUnit.MILLISECONDS) |
|
48 @State(Scope.Thread) |
|
49 public class SocketChannelCompare { |
|
50 |
|
51 static final int BUFSIZE = 128; // message size sent and received |
|
52 private ServerSocketChannel ssc; |
|
53 private SocketChannel s1, s2; |
|
54 private EchoThread rt; |
|
55 private ByteBuffer bb = ByteBuffer.allocate(BUFSIZE); |
|
56 |
|
57 private static volatile String tempDir; |
|
58 private static final AtomicInteger count = new AtomicInteger(0); |
|
59 private volatile Path socket; |
|
60 |
|
61 @Param({"inet", "unix"}) |
|
62 private volatile String family; |
|
63 |
|
64 static { |
|
65 try { |
|
66 Path p = Files.createTempDirectory("readWriteTest"); |
|
67 tempDir = p.toString(); |
|
68 } catch (IOException e) { |
|
69 tempDir = null; |
|
70 } |
|
71 } |
|
72 |
|
73 private ServerSocketChannel getServerSocketChannel() throws IOException { |
|
74 if (family.equals("inet")) |
|
75 return getInetServerSocketChannel(); |
|
76 else if (family.equals("unix")) |
|
77 return getUnixServerSocketChannel(); |
|
78 throw new InternalError(); |
|
79 } |
|
80 |
|
81 |
|
82 private ServerSocketChannel getInetServerSocketChannel() throws IOException { |
|
83 InetAddress iaddr = InetAddress.getLocalHost(); |
|
84 return ServerSocketChannel.open().bind(null); |
|
85 } |
|
86 |
|
87 private ServerSocketChannel getUnixServerSocketChannel() throws IOException { |
|
88 int next = count.incrementAndGet(); |
|
89 socket = Paths.get(tempDir, Integer.toString(next)); |
|
90 UnixDomainSocketAddress addr = new UnixDomainSocketAddress(socket); |
|
91 return ServerSocketChannel.open(StandardProtocolFamily.UNIX).bind(addr); |
|
92 } |
|
93 |
|
94 @Setup(Level.Trial) |
|
95 public void beforeRun() throws IOException { |
|
96 ssc = getServerSocketChannel(); |
|
97 s1 = SocketChannel.open(ssc.getLocalAddress()); |
|
98 s2 = ssc.accept(); |
|
99 |
|
100 rt = new EchoThread(s2); |
|
101 rt.start(); |
|
102 } |
|
103 |
|
104 @TearDown(Level.Trial) |
|
105 public void afterRun() throws IOException, InterruptedException { |
|
106 s1.close(); |
|
107 s2.close(); |
|
108 ssc.close(); |
|
109 if (family.equals("unix")) { |
|
110 Files.delete(socket); |
|
111 Files.delete(Path.of(tempDir)); |
|
112 } |
|
113 rt.join(); |
|
114 } |
|
115 |
|
116 @Benchmark |
|
117 public void test() throws IOException { |
|
118 bb.position(0).limit(BUFSIZE); |
|
119 s1.write(bb); |
|
120 bb.clear(); |
|
121 readFully(s1, bb); |
|
122 } |
|
123 |
|
124 // read until buf is full, or EOF. Always returns number of bytes read |
|
125 |
|
126 static int readFully(SocketChannel chan, ByteBuffer buf) throws IOException { |
|
127 int n = buf.remaining(); |
|
128 int count = 0; |
|
129 while (n > 0) { |
|
130 int c = chan.read(buf); |
|
131 if (c == -1) |
|
132 return count; |
|
133 n -= c; |
|
134 count += c; |
|
135 } |
|
136 return count; |
|
137 } |
|
138 |
|
139 static class EchoThread extends Thread { |
|
140 private SocketChannel sc; |
|
141 |
|
142 public EchoThread(SocketChannel s2) { |
|
143 this.sc = s2; |
|
144 } |
|
145 |
|
146 public void run() { |
|
147 try { |
|
148 ByteBuffer bb = ByteBuffer.allocate(BUFSIZE); |
|
149 while (true) { |
|
150 bb.clear(); |
|
151 int c = readFully(sc, bb); |
|
152 if (c == 0) { |
|
153 sc.close(); |
|
154 return; |
|
155 } |
|
156 bb.flip(); |
|
157 sc.write(bb); |
|
158 } |
|
159 } catch (ClosedChannelException ex) { |
|
160 // shutdown time |
|
161 } catch (IOException ioex) { |
|
162 ioex.printStackTrace(); |
|
163 } |
|
164 } |
|
165 } |
|
166 } |