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