|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package opennlp.tools.util.jvm; |
| 18 | + |
| 19 | +import org.openjdk.jmh.annotations.Benchmark; |
| 20 | +import org.openjdk.jmh.annotations.Param; |
| 21 | +import org.openjdk.jmh.annotations.Scope; |
| 22 | +import org.openjdk.jmh.annotations.Setup; |
| 23 | +import org.openjdk.jmh.annotations.State; |
| 24 | +import org.openjdk.jmh.infra.Blackhole; |
| 25 | + |
| 26 | +/** |
| 27 | + * A benchmark class / setup by Aleksey Shipilëv. |
| 28 | + * Resides here to investigate performance of String deduplication approaches |
| 29 | + * on different environments. |
| 30 | + * <p> |
| 31 | + * Origin: |
| 32 | + * <a href="https://shipilev.net/jvm/anatomy-quarks/10-string-intern/"> |
| 33 | + * https://shipilev.net/jvm/anatomy-quarks/10-string-intern/</a> |
| 34 | + * <p> |
| 35 | + * His conclusion:<br> |
| 36 | + * "Do not use String.intern() without thinking very hard about it, okay?" |
| 37 | + */ |
| 38 | +@State(Scope.Benchmark) |
| 39 | +public class StringDeduplicationBenchmark { |
| 40 | + |
| 41 | + @Param({"1", "100", "10000", "1000000"}) |
| 42 | + private int size; |
| 43 | + |
| 44 | + private JvmStringInterner str; |
| 45 | + private CHMStringInterner chm; |
| 46 | + private HMStringInterner hm; |
| 47 | + private CHMStringDeduplicator chmd05; |
| 48 | + private NoOpStringInterner noop; |
| 49 | + |
| 50 | + @Setup |
| 51 | + public void setup() { |
| 52 | + str = new JvmStringInterner(); |
| 53 | + chm = new CHMStringInterner(); |
| 54 | + hm = new HMStringInterner(); |
| 55 | + chmd05 = new CHMStringDeduplicator(); |
| 56 | + noop = new NoOpStringInterner(); |
| 57 | + } |
| 58 | + |
| 59 | + @Benchmark |
| 60 | + public void intern(Blackhole bh) { |
| 61 | + for (int c = 0; c < size; c++) { |
| 62 | + bh.consume(str.intern("String" + c)); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Benchmark |
| 67 | + public void chm(Blackhole bh) { |
| 68 | + for (int c = 0; c < size; c++) { |
| 69 | + bh.consume(chm.intern("String" + c)); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Benchmark |
| 74 | + public void hm(Blackhole bh) { |
| 75 | + for (int c = 0; c < size; c++) { |
| 76 | + bh.consume(hm.intern("String" + c)); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Benchmark |
| 81 | + public void chmd05(Blackhole bh) { |
| 82 | + for (int c = 0; c < size; c++) { |
| 83 | + bh.consume(chmd05.intern("String" + c)); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Benchmark |
| 88 | + public void noop(Blackhole bh) { |
| 89 | + for (int c = 0; c < size; c++) { |
| 90 | + bh.consume(noop.intern("String" + c)); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments