|
| 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. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | +package net.java.btrace; |
| 26 | + |
| 27 | +import java.util.concurrent.TimeUnit; |
| 28 | +import org.openjdk.jmh.annotations.Benchmark; |
| 29 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 30 | +import org.openjdk.jmh.annotations.Fork; |
| 31 | +import org.openjdk.jmh.annotations.Level; |
| 32 | +import org.openjdk.jmh.annotations.Measurement; |
| 33 | +import org.openjdk.jmh.annotations.Mode; |
| 34 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 35 | +import org.openjdk.jmh.annotations.Scope; |
| 36 | +import org.openjdk.jmh.annotations.Setup; |
| 37 | +import org.openjdk.jmh.annotations.State; |
| 38 | +import org.openjdk.jmh.annotations.Warmup; |
| 39 | +import org.openjdk.jmh.profile.ProfilerFactory; |
| 40 | +import org.openjdk.jmh.runner.Runner; |
| 41 | +import org.openjdk.jmh.runner.options.Options; |
| 42 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 43 | + |
| 44 | +@State(Scope.Thread) |
| 45 | +@OutputTimeUnit(TimeUnit.MICROSECONDS) |
| 46 | +@Fork(1) |
| 47 | +@BenchmarkMode(Mode.AverageTime) |
| 48 | +public class StringOpBenchmarks { |
| 49 | + private static final String STRING_PART = "h"; |
| 50 | + |
| 51 | + StringBuilder sb; |
| 52 | + String st; |
| 53 | + String res; |
| 54 | + |
| 55 | + @Setup |
| 56 | + public void setup() { |
| 57 | + st = ""; |
| 58 | + } |
| 59 | + |
| 60 | + @Setup(Level.Invocation) |
| 61 | + public void setupEach() { |
| 62 | + sb = new StringBuilder(); |
| 63 | + } |
| 64 | + |
| 65 | + @Warmup(iterations = 5, time = 200, timeUnit = TimeUnit.MILLISECONDS) |
| 66 | + @Measurement(iterations = 5, time = 1200, timeUnit = TimeUnit.MILLISECONDS) |
| 67 | + @Benchmark |
| 68 | + public void testStringBuilder() { |
| 69 | + sb.append(STRING_PART).append(STRING_PART); |
| 70 | + } |
| 71 | + |
| 72 | + @Warmup(iterations = 5, time = 200, timeUnit = TimeUnit.MILLISECONDS) |
| 73 | + @Measurement(iterations = 5, time = 1200, timeUnit = TimeUnit.MILLISECONDS) |
| 74 | + @Benchmark |
| 75 | + public void testStringPlus() { |
| 76 | + res = st + STRING_PART + STRING_PART; |
| 77 | + } |
| 78 | + |
| 79 | + @Warmup(iterations = 5, time = 200, timeUnit = TimeUnit.MILLISECONDS) |
| 80 | + @Measurement(iterations = 5, time = 1200, timeUnit = TimeUnit.MILLISECONDS) |
| 81 | + @Benchmark |
| 82 | + public void testStrCat() { |
| 83 | + res = st.concat(STRING_PART).concat(STRING_PART); |
| 84 | + } |
| 85 | + |
| 86 | + public static void main(String[] args) throws Exception { |
| 87 | + Options opt = new OptionsBuilder() |
| 88 | + .addProfiler(ProfilerFactory.getProfilerByName("gc")) |
| 89 | + .include(".*" + StringOpBenchmarks.class.getSimpleName() + ".*test.*") |
| 90 | + .build(); |
| 91 | + |
| 92 | + new Runner(opt).run(); |
| 93 | + } |
| 94 | +} |
0 commit comments