Path: blob/master/test/micro/org/openjdk/bench/java/net/SocketChannelCompare.java
41161 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package org.openjdk.bench.java.net;2324import java.io.IOException;25import java.net.InetAddress;26import java.net.InetSocketAddress;27import java.net.StandardProtocolFamily;28import java.net.UnixDomainSocketAddress;29import java.nio.ByteBuffer;30import java.nio.channels.ClosedChannelException;31import java.nio.channels.ServerSocketChannel;32import java.nio.channels.SocketChannel;33import java.nio.file.*;34import java.util.concurrent.TimeUnit;35import java.util.concurrent.atomic.AtomicInteger;3637import org.openjdk.jmh.annotations.*;38import org.openjdk.jmh.runner.Runner;39import org.openjdk.jmh.runner.RunnerException;40import org.openjdk.jmh.runner.options.Options;41import org.openjdk.jmh.runner.options.OptionsBuilder;4243/**44* Tests sending a 128 byte message on a second, to a thread which45* echo's it back and received by the original thread.46* Benchmark is performed for "inet" channels over TCP/IP47* and "unix" domain channels.48*/49@BenchmarkMode(Mode.Throughput)50@OutputTimeUnit(TimeUnit.MILLISECONDS)51@State(Scope.Thread)52public class SocketChannelCompare {5354static final int BUFSIZE = 128; // message size sent and received55private ServerSocketChannel ssc;56private SocketChannel s1, s2;57private EchoThread rt;58private ByteBuffer bb = ByteBuffer.allocate(BUFSIZE);5960private static volatile String tempDir;61private static final AtomicInteger count = new AtomicInteger(0);62private volatile Path socket;6364@Param({"inet", "unix"})65private volatile String family;6667static {68try {69Path p = Files.createTempDirectory("readWriteTest");70tempDir = p.toString();71} catch (IOException e) {72tempDir = null;73}74}7576private ServerSocketChannel getServerSocketChannel() throws IOException {77if (family.equals("inet"))78return getInetServerSocketChannel();79else if (family.equals("unix"))80return getUnixServerSocketChannel();81throw new InternalError();82}838485private ServerSocketChannel getInetServerSocketChannel() throws IOException {86InetAddress iaddr = InetAddress.getLoopbackAddress();87return ServerSocketChannel.open().bind(null);88}8990private ServerSocketChannel getUnixServerSocketChannel() throws IOException {91int next = count.incrementAndGet();92socket = Paths.get(tempDir, Integer.toString(next));93UnixDomainSocketAddress addr = UnixDomainSocketAddress.of(socket);94return ServerSocketChannel.open(StandardProtocolFamily.UNIX).bind(addr);95}9697@Setup(Level.Trial)98public void beforeRun() throws IOException {99ssc = getServerSocketChannel();100s1 = SocketChannel.open(ssc.getLocalAddress());101s2 = ssc.accept();102103rt = new EchoThread(s2);104rt.start();105}106107@TearDown(Level.Trial)108public void afterRun() throws IOException, InterruptedException {109s1.close();110s2.close();111ssc.close();112if (family.equals("unix")) {113Files.delete(socket);114Files.delete(Path.of(tempDir));115}116rt.join();117}118119@Benchmark120public void test() throws IOException {121bb.position(0).limit(BUFSIZE);122s1.write(bb);123bb.clear();124readFully(s1, bb);125}126127// read until buf is full, or EOF. Always returns number of bytes read128129static int readFully(SocketChannel chan, ByteBuffer buf) throws IOException {130int n = buf.remaining();131int count = 0;132while (n > 0) {133int c = chan.read(buf);134if (c == -1)135return count;136n -= c;137count += c;138}139return count;140}141142static class EchoThread extends Thread {143private SocketChannel sc;144145public EchoThread(SocketChannel s2) {146this.sc = s2;147}148149public void run() {150try {151ByteBuffer bb = ByteBuffer.allocate(BUFSIZE);152while (true) {153bb.clear();154int c = readFully(sc, bb);155if (c == 0) {156sc.close();157return;158}159bb.flip();160sc.write(bb);161}162} catch (ClosedChannelException ex) {163// shutdown time164} catch (IOException ioex) {165ioex.printStackTrace();166}167}168}169170public static void main(String[] args) throws RunnerException {171Options opt = new OptionsBuilder()172.include(org.openjdk.bench.java.net.SocketChannelCompare.class.getSimpleName())173.warmupForks(1)174.warmupIterations(2)175.measurementIterations(2)176.forks(2)177.build();178179new Runner(opt).run();180181opt = new OptionsBuilder()182.include(org.openjdk.bench.java.net.SocketChannelCompare.class.getSimpleName())183.warmupForks(1)184.warmupIterations(2)185.measurementIterations(2)186.jvmArgsPrepend("-Djdk.net.useFastTcpLoopback=true")187.forks(3)188.build();189190new Runner(opt).run();191}192}193194195