59030
|
1 |
/*
|
|
2 |
* Copyright (c) 2019 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.channels.ClosedChannelException;
|
|
31 |
import java.nio.channels.ServerSocketChannel;
|
|
32 |
import java.nio.channels.SocketChannel;
|
|
33 |
import java.nio.file.*;
|
|
34 |
import java.util.concurrent.TimeUnit;
|
|
35 |
import java.util.concurrent.atomic.AtomicInteger;
|
|
36 |
|
|
37 |
import org.openjdk.jmh.annotations.*;
|
|
38 |
import org.openjdk.jmh.runner.Runner;
|
|
39 |
import org.openjdk.jmh.runner.RunnerException;
|
|
40 |
import org.openjdk.jmh.runner.options.Options;
|
|
41 |
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
|
42 |
|
|
43 |
/**
|
|
44 |
* Measures connection setup times
|
|
45 |
*/
|
|
46 |
@BenchmarkMode(Mode.SingleShotTime)
|
|
47 |
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
|
48 |
@State(Scope.Thread)
|
|
49 |
public class SocketChannelConnectionSetup {
|
|
50 |
|
|
51 |
private ServerSocketChannel ssc;
|
|
52 |
private SocketChannel s1, s2;
|
|
53 |
|
|
54 |
private static volatile String tempDir;
|
|
55 |
private static final AtomicInteger count = new AtomicInteger(0);
|
|
56 |
private volatile Path socket;
|
|
57 |
|
|
58 |
@Param({"inet", "unix"})
|
|
59 |
private volatile String family;
|
|
60 |
|
|
61 |
static {
|
|
62 |
try {
|
|
63 |
Path p = Files.createTempDirectory("readWriteTest");
|
|
64 |
tempDir = p.toString();
|
|
65 |
} catch (IOException e) {
|
|
66 |
tempDir = null;
|
|
67 |
}
|
|
68 |
}
|
|
69 |
|
|
70 |
private ServerSocketChannel getServerSocketChannel() throws IOException {
|
|
71 |
if (family.equals("inet"))
|
|
72 |
return getInetServerSocketChannel();
|
|
73 |
else if (family.equals("unix"))
|
|
74 |
return getUnixServerSocketChannel();
|
|
75 |
throw new InternalError();
|
|
76 |
}
|
|
77 |
|
|
78 |
|
|
79 |
private ServerSocketChannel getInetServerSocketChannel() throws IOException {
|
|
80 |
InetAddress iaddr = InetAddress.getLoopbackAddress();
|
|
81 |
return ServerSocketChannel.open().bind(null);
|
|
82 |
}
|
|
83 |
|
|
84 |
private ServerSocketChannel getUnixServerSocketChannel() throws IOException {
|
|
85 |
int next = count.incrementAndGet();
|
|
86 |
socket = Paths.get(tempDir, Integer.toString(next));
|
|
87 |
UnixDomainSocketAddress addr = new UnixDomainSocketAddress(socket);
|
|
88 |
return ServerSocketChannel.open(StandardProtocolFamily.UNIX).bind(addr);
|
|
89 |
}
|
|
90 |
|
|
91 |
@Setup(Level.Trial)
|
|
92 |
public void beforeRun() throws IOException {
|
|
93 |
ssc = getServerSocketChannel();
|
|
94 |
}
|
|
95 |
|
|
96 |
@TearDown(Level.Trial)
|
|
97 |
public void afterRun() throws IOException, InterruptedException {
|
|
98 |
ssc.close();
|
|
99 |
if (family.equals("unix")) {
|
|
100 |
Files.delete(socket);
|
|
101 |
Files.delete(Path.of(tempDir));
|
|
102 |
}
|
|
103 |
}
|
|
104 |
|
|
105 |
@Benchmark
|
|
106 |
@Measurement(iterations = 5, batchSize=50)
|
|
107 |
public void test() throws IOException {
|
|
108 |
s1 = SocketChannel.open(ssc.getLocalAddress());
|
|
109 |
s2 = ssc.accept();
|
|
110 |
s1.close();
|
|
111 |
s2.close();
|
|
112 |
}
|
|
113 |
|
|
114 |
public static void main(String[] args) throws RunnerException {
|
|
115 |
Options opt = new OptionsBuilder()
|
|
116 |
.include(org.openjdk.bench.java.net.SocketChannelConnectionSetup.class.getSimpleName())
|
|
117 |
.forks(3)
|
|
118 |
.build();
|
|
119 |
|
|
120 |
new Runner(opt).run();
|
|
121 |
|
|
122 |
opt = new OptionsBuilder()
|
|
123 |
.include(org.openjdk.bench.java.net.SocketChannelConnectionSetup.class.getSimpleName())
|
|
124 |
.jvmArgsPrepend("-Djdk.net.useFastTcpLoopback=true")
|
|
125 |
.forks(3)
|
|
126 |
.build();
|
|
127 |
|
|
128 |
new Runner(opt).run();
|
|
129 |
}
|
|
130 |
}
|