test/micro/org/openjdk/bench/vm/lang/ThrowableRuntimeMicros.java
changeset 58347 ac24594d2c8c
equal deleted inserted replaced
58344:d29f0181ba42 58347:ac24594d2c8c
       
     1 /*
       
     2  * Copyright (c) 2015, 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.vm.lang;
       
    24 
       
    25 import java.util.concurrent.TimeUnit;
       
    26 import org.openjdk.jmh.annotations.Benchmark;
       
    27 import org.openjdk.jmh.annotations.BenchmarkMode;
       
    28 import org.openjdk.jmh.annotations.Mode;
       
    29 import org.openjdk.jmh.annotations.OutputTimeUnit;
       
    30 import org.openjdk.jmh.annotations.Param;
       
    31 import org.openjdk.jmh.annotations.Scope;
       
    32 import org.openjdk.jmh.annotations.State;
       
    33 import org.openjdk.jmh.infra.Blackhole;
       
    34 
       
    35 @State(value = Scope.Benchmark)
       
    36 @BenchmarkMode(Mode.AverageTime)
       
    37 @OutputTimeUnit(TimeUnit.NANOSECONDS)
       
    38 public class ThrowableRuntimeMicros {
       
    39 
       
    40     // TestStack will add this number of calls to the call stack
       
    41     @Param({"4", "100", "1000"})
       
    42     // For more thorough testing, consider:
       
    43     // @Param({"4", "10", "100", "256", "1000"})
       
    44     public int depth;
       
    45 
       
    46     /** Build a call stack of a given size, then run trigger code in it.
       
    47       * (Does not account for existing frames higher up in the JMH machinery).
       
    48       */
       
    49     static class TestStack {
       
    50         final long fence;
       
    51         long current;
       
    52         final Runnable trigger;
       
    53 
       
    54         TestStack(long max, Runnable trigger) {
       
    55             this.fence = max;
       
    56             this.current = 0;
       
    57             this.trigger = trigger;
       
    58         }
       
    59 
       
    60         public void start() {
       
    61             one();
       
    62         }
       
    63 
       
    64         public void one() {
       
    65             if (check()) {
       
    66                 two();
       
    67             }
       
    68         }
       
    69 
       
    70         void two() {
       
    71             if (check()) {
       
    72                 three();
       
    73             }
       
    74         }
       
    75 
       
    76         private void three() {
       
    77             if (check()) {
       
    78                 one();
       
    79             }
       
    80         }
       
    81 
       
    82         boolean check() {
       
    83             if (++current == fence) {
       
    84                 trigger.run();
       
    85                 return false;
       
    86             } else {
       
    87                 return true;
       
    88             }
       
    89         }
       
    90     }
       
    91 
       
    92     @Benchmark
       
    93     public void testThrowableInit(Blackhole bh) {
       
    94         final Blackhole localBH = bh;
       
    95         final boolean[] done = {false};
       
    96         new TestStack(depth, new Runnable() {
       
    97             public void run() {
       
    98                 localBH.consume(new Throwable());
       
    99                 done[0] = true;
       
   100             }
       
   101         }).start();
       
   102         if (!done[0]) {
       
   103             throw new RuntimeException();
       
   104         }
       
   105     }
       
   106 
       
   107     @Benchmark
       
   108     public void testThrowableGetStackTrace(Blackhole bh) {
       
   109         final Blackhole localBH = bh;
       
   110         final boolean[] done = {false};
       
   111         new TestStack(depth, new Runnable() {
       
   112             public void run() {
       
   113                 localBH.consume(new Throwable().getStackTrace());
       
   114                 done[0] = true;
       
   115             }
       
   116         }).start();
       
   117         if (!done[0]) {
       
   118             throw new RuntimeException();
       
   119         }
       
   120     }
       
   121 
       
   122     @Benchmark
       
   123     public void testThrowableSTEtoString(Blackhole bh) {
       
   124         final Blackhole localBH = bh;
       
   125         final boolean[] done = {false};
       
   126         new TestStack(depth, new Runnable() {
       
   127             public void run() {
       
   128                 Throwable t = new Throwable();
       
   129                 for (StackTraceElement ste : t.getStackTrace()) {
       
   130                     localBH.consume(ste.toString());
       
   131                 }
       
   132                 done[0] = true;
       
   133             }
       
   134         }).start();
       
   135         if (!done[0]) {
       
   136             throw new RuntimeException();
       
   137         }
       
   138     }
       
   139 }