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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package org.openjdk.bench.java.net;
  26 
  27 import java.io.IOException;
  28 import java.net.InetAddress;
  29 import java.net.InetSocketAddress;
  30 import java.nio.ByteBuffer;
  31 import java.nio.channels.ClosedChannelException;
  32 import java.nio.channels.ServerSocketChannel;
  33 import java.nio.channels.SocketChannel;
  34 import java.util.concurrent.TimeUnit;
  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 SocketChannelReadWrite {
  46 
  47     private ServerSocketChannel ssc;
  48     private SocketChannel s1, s2;
  49     private ReadThread rt;
  50     private ByteBuffer bb = ByteBuffer.allocate(1);
  51 
  52     @Setup(Level.Trial)
  53     public void beforeRun() throws IOException {
  54         InetAddress iaddr = InetAddress.getLocalHost();
  55 
  56         ssc = ServerSocketChannel.open().bind(null);
  57         s1 = SocketChannel.open(new InetSocketAddress(iaddr, ssc.socket().getLocalPort()));
  58         s2 = ssc.accept();
  59 
  60         rt = new ReadThread(s2);
  61         rt.start();
  62 
  63         bb.put((byte) 47);
  64         bb.flip();
  65     }
  66 
  67     @TearDown(Level.Trial)
  68     public void afterRun() throws IOException, InterruptedException {
  69         s1.close();
  70         s2.close();
  71         ssc.close();
  72         rt.join();
  73     }
  74 
  75     @Benchmark
  76     public void test() throws IOException {
  77         s1.write(bb);
  78         bb.flip();
  79     }
  80 
  81     static class ReadThread extends Thread {
  82         private SocketChannel sc;
  83 
  84         public ReadThread(SocketChannel s2) {
  85             this.sc = s2;
  86         }
  87 
  88         public void run() {
  89             try {
  90                 ByteBuffer bb = ByteBuffer.allocate(1);
  91                 while (sc.read(bb) > 0) {
  92                     bb.flip();
  93                 }
  94             } catch (ClosedChannelException ex) {
  95                 // shutdown time
  96             } catch (IOException e) {
  97                 e.printStackTrace();
  98             }
  99         }
 100     }
 101 
 102 }