|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.logstash.benchmark; |
| 21 | + |
| 22 | +import org.logstash.common.BufferedTokenizer; |
| 23 | +import org.openjdk.jmh.annotations.Benchmark; |
| 24 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 25 | +import org.openjdk.jmh.annotations.Fork; |
| 26 | +import org.openjdk.jmh.annotations.Level; |
| 27 | +import org.openjdk.jmh.annotations.Measurement; |
| 28 | +import org.openjdk.jmh.annotations.Mode; |
| 29 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 30 | +import org.openjdk.jmh.annotations.Scope; |
| 31 | +import org.openjdk.jmh.annotations.Setup; |
| 32 | +import org.openjdk.jmh.annotations.State; |
| 33 | +import org.openjdk.jmh.annotations.Warmup; |
| 34 | +import org.openjdk.jmh.infra.Blackhole; |
| 35 | + |
| 36 | +import java.util.concurrent.TimeUnit; |
| 37 | + |
| 38 | + |
| 39 | +@Warmup(iterations = 3, time = 100, timeUnit = TimeUnit.MILLISECONDS) |
| 40 | +@Measurement(iterations = 10, time = 3000, timeUnit = TimeUnit.MILLISECONDS) |
| 41 | +@Fork(1) |
| 42 | +@BenchmarkMode(Mode.Throughput) |
| 43 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 44 | +@State(Scope.Thread) |
| 45 | +public class BufferedTokenizerBenchmark { |
| 46 | + |
| 47 | + private BufferedTokenizer sut; |
| 48 | + private String singleTokenPerFragment; |
| 49 | + private String multipleTokensPerFragment; |
| 50 | + private String multipleTokensSpreadMultipleFragments_1; |
| 51 | + private String multipleTokensSpreadMultipleFragments_2; |
| 52 | + private String multipleTokensSpreadMultipleFragments_3; |
| 53 | + |
| 54 | + @Setup(Level.Invocation) |
| 55 | + public void setUp() { |
| 56 | + sut = new BufferedTokenizer(); |
| 57 | + singleTokenPerFragment = "a".repeat(512) + "\n"; |
| 58 | + |
| 59 | + multipleTokensPerFragment = "a".repeat(512) + "\n" + "b".repeat(512) + "\n" + "c".repeat(512) + "\n"; |
| 60 | + |
| 61 | + multipleTokensSpreadMultipleFragments_1 = "a".repeat(512) + "\n" + "b".repeat(512) + "\n" + "c".repeat(256); |
| 62 | + multipleTokensSpreadMultipleFragments_2 = "c".repeat(256) + "\n" + "d".repeat(512) + "\n" + "e".repeat(256); |
| 63 | + multipleTokensSpreadMultipleFragments_3 = "f".repeat(256) + "\n" + "g".repeat(512) + "\n" + "h".repeat(512) + "\n"; |
| 64 | + } |
| 65 | + |
| 66 | + @Benchmark |
| 67 | + public final void onlyOneTokenPerFragment(Blackhole blackhole) { |
| 68 | + Iterable<String> tokens = sut.extract(singleTokenPerFragment); |
| 69 | + tokens.forEach(blackhole::consume); |
| 70 | + blackhole.consume(tokens); |
| 71 | + } |
| 72 | + |
| 73 | + @Benchmark |
| 74 | + public final void multipleTokenPerFragment(Blackhole blackhole) { |
| 75 | + Iterable<String> tokens = sut.extract(multipleTokensPerFragment); |
| 76 | + tokens.forEach(blackhole::consume); |
| 77 | + blackhole.consume(tokens); |
| 78 | + } |
| 79 | + |
| 80 | + @Benchmark |
| 81 | + public final void multipleTokensCrossingMultipleFragments(Blackhole blackhole) { |
| 82 | + Iterable<String> tokens = sut.extract(multipleTokensSpreadMultipleFragments_1); |
| 83 | + tokens.forEach(t -> {}); |
| 84 | + blackhole.consume(tokens); |
| 85 | + |
| 86 | + tokens = sut.extract(multipleTokensSpreadMultipleFragments_2); |
| 87 | + tokens.forEach(t -> {}); |
| 88 | + blackhole.consume(tokens); |
| 89 | + |
| 90 | + tokens = sut.extract(multipleTokensSpreadMultipleFragments_3); |
| 91 | + tokens.forEach(blackhole::consume); |
| 92 | + blackhole.consume(tokens); |
| 93 | + } |
| 94 | +} |
0 commit comments