|
| 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; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Random; |
| 23 | +import java.util.concurrent.TimeUnit; |
| 24 | + |
| 25 | +import org.openjdk.jmh.annotations.Benchmark; |
| 26 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 27 | +import org.openjdk.jmh.annotations.Fork; |
| 28 | +import org.openjdk.jmh.annotations.Level; |
| 29 | +import org.openjdk.jmh.annotations.Measurement; |
| 30 | +import org.openjdk.jmh.annotations.Mode; |
| 31 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 32 | +import org.openjdk.jmh.annotations.Param; |
| 33 | +import org.openjdk.jmh.annotations.Scope; |
| 34 | +import org.openjdk.jmh.annotations.Setup; |
| 35 | +import org.openjdk.jmh.annotations.State; |
| 36 | +import org.openjdk.jmh.annotations.Threads; |
| 37 | +import org.openjdk.jmh.annotations.Warmup; |
| 38 | +import org.openjdk.jmh.infra.Blackhole; |
| 39 | +import org.openjdk.jmh.runner.Runner; |
| 40 | +import org.openjdk.jmh.runner.options.Options; |
| 41 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 42 | + |
| 43 | +import opennlp.tools.stemmer.snowball.SnowballStemmer; |
| 44 | +import opennlp.tools.stemmer.snowball.SnowballStemmerFactory; |
| 45 | + |
| 46 | +/** |
| 47 | + * JMH benchmark for {@link CachingStemmer} against an uncached shared {@link SnowballStemmer}. |
| 48 | + * |
| 49 | + * <p>Two workloads drive both strategies:</p> |
| 50 | + * <ul> |
| 51 | + * <li>{@code zipf} — a 64k-token stream sampled with 1/rank weights from a 512-word |
| 52 | + * vocabulary. This models real text, where a small vocabulary dominates; the default |
| 53 | + * 1024-entry cache holds the whole vocabulary.</li> |
| 54 | + * <li>{@code diverse} — a 64k-token stream sampled uniformly from an 8192-word vocabulary, |
| 55 | + * 8x the cache capacity. This is the cache-hostile case: mostly misses plus constant |
| 56 | + * eviction, so it bounds the overhead the cache can add.</li> |
| 57 | + * </ul> |
| 58 | + * |
| 59 | + * <p>One op stems 16 consecutive tokens from the stream; each benchmark thread walks the stream |
| 60 | + * from its own cursor.</p> |
| 61 | + */ |
| 62 | +@BenchmarkMode(Mode.Throughput) |
| 63 | +@OutputTimeUnit(TimeUnit.SECONDS) |
| 64 | +@Warmup(iterations = 5, time = 2) |
| 65 | +@Measurement(iterations = 10, time = 2) |
| 66 | +@Fork(2) |
| 67 | +public class CachingStemmerBenchmark { |
| 68 | + |
| 69 | + private static final int STREAM_LENGTH = 65536; |
| 70 | + private static final int WORDS_PER_OP = 16; |
| 71 | + |
| 72 | + private static final String[] ROOTS = { |
| 73 | + "run", "walk", "talk", "develop", "nation", "connect", "form", "create", |
| 74 | + "act", "direct", "govern", "manage", "operate", "organize", "present", "relate", |
| 75 | + "report", "state", "structure", "test", "train", "transform", "translate", "value", |
| 76 | + "view", "wonder", "yield", "zone", "note", "mark", "place", "point" |
| 77 | + }; |
| 78 | + private static final String[] PREFIXES = { |
| 79 | + "", "re", "un", "over", "under", "out", "pre", "post", |
| 80 | + "non", "anti", "de", "dis", "mis", "sub", "super", "inter" |
| 81 | + }; |
| 82 | + private static final String[] SUFFIXES = { |
| 83 | + "", "s", "ed", "ing", "er", "ers", "ation", "ations", |
| 84 | + "ly", "ness", "ment", "ments", "ize", "ized", "izing", "al" |
| 85 | + }; |
| 86 | + |
| 87 | + @State(Scope.Benchmark) |
| 88 | + public static class WorkloadState { |
| 89 | + |
| 90 | + @Param({"zipf", "diverse"}) |
| 91 | + String workload; |
| 92 | + |
| 93 | + String[] stream; |
| 94 | + |
| 95 | + @Setup(Level.Trial) |
| 96 | + public void build() { |
| 97 | + Random random = new Random(42); |
| 98 | + List<String> vocabulary = new ArrayList<>(); |
| 99 | + if ("zipf".equals(workload)) { |
| 100 | + // 32 roots x 16 suffixes = 512 unique words, sampled with 1/rank weights. |
| 101 | + for (String root : ROOTS) { |
| 102 | + for (String suffix : SUFFIXES) { |
| 103 | + vocabulary.add(root + suffix); |
| 104 | + } |
| 105 | + } |
| 106 | + double[] cumulative = new double[vocabulary.size()]; |
| 107 | + double sum = 0; |
| 108 | + for (int rank = 0; rank < vocabulary.size(); rank++) { |
| 109 | + sum += 1.0 / (rank + 1); |
| 110 | + cumulative[rank] = sum; |
| 111 | + } |
| 112 | + stream = new String[STREAM_LENGTH]; |
| 113 | + for (int i = 0; i < STREAM_LENGTH; i++) { |
| 114 | + double r = random.nextDouble() * sum; |
| 115 | + int idx = 0; |
| 116 | + while (cumulative[idx] < r) { |
| 117 | + idx++; |
| 118 | + } |
| 119 | + stream[i] = vocabulary.get(idx); |
| 120 | + } |
| 121 | + } else { |
| 122 | + // 16 prefixes x 32 roots x 16 suffixes = 8192 unique words, sampled uniformly. |
| 123 | + for (String prefix : PREFIXES) { |
| 124 | + for (String root : ROOTS) { |
| 125 | + for (String suffix : SUFFIXES) { |
| 126 | + vocabulary.add(prefix + root + suffix); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + stream = new String[STREAM_LENGTH]; |
| 131 | + for (int i = 0; i < STREAM_LENGTH; i++) { |
| 132 | + stream[i] = vocabulary.get(random.nextInt(vocabulary.size())); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @State(Scope.Benchmark) |
| 139 | + public static class UncachedState { |
| 140 | + Stemmer stemmer; |
| 141 | + |
| 142 | + @Setup(Level.Trial) |
| 143 | + public void create() { |
| 144 | + stemmer = new SnowballStemmer(SnowballStemmer.ALGORITHM.ENGLISH); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + @State(Scope.Benchmark) |
| 149 | + public static class CachedState { |
| 150 | + Stemmer stemmer; |
| 151 | + |
| 152 | + @Setup(Level.Trial) |
| 153 | + public void create() { |
| 154 | + stemmer = new CachingStemmer( |
| 155 | + new SnowballStemmerFactory(SnowballStemmer.ALGORITHM.ENGLISH)); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + @State(Scope.Thread) |
| 160 | + public static class Cursor { |
| 161 | + int position; |
| 162 | + |
| 163 | + @Setup(Level.Trial) |
| 164 | + public void randomize() { |
| 165 | + position = new Random().nextInt(STREAM_LENGTH); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + @Benchmark |
| 170 | + @Threads(Threads.MAX) |
| 171 | + public void uncachedShared(WorkloadState w, UncachedState st, Cursor cursor, Blackhole bh) { |
| 172 | + for (int i = 0; i < WORDS_PER_OP; i++) { |
| 173 | + bh.consume(st.stemmer.stem(w.stream[cursor.position++ & (STREAM_LENGTH - 1)])); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + @Benchmark |
| 178 | + @Threads(Threads.MAX) |
| 179 | + public void cachedShared(WorkloadState w, CachedState st, Cursor cursor, Blackhole bh) { |
| 180 | + for (int i = 0; i < WORDS_PER_OP; i++) { |
| 181 | + bh.consume(st.stemmer.stem(w.stream[cursor.position++ & (STREAM_LENGTH - 1)])); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Quick local iteration only: {@code forks(0)} disables JVM fork isolation |
| 187 | + * (unlike {@code mvn} with the {@code jmh} profile). |
| 188 | + * Use the Maven-invoked configuration for publishable numbers. |
| 189 | + */ |
| 190 | + public static void main(String[] args) throws Exception { |
| 191 | + Options opt = new OptionsBuilder() |
| 192 | + .include(CachingStemmerBenchmark.class.getSimpleName()) |
| 193 | + .forks(0) |
| 194 | + .warmupIterations(3) |
| 195 | + .measurementIterations(5) |
| 196 | + .build(); |
| 197 | + new Runner(opt).run(); |
| 198 | + } |
| 199 | +} |
0 commit comments