Path: blob/master/test/micro/org/openjdk/bench/vm/lang/ThrowableRuntimeMicros.java
41161 views
/*1* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package org.openjdk.bench.vm.lang;2324import java.util.concurrent.TimeUnit;25import org.openjdk.jmh.annotations.Benchmark;26import org.openjdk.jmh.annotations.BenchmarkMode;27import org.openjdk.jmh.annotations.Mode;28import org.openjdk.jmh.annotations.OutputTimeUnit;29import org.openjdk.jmh.annotations.Param;30import org.openjdk.jmh.annotations.Scope;31import org.openjdk.jmh.annotations.State;32import org.openjdk.jmh.infra.Blackhole;3334@State(value = Scope.Benchmark)35@BenchmarkMode(Mode.AverageTime)36@OutputTimeUnit(TimeUnit.NANOSECONDS)37public class ThrowableRuntimeMicros {3839// TestStack will add this number of calls to the call stack40@Param({"4", "100", "1000"})41// For more thorough testing, consider:42// @Param({"4", "10", "100", "256", "1000"})43public int depth;4445/** Build a call stack of a given size, then run trigger code in it.46* (Does not account for existing frames higher up in the JMH machinery).47*/48static class TestStack {49final long fence;50long current;51final Runnable trigger;5253TestStack(long max, Runnable trigger) {54this.fence = max;55this.current = 0;56this.trigger = trigger;57}5859public void start() {60one();61}6263public void one() {64if (check()) {65two();66}67}6869void two() {70if (check()) {71three();72}73}7475private void three() {76if (check()) {77one();78}79}8081boolean check() {82if (++current == fence) {83trigger.run();84return false;85} else {86return true;87}88}89}9091@Benchmark92public void testThrowableInit(Blackhole bh) {93final Blackhole localBH = bh;94final boolean[] done = {false};95new TestStack(depth, new Runnable() {96public void run() {97localBH.consume(new Throwable());98done[0] = true;99}100}).start();101if (!done[0]) {102throw new RuntimeException();103}104}105106@Benchmark107public void testThrowableGetStackTrace(Blackhole bh) {108final Blackhole localBH = bh;109final boolean[] done = {false};110new TestStack(depth, new Runnable() {111public void run() {112localBH.consume(new Throwable().getStackTrace());113done[0] = true;114}115}).start();116if (!done[0]) {117throw new RuntimeException();118}119}120121@Benchmark122public void testThrowableSTEtoString(Blackhole bh) {123final Blackhole localBH = bh;124final boolean[] done = {false};125new TestStack(depth, new Runnable() {126public void run() {127Throwable t = new Throwable();128for (StackTraceElement ste : t.getStackTrace()) {129localBH.consume(ste.toString());130}131done[0] = true;132}133}).start();134if (!done[0]) {135throw new RuntimeException();136}137}138}139140141