|
| 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 | + |
| 18 | +package opennlp.tools.stemmer.snowball; |
| 19 | + |
| 20 | +import java.util.concurrent.TimeUnit; |
| 21 | + |
| 22 | +import org.openjdk.jmh.annotations.Benchmark; |
| 23 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 24 | +import org.openjdk.jmh.annotations.Fork; |
| 25 | +import org.openjdk.jmh.annotations.Level; |
| 26 | +import org.openjdk.jmh.annotations.Measurement; |
| 27 | +import org.openjdk.jmh.annotations.Mode; |
| 28 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 29 | +import org.openjdk.jmh.annotations.Scope; |
| 30 | +import org.openjdk.jmh.annotations.Setup; |
| 31 | +import org.openjdk.jmh.annotations.State; |
| 32 | +import org.openjdk.jmh.annotations.Threads; |
| 33 | +import org.openjdk.jmh.annotations.Warmup; |
| 34 | +import org.openjdk.jmh.infra.Blackhole; |
| 35 | +import org.openjdk.jmh.runner.Runner; |
| 36 | +import org.openjdk.jmh.runner.options.Options; |
| 37 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 38 | + |
| 39 | +import opennlp.tools.stemmer.Stemmer; |
| 40 | + |
| 41 | +/** |
| 42 | + * JMH benchmark for the thread-safe {@link SnowballStemmer} versus the pre-patch implementation |
| 43 | + * that held its generated engine in a plain field. |
| 44 | + * |
| 45 | + * <p>Three strategies are measured, all at {@link Threads#MAX}:</p> |
| 46 | + * <ul> |
| 47 | + * <li>{@code sharedInstance} — one thread-safe {@link SnowballStemmer} shared by every thread |
| 48 | + * (one thread runs on the owner fast path, the rest on {@link ThreadLocal} state)</li> |
| 49 | + * <li>{@code instancePerThread} — one thread-safe {@link SnowballStemmer} per thread (every |
| 50 | + * thread is the owner of its own instance, so this isolates the owner-fast-path cost)</li> |
| 51 | + * <li>{@code legacyInstancePerThread} — the old non-thread-safe implementation, one per thread |
| 52 | + * (the pre-patch baseline; sharing it across threads would be a correctness bug)</li> |
| 53 | + * </ul> |
| 54 | + */ |
| 55 | +@BenchmarkMode(Mode.Throughput) |
| 56 | +@OutputTimeUnit(TimeUnit.SECONDS) |
| 57 | +@Warmup(iterations = 5, time = 2) |
| 58 | +@Measurement(iterations = 10, time = 2) |
| 59 | +@Fork(2) |
| 60 | +public class SnowballStemmerBenchmark { |
| 61 | + |
| 62 | + private static final String[] INPUT = { |
| 63 | + "running", "accompanying", "malediction", "softeners", "declining", |
| 64 | + "conspiracies", "monotonically", "annotations", "internationalization", |
| 65 | + "denormalization", "photographers", "responsibilities", "acknowledgement", |
| 66 | + "this", "cat", "querying" |
| 67 | + }; |
| 68 | + |
| 69 | + /** |
| 70 | + * Replica of the pre-patch {@code SnowballStemmer}: the generated engine lives in a plain |
| 71 | + * field, so an instance must not be shared across threads. |
| 72 | + */ |
| 73 | + static final class LegacySnowballStemmer implements Stemmer { |
| 74 | + |
| 75 | + private final AbstractSnowballStemmer stemmer = new englishStemmer(); |
| 76 | + |
| 77 | + @Override |
| 78 | + public CharSequence stem(CharSequence word) { |
| 79 | + stemmer.setCurrent(word.toString()); |
| 80 | + stemmer.stem(); |
| 81 | + return stemmer.getCurrent(); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @State(Scope.Benchmark) |
| 86 | + public static class SharedState { |
| 87 | + Stemmer stemmer; |
| 88 | + |
| 89 | + @Setup(Level.Trial) |
| 90 | + public void create() { |
| 91 | + stemmer = new SnowballStemmer(SnowballStemmer.ALGORITHM.ENGLISH); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @State(Scope.Thread) |
| 96 | + public static class PerThreadState { |
| 97 | + Stemmer stemmer; |
| 98 | + |
| 99 | + @Setup(Level.Trial) |
| 100 | + public void create() { |
| 101 | + stemmer = new SnowballStemmer(SnowballStemmer.ALGORITHM.ENGLISH); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @State(Scope.Thread) |
| 106 | + public static class LegacyPerThreadState { |
| 107 | + Stemmer stemmer; |
| 108 | + |
| 109 | + @Setup(Level.Trial) |
| 110 | + public void create() { |
| 111 | + stemmer = new LegacySnowballStemmer(); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Benchmark |
| 116 | + @Threads(Threads.MAX) |
| 117 | + public void sharedInstance(SharedState st, Blackhole bh) { |
| 118 | + for (String s : INPUT) { |
| 119 | + bh.consume(st.stemmer.stem(s)); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @Benchmark |
| 124 | + @Threads(Threads.MAX) |
| 125 | + public void instancePerThread(PerThreadState pt, Blackhole bh) { |
| 126 | + for (String s : INPUT) { |
| 127 | + bh.consume(pt.stemmer.stem(s)); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + @Benchmark |
| 132 | + @Threads(Threads.MAX) |
| 133 | + public void legacyInstancePerThread(LegacyPerThreadState pt, Blackhole bh) { |
| 134 | + for (String s : INPUT) { |
| 135 | + bh.consume(pt.stemmer.stem(s)); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Quick local iteration only: {@code forks(0)} disables JVM fork isolation |
| 141 | + * (unlike {@code mvn} with the {@code jmh} profile). |
| 142 | + * Use the Maven-invoked configuration for publishable numbers. |
| 143 | + */ |
| 144 | + public static void main(String[] args) throws Exception { |
| 145 | + Options opt = new OptionsBuilder() |
| 146 | + .include(SnowballStemmerBenchmark.class.getSimpleName()) |
| 147 | + .forks(0) |
| 148 | + .warmupIterations(3) |
| 149 | + .measurementIterations(5) |
| 150 | + .build(); |
| 151 | + new Runner(opt).run(); |
| 152 | + } |
| 153 | +} |
0 commit comments