Path: blob/master/test/micro/org/openjdk/bench/java/net/UnixSocketChannelReadWrite.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.StandardProtocolFamily;26import java.net.UnixDomainSocketAddress;27import java.nio.ByteBuffer;28import java.nio.channels.ClosedChannelException;29import java.nio.channels.ServerSocketChannel;30import java.nio.channels.SocketChannel;31import java.nio.file.*;32import java.util.concurrent.TimeUnit;33import java.util.concurrent.atomic.AtomicInteger;3435import org.openjdk.jmh.annotations.*;3637/**38* Tests the overheads of I/O API.39* This test is known to depend heavily on network conditions and paltform.40*/41@BenchmarkMode(Mode.Throughput)42@OutputTimeUnit(TimeUnit.MILLISECONDS)43@State(Scope.Thread)44public class UnixSocketChannelReadWrite {4546private ServerSocketChannel ssc;47private SocketChannel s1, s2;48private ReadThread rt;49private ByteBuffer bb = ByteBuffer.allocate(1);5051private static volatile String tempDir;52private static final AtomicInteger count = new AtomicInteger(0);53private volatile Path socket;5455static {56try {57Path p = Files.createTempDirectory("readWriteTest");58tempDir = p.toString();59} catch (IOException e) {60tempDir = null;61}62}6364private ServerSocketChannel getServerSocketChannel() throws IOException {65int next = count.incrementAndGet();66socket = Paths.get(tempDir, Integer.toString(next));67UnixDomainSocketAddress addr = UnixDomainSocketAddress.of(socket);68ServerSocketChannel c = ServerSocketChannel.open(StandardProtocolFamily.UNIX);69c.bind(addr);70return c;71}7273@Setup(Level.Trial)74public void beforeRun() throws IOException {75ssc = getServerSocketChannel();76s1 = SocketChannel.open(ssc.getLocalAddress());77s2 = ssc.accept();7879rt = new ReadThread(s2);80rt.start();8182bb.put((byte) 47);83bb.flip();84}8586@TearDown(Level.Trial)87public void afterRun() throws IOException, InterruptedException {88s1.close();89s2.close();90ssc.close();91Files.delete(socket);92Files.delete(Path.of(tempDir));93rt.join();94}9596@Benchmark97public void test() throws IOException {98s1.write(bb);99bb.flip();100}101102static class ReadThread extends Thread {103private SocketChannel sc;104105public ReadThread(SocketChannel s2) {106this.sc = s2;107}108109public void run() {110try {111ByteBuffer bb = ByteBuffer.allocate(1);112while (sc.read(bb) > 0) {113bb.flip();114}115} catch (ClosedChannelException ex) {116// shutdown time117} catch (IOException e) {118e.printStackTrace();119}120}121}122123}124125126