Path: blob/master/test/micro/org/openjdk/bench/java/net/SocketChannelConnectionSetup.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.channels.ClosedChannelException;30import java.nio.channels.ServerSocketChannel;31import java.nio.channels.SocketChannel;32import java.nio.file.*;33import java.util.concurrent.TimeUnit;34import java.util.concurrent.atomic.AtomicInteger;3536import org.openjdk.jmh.annotations.*;37import org.openjdk.jmh.runner.Runner;38import org.openjdk.jmh.runner.RunnerException;39import org.openjdk.jmh.runner.options.Options;40import org.openjdk.jmh.runner.options.OptionsBuilder;4142/**43* Measures connection setup times44*/45@BenchmarkMode(Mode.SingleShotTime)46@OutputTimeUnit(TimeUnit.MILLISECONDS)47@State(Scope.Thread)48public class SocketChannelConnectionSetup {4950private ServerSocketChannel ssc;51private SocketChannel s1, s2;5253private static volatile String tempDir;54private static final AtomicInteger count = new AtomicInteger(0);55private volatile Path socket;5657@Param({"inet", "unix"})58private volatile String family;5960static {61try {62Path p = Files.createTempDirectory("readWriteTest");63tempDir = p.toString();64} catch (IOException e) {65tempDir = null;66}67}6869private ServerSocketChannel getServerSocketChannel() throws IOException {70if (family.equals("inet"))71return getInetServerSocketChannel();72else if (family.equals("unix"))73return getUnixServerSocketChannel();74throw new InternalError();75}767778private ServerSocketChannel getInetServerSocketChannel() throws IOException {79InetAddress iaddr = InetAddress.getLoopbackAddress();80return ServerSocketChannel.open().bind(null);81}8283private ServerSocketChannel getUnixServerSocketChannel() throws IOException {84int next = count.incrementAndGet();85socket = Paths.get(tempDir, Integer.toString(next));86UnixDomainSocketAddress addr = UnixDomainSocketAddress.of(socket);87return ServerSocketChannel.open(StandardProtocolFamily.UNIX).bind(addr);88}8990@Setup(Level.Trial)91public void beforeRun() throws IOException {92ssc = getServerSocketChannel();93}9495@TearDown(Level.Trial)96public void afterRun() throws IOException, InterruptedException {97ssc.close();98if (family.equals("unix")) {99Files.delete(socket);100Files.delete(Path.of(tempDir));101}102}103104@Benchmark105@Measurement(iterations = 5, batchSize=200)106public void test() throws IOException {107s1 = SocketChannel.open(ssc.getLocalAddress());108s2 = ssc.accept();109s1.close();110s2.close();111}112113public static void main(String[] args) throws RunnerException {114Options opt = new OptionsBuilder()115.include(org.openjdk.bench.java.net.SocketChannelConnectionSetup.class.getSimpleName())116.warmupForks(1)117.forks(2)118.build();119120new Runner(opt).run();121122opt = new OptionsBuilder()123.include(org.openjdk.bench.java.net.SocketChannelConnectionSetup.class.getSimpleName())124.jvmArgsPrepend("-Djdk.net.useFastTcpLoopback=true")125.warmupForks(1)126.forks(2)127.build();128129new Runner(opt).run();130}131}132133134