Path: blob/master/test/micro/org/openjdk/bench/java/net/SocketReadWrite.java
41161 views
/*1* Copyright (c) 2014, 2019, 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 org.openjdk.jmh.annotations.Benchmark;25import org.openjdk.jmh.annotations.BenchmarkMode;26import org.openjdk.jmh.annotations.Mode;27import org.openjdk.jmh.annotations.OutputTimeUnit;28import org.openjdk.jmh.annotations.Param;29import org.openjdk.jmh.annotations.Scope;30import org.openjdk.jmh.annotations.Setup;31import org.openjdk.jmh.annotations.State;32import org.openjdk.jmh.annotations.TearDown;3334import java.io.IOException;35import java.io.InputStream;36import java.io.OutputStream;37import java.net.InetAddress;38import java.net.ServerSocket;39import java.net.Socket;40import java.util.ArrayList;41import java.util.List;42import java.util.concurrent.CountDownLatch;43import java.util.concurrent.ThreadLocalRandom;44import java.util.concurrent.TimeUnit;4546/**47* Benchmark socket read/write.48*49*/50@BenchmarkMode(Mode.Throughput)51@OutputTimeUnit(TimeUnit.SECONDS)52@State(Scope.Thread)53public class SocketReadWrite {5455static final InetAddress address = InetAddress.getLoopbackAddress();56public static final int TIMEOUT = 10000;5758static class EchoServer implements Runnable {59// EchoServer is implemented to execute the same amount echo threads as benchmarking threads are running6061final ServerSocket ss;62final int port;63final CountDownLatch startedLatch;64final int size;65final boolean timeout;66List<ServerThread> threads = new ArrayList<>();67volatile boolean isDone = false;6869public EchoServer(CountDownLatch await, int size, boolean timeout) throws IOException {70this.size = size;71this.timeout = timeout;72ss = new ServerSocket(0);73port = ss.getLocalPort();74this.startedLatch = await;75}7677@Override78public void run() {79startedLatch.countDown();80while (!isDone) {81try {82Socket s = ss.accept();83s.setTcpNoDelay(true);84if (timeout) {85s.setSoTimeout(TIMEOUT);86}87ServerThread st = new ServerThread(s, size);88threads.add(st);89new Thread(st).start();90} catch (IOException e) {91if (!isDone) {92e.printStackTrace();93}94}95}96}9798synchronized void close() throws IOException {99if (!isDone) {100isDone = true;101ss.close();102for (ServerThread st : threads) {103st.close();104}105}106}107108static EchoServer instance = null;109110static synchronized EchoServer startServer(int size, boolean timeout) throws IOException {111if (instance == null) {112CountDownLatch started = new CountDownLatch(1);113EchoServer s = new EchoServer(started, size, timeout);114new Thread(s).start();115try {116started.await(); // wait until server thread started117} catch (InterruptedException e) {118e.printStackTrace();119}120instance = s;121}122return instance;123}124125static class ServerThread implements Runnable {126127final Socket s;128final InputStream in;129final OutputStream out;130final int size;131volatile boolean isDone = false;132133ServerThread(Socket s, int size) throws IOException {134this.s = s;135this.size = size;136in = s.getInputStream();137out = s.getOutputStream();138}139140@Override141public void run() {142byte[] a = new byte[size];143while (!isDone) {144try {145readN(a, size, this.in);146out.write(a);147} catch (IOException e) {148if (!isDone) {149e.printStackTrace();150}151}152}153}154155public void close() throws IOException {156isDone = true;157s.close();158}159160}161}162163static void readN(byte[] array, int size, InputStream in) throws IOException {164int nread = 0;165while (size > 0) {166int n = in.read(array, nread, size);167if (n < 0) throw new RuntimeException();168nread += n;169size -= n;170}171}172173EchoServer server;174175@Param({"1", "1024", "8192", "64000", "128000"})176public int size;177178@Param({"false", "true"})179public boolean timeout;180181Socket s;182InputStream in;183OutputStream out;184byte[] array;185186@Setup187public void setup() throws IOException {188server = EchoServer.startServer(size, timeout);189int port = server.port;190s = new Socket(address, port);191s.setTcpNoDelay(true);192if (timeout) {193s.setSoTimeout(TIMEOUT);194// 10 seconds times is quite large and never will happen (for microbenchmarking),195// but it's required since other paths inside SocketImpl are involved196}197in = s.getInputStream();198out = s.getOutputStream();199array = new byte[size];200ThreadLocalRandom.current().nextBytes(array);201}202203@TearDown204public void tearDown() throws IOException {205server.close();206s.close();207}208209@Benchmark210public void echo() throws IOException {211out.write(array);212readN(array, size, in);213}214}215216217