|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | +package org.elasticsearch.benchmark.bytes; |
| 10 | + |
| 11 | +import org.elasticsearch.common.bytes.BytesArray; |
| 12 | +import org.elasticsearch.common.logging.LogConfigurator; |
| 13 | +import org.openjdk.jmh.annotations.Benchmark; |
| 14 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 15 | +import org.openjdk.jmh.annotations.Fork; |
| 16 | +import org.openjdk.jmh.annotations.Measurement; |
| 17 | +import org.openjdk.jmh.annotations.Mode; |
| 18 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 19 | +import org.openjdk.jmh.annotations.Param; |
| 20 | +import org.openjdk.jmh.annotations.Scope; |
| 21 | +import org.openjdk.jmh.annotations.Setup; |
| 22 | +import org.openjdk.jmh.annotations.State; |
| 23 | +import org.openjdk.jmh.annotations.Warmup; |
| 24 | + |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | + |
| 27 | +@Warmup(iterations = 4, time = 1) |
| 28 | +@Measurement(iterations = 5, time = 1) |
| 29 | +@BenchmarkMode(Mode.Throughput) |
| 30 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 31 | +@State(Scope.Thread) |
| 32 | +@Fork(value = 1) |
| 33 | +public class BytesArrayIndexOfBenchmark { |
| 34 | + |
| 35 | + static { |
| 36 | + LogConfigurator.configureESLogging(); // native access requires logging to be initialized |
| 37 | + } |
| 38 | + |
| 39 | + static final byte MARKER = (byte) '\n'; |
| 40 | + |
| 41 | + @Param(value = { "64", "127", "128", "4096", "16384", "65536", "1048576" }) |
| 42 | + public int size; |
| 43 | + |
| 44 | + BytesArray bytesArray; |
| 45 | + |
| 46 | + @Setup |
| 47 | + public void setup() { |
| 48 | + byte[] bytes = new byte[size]; |
| 49 | + bytes[bytes.length - 1] = MARKER; |
| 50 | + bytesArray = new BytesArray(bytes, 0, bytes.length); |
| 51 | + } |
| 52 | + |
| 53 | + @Benchmark |
| 54 | + public int indexOf() { |
| 55 | + return bytesArray.indexOf(MARKER, 0); |
| 56 | + } |
| 57 | + |
| 58 | + @Benchmark |
| 59 | + @Fork(jvmArgsPrepend = { "--add-modules=jdk.incubator.vector" }) |
| 60 | + public int indexOfPanama() { |
| 61 | + return bytesArray.indexOf(MARKER, 0); |
| 62 | + } |
| 63 | + |
| 64 | + @Benchmark |
| 65 | + public int withOffsetIndexOf() { |
| 66 | + return bytesArray.indexOf(MARKER, 1); |
| 67 | + } |
| 68 | + |
| 69 | + @Benchmark |
| 70 | + @Fork(jvmArgsPrepend = { "--add-modules=jdk.incubator.vector" }) |
| 71 | + public int withOffsetIndexPanama() { |
| 72 | + return bytesArray.indexOf(MARKER, 1); |
| 73 | + } |
| 74 | +} |
0 commit comments