Path: blob/master/test/micro/org/openjdk/bench/vm/lang/Throw.java
41161 views
/*1* Copyright (c) 2014, 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 org.openjdk.jmh.annotations.Benchmark;25import org.openjdk.jmh.annotations.BenchmarkMode;26import org.openjdk.jmh.annotations.Mode;27import org.openjdk.jmh.annotations.OutputTimeUnit;28import org.openjdk.jmh.annotations.Scope;29import org.openjdk.jmh.annotations.State;30import org.openjdk.jmh.infra.Blackhole;3132import java.util.concurrent.TimeUnit;3334/**35* Tests throwing exceptions.36*/37@BenchmarkMode(Mode.AverageTime)38@OutputTimeUnit(TimeUnit.NANOSECONDS)39@State(Scope.Thread)40public class Throw {4142public static boolean alwaysTrue = true;43private static Object nullObject = null;44public Object useObject = new Object();4546@Benchmark47public void throwSyncException(Blackhole bh) {48try {49throwingMethod();50} catch (Exception ex) {51bh.consume(useObject);52}53}5455@Benchmark56public void throwASyncException(Blackhole bh) {57try {58throwNullpointer();59} catch (Exception ex) {60bh.consume(useObject);61}62}6364@Benchmark65public void throwSyncExceptionUseException(Blackhole bh) {66try {67throwingMethod();68} catch (Exception ex) {69bh.consume(ex);70}71}7273@Benchmark74public void throwSyncExceptionUseMessage(Blackhole bh) {75try {76throwingMethod();77} catch (Exception ex) {78bh.consume(ex.getMessage());79}80}8182@Benchmark83public void throwSyncExceptionUseStacktrace(Blackhole bh) {84try {85throwingMethod();86} catch (Exception ex) {87bh.consume(ex.getStackTrace());88}89}9091@Benchmark92public void throwWith16Frames(Blackhole bh) {93try {94throwingMethod(16);95} catch (Exception ex) {96bh.consume(useObject);97}98}99100@Benchmark101public void throwWith32Frames(Blackhole bh) {102try {103throwingMethod(32);104} catch (Exception ex) {105bh.consume(useObject);106}107}108109@Benchmark110public void throwWith64Frames(Blackhole bh) {111try {112throwingMethod(64);113} catch (Exception ex) {114bh.consume(useObject);115}116}117118public void throwingMethod() throws Exception {119if (alwaysTrue) {120throw new Exception();121}122}123124public void throwingMethod(int i) throws Exception {125if (i == 0) {126throw new Exception();127}128throwingMethod(i - 1);129}130131public void throwNullpointer() {132nullObject.hashCode();133}134}135136137