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.vm.lang;
  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.State;
  31 import org.openjdk.jmh.infra.Blackhole;
  32 
  33 import java.util.concurrent.TimeUnit;
  34 
  35 /**
  36  * Tests throwing exceptions.
  37  */
  38 @BenchmarkMode(Mode.AverageTime)
  39 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  40 @State(Scope.Thread)
  41 public class Throw {
  42 
  43     public static boolean alwaysTrue = true;
  44     private static Object nullObject = null;
  45     public Object useObject = new Object();
  46 
  47     @Benchmark
  48     public void throwSyncException(Blackhole bh) {
  49         try {
  50             throwingMethod();
  51         } catch (Exception ex) {
  52             bh.consume(useObject);
  53         }
  54     }
  55 
  56     @Benchmark
  57     public void throwASyncException(Blackhole bh) {
  58         try {
  59             throwNullpointer();
  60         } catch (Exception ex) {
  61             bh.consume(useObject);
  62         }
  63     }
  64 
  65     @Benchmark
  66     public void throwSyncExceptionUseException(Blackhole bh) {
  67         try {
  68             throwingMethod();
  69         } catch (Exception ex) {
  70             bh.consume(ex);
  71         }
  72     }
  73 
  74     @Benchmark
  75     public void throwSyncExceptionUseMessage(Blackhole bh) {
  76         try {
  77             throwingMethod();
  78         } catch (Exception ex) {
  79             bh.consume(ex.getMessage());
  80         }
  81     }
  82 
  83     @Benchmark
  84     public void throwSyncExceptionUseStacktrace(Blackhole bh) {
  85         try {
  86             throwingMethod();
  87         } catch (Exception ex) {
  88             bh.consume(ex.getStackTrace());
  89         }
  90     }
  91 
  92     @Benchmark
  93     public void throwWith16Frames(Blackhole bh) {
  94         try {
  95             throwingMethod(16);
  96         } catch (Exception ex) {
  97             bh.consume(useObject);
  98         }
  99     }
 100 
 101     @Benchmark
 102     public void throwWith32Frames(Blackhole bh) {
 103         try {
 104             throwingMethod(32);
 105         } catch (Exception ex) {
 106             bh.consume(useObject);
 107         }
 108     }
 109 
 110     @Benchmark
 111     public void throwWith64Frames(Blackhole bh) {
 112         try {
 113             throwingMethod(64);
 114         } catch (Exception ex) {
 115             bh.consume(useObject);
 116         }
 117     }
 118 
 119     public void throwingMethod() throws Exception {
 120         if (alwaysTrue) {
 121             throw new Exception();
 122         }
 123     }
 124 
 125     public void throwingMethod(int i) throws Exception {
 126         if (i == 0) {
 127             throw new Exception();
 128         }
 129         throwingMethod(i - 1);
 130     }
 131 
 132     public void throwNullpointer() {
 133         nullObject.hashCode();
 134     }
 135 }