Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/java/net/SocketChannelConnectionSetup.java
41161 views
1
/*
2
* Copyright (c) 2020, 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.net.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 = UnixDomainSocketAddress.of(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=200)
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
.warmupForks(1)
118
.forks(2)
119
.build();
120
121
new Runner(opt).run();
122
123
opt = new OptionsBuilder()
124
.include(org.openjdk.bench.java.net.SocketChannelConnectionSetup.class.getSimpleName())
125
.jvmArgsPrepend("-Djdk.net.useFastTcpLoopback=true")
126
.warmupForks(1)
127
.forks(2)
128
.build();
129
130
new Runner(opt).run();
131
}
132
}
133
134