|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, 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; |
| 24 | + |
| 25 | +import java.io.FilterOutputStream; |
| 26 | +import java.io.IOException; |
| 27 | +import java.io.OutputStream; |
| 28 | +import java.io.PrintStream; |
| 29 | +import java.io.PrintWriter; |
| 30 | +import java.io.StringWriter; |
| 31 | +import java.util.concurrent.TimeUnit; |
| 32 | +import java.util.stream.IntStream; |
| 33 | +import org.openjdk.jmh.annotations.Benchmark; |
| 34 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 35 | +import org.openjdk.jmh.annotations.Fork; |
| 36 | +import org.openjdk.jmh.annotations.Measurement; |
| 37 | +import org.openjdk.jmh.annotations.Mode; |
| 38 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 39 | +import org.openjdk.jmh.annotations.Param; |
| 40 | +import org.openjdk.jmh.annotations.Scope; |
| 41 | +import org.openjdk.jmh.annotations.Setup; |
| 42 | +import org.openjdk.jmh.annotations.State; |
| 43 | +import org.openjdk.jmh.annotations.Warmup; |
| 44 | + |
| 45 | +/** |
| 46 | + * Benchmarks for java.lang.StackTraceElement |
| 47 | + */ |
| 48 | +@State(value = Scope.Benchmark) |
| 49 | +@BenchmarkMode(Mode.AverageTime) |
| 50 | +@OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 51 | +@Warmup(iterations = 5, time = 1) |
| 52 | +@Measurement(iterations = 5, time = 1) |
| 53 | +@Fork(3) |
| 54 | +public class StackTraceElementBench { |
| 55 | + |
| 56 | + private StackTraceElement element; |
| 57 | + private Throwable throwable; |
| 58 | + |
| 59 | + @Setup |
| 60 | + public void setup() { |
| 61 | + StackTraceElement[] elements = IntStream.range(0, 500) |
| 62 | + .mapToObj(i -> new StackTraceElement( |
| 63 | + "classloader" + i, |
| 64 | + "module" + i, |
| 65 | + "version" + i, |
| 66 | + "class" + i, |
| 67 | + "method" + i, |
| 68 | + "file" + i + ".java", |
| 69 | + i)) |
| 70 | + .toArray(StackTraceElement[]::new); |
| 71 | + this.element = elements[0]; |
| 72 | + this.throwable = new RuntimeException("benchmark") { |
| 73 | + @Override |
| 74 | + public synchronized Throwable fillInStackTrace() { |
| 75 | + setStackTrace(elements); |
| 76 | + return this; |
| 77 | + } |
| 78 | + }; |
| 79 | + } |
| 80 | + |
| 81 | + @Benchmark |
| 82 | + public String toString() { |
| 83 | + return element.toString(); |
| 84 | + } |
| 85 | + |
| 86 | + @Benchmark |
| 87 | + public String printStackTrace() { |
| 88 | + StringWriter sw = new StringWriter(); |
| 89 | + throwable.printStackTrace(new PrintWriter(sw)); |
| 90 | + return sw.toString(); |
| 91 | + } |
| 92 | +} |
0 commit comments