test/micro/org/openjdk/bench/java/net/NetworkInterfaceLookup.java
changeset 55596 d01b345865d7
equal deleted inserted replaced
55595:cf5a438b3c41 55596:d01b345865d7
       
     1 /*
       
     2  * Copyright (c) 2019, 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 org.openjdk.jmh.annotations.Benchmark;
       
    26 import org.openjdk.jmh.annotations.BenchmarkMode;
       
    27 import org.openjdk.jmh.annotations.Fork;
       
    28 import org.openjdk.jmh.annotations.Measurement;
       
    29 import org.openjdk.jmh.annotations.Mode;
       
    30 import org.openjdk.jmh.annotations.OutputTimeUnit;
       
    31 import org.openjdk.jmh.annotations.Scope;
       
    32 import org.openjdk.jmh.annotations.State;
       
    33 import org.openjdk.jmh.annotations.Warmup;
       
    34 
       
    35 import java.lang.reflect.Method;
       
    36 import java.net.InetAddress;
       
    37 import java.net.NetworkInterface;
       
    38 import java.util.concurrent.TimeUnit;
       
    39 
       
    40 /**
       
    41  * Assess time to perform native NetworkInterface lookups; uses
       
    42  * reflection to access both package-private isBoundInetAddress and
       
    43  * public getByInetAddress (to get comparable numbers)
       
    44  */
       
    45 @BenchmarkMode(Mode.Throughput)
       
    46 @OutputTimeUnit(TimeUnit.SECONDS)
       
    47 @State(Scope.Thread)
       
    48 @Fork(2)
       
    49 @Warmup(iterations = 5, time = 2)
       
    50 @Measurement(iterations = 10, time = 2)
       
    51 public class NetworkInterfaceLookup {
       
    52 
       
    53     static final InetAddress address = InetAddress.getLoopbackAddress();
       
    54 
       
    55     static final Method isBoundInetAddress_method;
       
    56 
       
    57     static final Method getByInetAddress_method;
       
    58 
       
    59     static {
       
    60         Method isBound = null;
       
    61         Method getByInet = null;
       
    62 
       
    63         try {
       
    64             isBound = NetworkInterface.class.getDeclaredMethod("isBoundInetAddress", InetAddress.class);
       
    65             isBound.setAccessible(true);
       
    66         } catch (Exception e) {
       
    67             System.out.println("NetworkInterface.isBoundInetAddress not found");
       
    68         }
       
    69 
       
    70         try {
       
    71             getByInet = NetworkInterface.class.getDeclaredMethod("getByInetAddress", InetAddress.class);
       
    72         } catch (Exception e) {
       
    73             System.out.println("NetworkInterface.getByInetAddress not found");
       
    74         }
       
    75         isBoundInetAddress_method = isBound;
       
    76         getByInetAddress_method = getByInet;
       
    77     }
       
    78 
       
    79     @Benchmark
       
    80     public boolean bound() throws Exception {
       
    81         return (boolean)isBoundInetAddress_method.invoke(null, address);
       
    82     }
       
    83 
       
    84     @Benchmark
       
    85     public NetworkInterface getByInetAddress() throws Exception {
       
    86         return (NetworkInterface)getByInetAddress_method.invoke(null, address);
       
    87     }
       
    88 }