test/micro/org/openjdk/bench/java/net/SocketReadWrite.java
author skuksenko
Tue, 14 May 2019 15:09:31 -0700
changeset 54850 f8d45530b75e
parent 52595 16609197022c
child 57358 f0a1d9760c5e
permissions -rw-r--r--
8223921: Update SocketReadWrite benchmark Reviewed-by: redestad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52595
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
     1
/*
54850
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
     2
 * Copyright (c) 2014, 2019 Oracle and/or its affiliates. All rights reserved.
52595
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
     4
 *
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
     7
 * published by the Free Software Foundation.
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
     8
 *
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    13
 * accompanied this code).
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    14
 *
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    18
 *
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    21
 * questions.
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    22
 */
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    23
package org.openjdk.bench.java.net;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    24
54850
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    25
import org.openjdk.jmh.annotations.Benchmark;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    26
import org.openjdk.jmh.annotations.BenchmarkMode;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    27
import org.openjdk.jmh.annotations.Mode;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    28
import org.openjdk.jmh.annotations.OutputTimeUnit;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    29
import org.openjdk.jmh.annotations.Param;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    30
import org.openjdk.jmh.annotations.Scope;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    31
import org.openjdk.jmh.annotations.Setup;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    32
import org.openjdk.jmh.annotations.State;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    33
import org.openjdk.jmh.annotations.TearDown;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    34
52595
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    35
import java.io.IOException;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    36
import java.io.InputStream;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    37
import java.io.OutputStream;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    38
import java.net.InetAddress;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    39
import java.net.ServerSocket;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    40
import java.net.Socket;
54850
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    41
import java.util.ArrayList;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    42
import java.util.List;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    43
import java.util.concurrent.CountDownLatch;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    44
import java.util.concurrent.ThreadLocalRandom;
52595
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    45
import java.util.concurrent.TimeUnit;
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    46
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    47
/**
54850
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    48
 * Benchmark socket read/write.
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    49
 *
52595
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    50
 */
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    51
@BenchmarkMode(Mode.Throughput)
54850
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    52
@OutputTimeUnit(TimeUnit.SECONDS)
52595
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    53
@State(Scope.Thread)
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    54
public class SocketReadWrite {
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
    55
54850
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    56
    static final InetAddress address = InetAddress.getLoopbackAddress();
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    57
    public static final int TIMEOUT = 10000;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    58
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    59
    static class EchoServer implements Runnable {
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    60
        // EchoServer is implemented to execute the same amount echo threads as benchmarking threads are running
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    61
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    62
        final ServerSocket ss;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    63
        final int port;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    64
        final CountDownLatch startedLatch;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    65
        final int size;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    66
        final boolean timeout;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    67
        List<ServerThread> threads = new ArrayList<>();
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    68
        volatile boolean isDone = false;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    69
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    70
        public EchoServer(CountDownLatch await, int size, boolean timeout) throws IOException {
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    71
            this.size = size;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    72
            this.timeout = timeout;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    73
            ss = new ServerSocket(0);
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    74
            port = ss.getLocalPort();
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    75
            this.startedLatch = await;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    76
        }
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    77
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    78
        @Override
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    79
        public void run() {
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    80
            startedLatch.countDown();
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    81
            while (!isDone) {
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    82
                try {
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    83
                    Socket s = ss.accept();
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    84
                    s.setTcpNoDelay(true);
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    85
                    if (timeout) {
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    86
                        s.setSoTimeout(TIMEOUT);
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    87
                    }
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    88
                    ServerThread st = new ServerThread(s, size);
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    89
                    threads.add(st);
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    90
                    new Thread(st).start();
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    91
                } catch (IOException e) {
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    92
                    if (!isDone) {
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    93
                        e.printStackTrace();
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    94
                    }
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    95
                }
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    96
            }
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    97
        }
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    98
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
    99
        synchronized void close() throws IOException {
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   100
            if (!isDone) {
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   101
                isDone = true;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   102
                ss.close();
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   103
                for (ServerThread st : threads) {
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   104
                    st.close();
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   105
                }
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   106
            }
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   107
        }
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   108
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   109
        static EchoServer instance = null;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   110
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   111
        static synchronized EchoServer startServer(int size, boolean timeout) throws IOException {
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   112
            if (instance == null) {
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   113
                CountDownLatch started = new CountDownLatch(1);
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   114
                EchoServer s = new EchoServer(started, size, timeout);
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   115
                new Thread(s).start();
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   116
                try {
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   117
                    started.await(); // wait until server thread started
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   118
                } catch (InterruptedException e) {
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   119
                    e.printStackTrace();
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   120
                }
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   121
                instance = s;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   122
            }
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   123
            return instance;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   124
        }
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   125
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   126
        static class ServerThread implements Runnable {
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   127
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   128
            final Socket s;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   129
            final InputStream in;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   130
            final OutputStream out;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   131
            final int size;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   132
            volatile boolean isDone = false;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   133
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   134
            ServerThread(Socket s, int size) throws IOException {
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   135
                this.s = s;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   136
                this.size = size;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   137
                in = s.getInputStream();
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   138
                out = s.getOutputStream();
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   139
            }
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   140
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   141
            @Override
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   142
            public void run() {
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   143
                byte[] a = new byte[size];
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   144
                while (!isDone) {
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   145
                    try {
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   146
                        readN(a, size, this.in);
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   147
                        out.write(a);
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   148
                    } catch (IOException e) {
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   149
                        if (!isDone) {
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   150
                            e.printStackTrace();
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   151
                        }
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   152
                    }
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   153
                }
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   154
            }
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   155
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   156
            public void close() throws IOException {
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   157
                isDone = true;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   158
                s.close();
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   159
            }
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   160
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   161
        }
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   162
    }
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   163
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   164
    static void readN(byte[] array, int size, InputStream in) throws IOException {
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   165
        int nread = 0;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   166
        while (size > 0) {
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   167
            int n = in.read(array, nread, size);
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   168
            if (n < 0) throw new RuntimeException();
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   169
            nread += n;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   170
            size -= n;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   171
        }
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   172
    }
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   173
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   174
    EchoServer server;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   175
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   176
    @Param({"1", "1024", "8192", "64000", "128000"})
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   177
    public int size;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   178
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   179
    @Param({"false", "true"})
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   180
    public boolean timeout;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   181
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   182
    Socket s;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   183
    InputStream in;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   184
    OutputStream out;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   185
    byte[] array;
52595
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   186
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   187
    @Setup
54850
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   188
    public void setup() throws IOException {
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   189
        server = EchoServer.startServer(size, timeout);
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   190
        int port = server.port;
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   191
        s = new Socket(address, port);
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   192
        s.setTcpNoDelay(true);
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   193
        if (timeout) {
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   194
            s.setSoTimeout(TIMEOUT);
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   195
            // 10 seconds times is quite large and never will happen (for microbenchmarking),
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   196
            // but it's required since other paths inside SocketImpl are involved
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   197
        }
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   198
        in = s.getInputStream();
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   199
        out = s.getOutputStream();
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   200
        array = new byte[size];
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   201
        ThreadLocalRandom.current().nextBytes(array);
52595
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   202
    }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   203
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   204
    @TearDown
54850
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   205
    public void tearDown() throws IOException {
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   206
        server.close();
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   207
        s.close();
52595
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   208
    }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   209
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   210
    @Benchmark
54850
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   211
    public void echo() throws IOException {
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   212
        out.write(array);
f8d45530b75e 8223921: Update SocketReadWrite benchmark
skuksenko
parents: 52595
diff changeset
   213
        readN(array, size, in);
52595
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   214
    }
16609197022c 8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff changeset
   215
}