test/micro/org/openjdk/bench/java/net/UnixSocketChannelReadWrite.java
author michaelm
Tue, 05 Nov 2019 15:10:00 +0000
branchunixdomainchannels
changeset 58933 57a2e3ed90ec
permissions -rw-r--r--
added some micro benchmarks (SocketChannelCompare does direct comparison)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58933
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
     1
/*
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
     2
 * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
     4
 *
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
     8
 *
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    13
 * accompanied this code).
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    14
 *
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    18
 *
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    21
 * questions.
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    22
 */
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    23
package org.openjdk.bench.java.net;
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    24
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    25
import java.io.IOException;
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    26
import java.net.StandardProtocolFamily;
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    27
import java.nio.channels.UnixDomainSocketAddress;
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    28
import java.nio.ByteBuffer;
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    29
import java.nio.channels.ClosedChannelException;
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    30
import java.nio.channels.ServerSocketChannel;
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    31
import java.nio.channels.SocketChannel;
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    32
import java.nio.file.*;
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    33
import java.util.concurrent.TimeUnit;
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    34
import java.util.concurrent.atomic.AtomicInteger;
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    35
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    36
import org.openjdk.jmh.annotations.*;
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    37
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    38
/**
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    39
 * Tests the overheads of I/O API.
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    40
 * This test is known to depend heavily on network conditions and paltform.
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    41
 */
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    42
@BenchmarkMode(Mode.Throughput)
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    43
@OutputTimeUnit(TimeUnit.MILLISECONDS)
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    44
@State(Scope.Thread)
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    45
public class UnixSocketChannelReadWrite {
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    46
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    47
    private ServerSocketChannel ssc;
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    48
    private SocketChannel s1, s2;
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    49
    private ReadThread rt;
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    50
    private ByteBuffer bb = ByteBuffer.allocate(1);
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    51
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    52
    private static volatile String tempDir;
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    53
    private static final AtomicInteger count = new AtomicInteger(0);
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    54
    private volatile Path socket;
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    55
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    56
    static {
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    57
        try {
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    58
            Path p = Files.createTempDirectory("readWriteTest");
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    59
            tempDir = p.toString();
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    60
        } catch (IOException e) {
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    61
            tempDir = null;
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    62
        }
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    63
    }
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    64
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    65
    private ServerSocketChannel getServerSocketChannel() throws IOException {
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    66
        int next = count.incrementAndGet();
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    67
        socket = Paths.get(tempDir, Integer.toString(next));
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    68
        UnixDomainSocketAddress addr = new UnixDomainSocketAddress(socket);
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    69
        ServerSocketChannel c = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    70
        c.bind(addr);
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    71
        return c;
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    72
    }
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    73
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    74
    @Setup(Level.Trial)
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    75
    public void beforeRun() throws IOException {
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    76
        ssc = getServerSocketChannel();
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    77
        s1 = SocketChannel.open(ssc.getLocalAddress());
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    78
        s2 = ssc.accept();
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    79
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    80
        rt = new ReadThread(s2);
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    81
        rt.start();
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    82
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    83
        bb.put((byte) 47);
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    84
        bb.flip();
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    85
    }
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    86
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    87
    @TearDown(Level.Trial)
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    88
    public void afterRun() throws IOException, InterruptedException {
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    89
        s1.close();
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    90
        s2.close();
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    91
        ssc.close();
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    92
        Files.delete(socket);
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    93
        Files.delete(Path.of(tempDir));
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    94
        rt.join();
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    95
    }
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    96
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    97
    @Benchmark
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    98
    public void test() throws IOException {
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
    99
        s1.write(bb);
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
   100
        bb.flip();
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
   101
    }
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
   102
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
   103
    static class ReadThread extends Thread {
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
   104
        private SocketChannel sc;
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
   105
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
   106
        public ReadThread(SocketChannel s2) {
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
   107
            this.sc = s2;
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
   108
        }
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
   109
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
   110
        public void run() {
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
   111
            try {
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
   112
                ByteBuffer bb = ByteBuffer.allocate(1);
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
   113
                while (sc.read(bb) > 0) {
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
   114
                    bb.flip();
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
   115
                }
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
   116
            } catch (ClosedChannelException ex) {
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
   117
                // shutdown time
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
   118
            } catch (IOException e) {
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
   119
                e.printStackTrace();
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
   120
            }
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
   121
        }
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
   122
    }
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
   123
57a2e3ed90ec added some micro benchmarks (SocketChannelCompare does direct comparison)
michaelm
parents:
diff changeset
   124
}