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.io.InputStream; 29 import java.io.OutputStream; 30 import java.net.InetAddress; 31 import java.net.ServerSocket; 32 import java.net.Socket; 33 import java.net.SocketException; 34 import java.util.concurrent.TimeUnit; 35 36 import org.openjdk.jmh.annotations.BenchmarkMode; 37 import org.openjdk.jmh.annotations.Benchmark; 38 import org.openjdk.jmh.annotations.Mode; 39 import org.openjdk.jmh.annotations.OutputTimeUnit; 40 import org.openjdk.jmh.annotations.Scope; 41 import org.openjdk.jmh.annotations.Setup; 42 import org.openjdk.jmh.annotations.State; 43 import org.openjdk.jmh.annotations.TearDown; 44 45 /** 46 * Tests the overheads of I/O API. 47 * This test is known to depend heavily on network conditions and paltform. 48 */ 49 @BenchmarkMode(Mode.Throughput) 50 @OutputTimeUnit(TimeUnit.MILLISECONDS) 51 @State(Scope.Thread) 52 public class SocketReadWrite { 53 54 private OutputStream os; 55 private InputStream is; 56 private ServerSocket ss; 57 private Socket s1, s2; 58 private ReadThread rt; 59 60 @Setup 61 public void beforeRun() throws IOException { 62 InetAddress iaddr = InetAddress.getLocalHost(); 63 64 ss = new ServerSocket(0); 65 s1 = new Socket(iaddr, ss.getLocalPort()); 66 s2 = ss.accept(); 67 68 os = s1.getOutputStream(); 69 is = s2.getInputStream(); 70 71 rt = new ReadThread(is); 72 rt.start(); 73 } 74 75 @TearDown 76 public void afterRun() throws IOException, InterruptedException { 77 os.write(0); 78 os.close(); 79 is.close(); 80 s1.close(); 81 s2.close(); 82 ss.close(); 83 rt.join(); 84 } 85 86 @Benchmark 87 public void test() throws IOException { 88 os.write((byte) 4711); 89 } 90 91 static class ReadThread extends Thread { 92 private InputStream is; 93 94 public ReadThread(InputStream is) { 95 this.is = is; 96 } 97 98 public void run() { 99 try { 100 while (is.read() > 0); 101 } catch (SocketException ex) { 102 // ignore - most likely "socket closed", which means shutdown 103 } catch (IOException e) { 104 e.printStackTrace(); 105 } 106 } 107 } 108 109 }