Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/java/net/SocketChannelCompare.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.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
import org.openjdk.jmh.runner.Runner;
40
import org.openjdk.jmh.runner.RunnerException;
41
import org.openjdk.jmh.runner.options.Options;
42
import org.openjdk.jmh.runner.options.OptionsBuilder;
43
44
/**
45
* Tests sending a 128 byte message on a second, to a thread which
46
* echo's it back and received by the original thread.
47
* Benchmark is performed for "inet" channels over TCP/IP
48
* and "unix" domain channels.
49
*/
50
@BenchmarkMode(Mode.Throughput)
51
@OutputTimeUnit(TimeUnit.MILLISECONDS)
52
@State(Scope.Thread)
53
public class SocketChannelCompare {
54
55
static final int BUFSIZE = 128; // message size sent and received
56
private ServerSocketChannel ssc;
57
private SocketChannel s1, s2;
58
private EchoThread rt;
59
private ByteBuffer bb = ByteBuffer.allocate(BUFSIZE);
60
61
private static volatile String tempDir;
62
private static final AtomicInteger count = new AtomicInteger(0);
63
private volatile Path socket;
64
65
@Param({"inet", "unix"})
66
private volatile String family;
67
68
static {
69
try {
70
Path p = Files.createTempDirectory("readWriteTest");
71
tempDir = p.toString();
72
} catch (IOException e) {
73
tempDir = null;
74
}
75
}
76
77
private ServerSocketChannel getServerSocketChannel() throws IOException {
78
if (family.equals("inet"))
79
return getInetServerSocketChannel();
80
else if (family.equals("unix"))
81
return getUnixServerSocketChannel();
82
throw new InternalError();
83
}
84
85
86
private ServerSocketChannel getInetServerSocketChannel() throws IOException {
87
InetAddress iaddr = InetAddress.getLoopbackAddress();
88
return ServerSocketChannel.open().bind(null);
89
}
90
91
private ServerSocketChannel getUnixServerSocketChannel() throws IOException {
92
int next = count.incrementAndGet();
93
socket = Paths.get(tempDir, Integer.toString(next));
94
UnixDomainSocketAddress addr = UnixDomainSocketAddress.of(socket);
95
return ServerSocketChannel.open(StandardProtocolFamily.UNIX).bind(addr);
96
}
97
98
@Setup(Level.Trial)
99
public void beforeRun() throws IOException {
100
ssc = getServerSocketChannel();
101
s1 = SocketChannel.open(ssc.getLocalAddress());
102
s2 = ssc.accept();
103
104
rt = new EchoThread(s2);
105
rt.start();
106
}
107
108
@TearDown(Level.Trial)
109
public void afterRun() throws IOException, InterruptedException {
110
s1.close();
111
s2.close();
112
ssc.close();
113
if (family.equals("unix")) {
114
Files.delete(socket);
115
Files.delete(Path.of(tempDir));
116
}
117
rt.join();
118
}
119
120
@Benchmark
121
public void test() throws IOException {
122
bb.position(0).limit(BUFSIZE);
123
s1.write(bb);
124
bb.clear();
125
readFully(s1, bb);
126
}
127
128
// read until buf is full, or EOF. Always returns number of bytes read
129
130
static int readFully(SocketChannel chan, ByteBuffer buf) throws IOException {
131
int n = buf.remaining();
132
int count = 0;
133
while (n > 0) {
134
int c = chan.read(buf);
135
if (c == -1)
136
return count;
137
n -= c;
138
count += c;
139
}
140
return count;
141
}
142
143
static class EchoThread extends Thread {
144
private SocketChannel sc;
145
146
public EchoThread(SocketChannel s2) {
147
this.sc = s2;
148
}
149
150
public void run() {
151
try {
152
ByteBuffer bb = ByteBuffer.allocate(BUFSIZE);
153
while (true) {
154
bb.clear();
155
int c = readFully(sc, bb);
156
if (c == 0) {
157
sc.close();
158
return;
159
}
160
bb.flip();
161
sc.write(bb);
162
}
163
} catch (ClosedChannelException ex) {
164
// shutdown time
165
} catch (IOException ioex) {
166
ioex.printStackTrace();
167
}
168
}
169
}
170
171
public static void main(String[] args) throws RunnerException {
172
Options opt = new OptionsBuilder()
173
.include(org.openjdk.bench.java.net.SocketChannelCompare.class.getSimpleName())
174
.warmupForks(1)
175
.warmupIterations(2)
176
.measurementIterations(2)
177
.forks(2)
178
.build();
179
180
new Runner(opt).run();
181
182
opt = new OptionsBuilder()
183
.include(org.openjdk.bench.java.net.SocketChannelCompare.class.getSimpleName())
184
.warmupForks(1)
185
.warmupIterations(2)
186
.measurementIterations(2)
187
.jvmArgsPrepend("-Djdk.net.useFastTcpLoopback=true")
188
.forks(3)
189
.build();
190
191
new Runner(opt).run();
192
}
193
}
194
195