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.lang.invoke;
  24 
  25 import org.openjdk.jmh.annotations.Benchmark;
  26 import org.openjdk.jmh.annotations.BenchmarkMode;
  27 import org.openjdk.jmh.annotations.Mode;
  28 import org.openjdk.jmh.annotations.OutputTimeUnit;
  29 import org.openjdk.jmh.annotations.Scope;
  30 import org.openjdk.jmh.annotations.Setup;
  31 import org.openjdk.jmh.annotations.State;
  32 
  33 import java.lang.invoke.MethodHandle;
  34 import java.lang.invoke.MethodHandles;
  35 import java.util.concurrent.TimeUnit;
  36 
  37 /**
  38  * Benchmark assesses MethodHandles.throwException() performance
  39  */
  40 @BenchmarkMode(Mode.AverageTime)
  41 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  42 @State(Scope.Benchmark)
  43 public class MethodHandlesThrowException {
  44 
  45     /**
  46      * Implementation notes:
  47      *   - exceptions have a thorough call back to benchmark instance to prevent elimination (against dumb JITs)
  48      *   - testing in plain and cached modes
  49      *   - baselines do the same thing, but in pure Java
  50      */
  51 
  52     public int flag;
  53     private MethodHandle mh;
  54     private MyException cachedException;
  55 
  56     @Setup
  57     public void setup() {
  58         flag = 42;
  59         cachedException = new MyException();
  60         mh = MethodHandles.throwException(void.class, MyException.class);
  61     }
  62 
  63     @Benchmark
  64     public int baselineRaw() {
  65         try {
  66             throw new MyException();
  67         } catch (MyException my) {
  68             return my.getFlag();
  69         }
  70     }
  71 
  72     @Benchmark
  73     public int baselineRawCached() {
  74         try {
  75             throw cachedException;
  76         } catch (MyException my) {
  77             return my.getFlag();
  78         }
  79     }
  80 
  81     @Benchmark
  82     public int testInvoke() throws Throwable {
  83         try {
  84             mh.invoke(new MyException());
  85             throw new IllegalStateException("Should throw exception");
  86         } catch (MyException my) {
  87             return my.getFlag();
  88         }
  89     }
  90 
  91     @Benchmark
  92     public int testInvokeCached() throws Throwable {
  93         try {
  94             mh.invoke(cachedException);
  95             throw new IllegalStateException("Should throw exception");
  96         } catch (MyException my) {
  97             return my.getFlag();
  98         }
  99     }
 100 
 101     @Benchmark
 102     public MethodHandle interCreate() {
 103         return MethodHandles.throwException(void.class, MyException.class);
 104     }
 105 
 106     public class MyException extends Exception {
 107 
 108         public int getFlag() {
 109             return flag;
 110         }
 111     }
 112 
 113 }