|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. 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, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.parquet.variant; |
| 20 | + |
| 21 | +import static java.util.concurrent.TimeUnit.MICROSECONDS; |
| 22 | + |
| 23 | +import java.nio.charset.StandardCharsets; |
| 24 | +import java.util.Random; |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | +import org.apache.parquet.io.api.Binary; |
| 27 | +import org.openjdk.jmh.annotations.Benchmark; |
| 28 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 29 | +import org.openjdk.jmh.annotations.Fork; |
| 30 | +import org.openjdk.jmh.annotations.Measurement; |
| 31 | +import org.openjdk.jmh.annotations.Mode; |
| 32 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 33 | +import org.openjdk.jmh.annotations.Param; |
| 34 | +import org.openjdk.jmh.annotations.Scope; |
| 35 | +import org.openjdk.jmh.annotations.Setup; |
| 36 | +import org.openjdk.jmh.annotations.State; |
| 37 | +import org.openjdk.jmh.annotations.Timeout; |
| 38 | +import org.openjdk.jmh.annotations.Warmup; |
| 39 | +import org.openjdk.jmh.infra.Blackhole; |
| 40 | +import org.slf4j.Logger; |
| 41 | +import org.slf4j.LoggerFactory; |
| 42 | + |
| 43 | +/** |
| 44 | + * Benchmark {@link VariantConverters}. These converters are used |
| 45 | + * when reconstructing shredded variants, so their performance |
| 46 | + * and memory consumption is on the critical path of queries reading |
| 47 | + * variants. |
| 48 | + * <p>Run: |
| 49 | + * <pre> |
| 50 | + * ./run.sh all org.apache.parquet.variant.VariantConverterBenchmark \ |
| 51 | + * -f 1 -foe true -rf json -rff target/results.json |
| 52 | + * </pre> |
| 53 | + * <p>Profile |
| 54 | + * <pre> |
| 55 | + * java -jar target/parquet-benchmarks.jar VariantProjectionBenchmark \ |
| 56 | + * -prof "async:output=flamegraph;dir=target/perf" -rf json -rff target/results.json |
| 57 | + * </pre> |
| 58 | + * */ |
| 59 | +@Fork(1) |
| 60 | +@State(Scope.Benchmark) |
| 61 | +@Warmup(iterations = 100) |
| 62 | +@Measurement(iterations = 500) |
| 63 | +@BenchmarkMode(Mode.SingleShotTime) |
| 64 | +@OutputTimeUnit(MICROSECONDS) |
| 65 | +@Timeout(time = 10, timeUnit = TimeUnit.MINUTES) |
| 66 | +public class VariantConverterBenchmark { |
| 67 | + |
| 68 | + private static final Logger LOG = LoggerFactory.getLogger(VariantConverterBenchmark.class); |
| 69 | + |
| 70 | + /** Number of iterations within each benchmark. |
| 71 | + * This compensates for the low resolution of ARM CPUs, |
| 72 | + * while also ensuring profile graphs are filled out in detail. */ |
| 73 | + private static final int INNER_ITERATIONS = 100_000; |
| 74 | + |
| 75 | + /** How long is the string to convert? */ |
| 76 | + @Param({"16", "512", "2048"}) |
| 77 | + int stringLength; |
| 78 | + |
| 79 | + /** |
| 80 | + * An input string, created in setup from random ASCII characters of length {@link #stringLength} |
| 81 | + */ |
| 82 | + private String inputString; |
| 83 | + |
| 84 | + /** {@link #inputString} as a binary. */ |
| 85 | + private Binary inputBinary; |
| 86 | + |
| 87 | + @Setup |
| 88 | + public void setup() { |
| 89 | + byte[] bytes = new byte[stringLength]; |
| 90 | + Random random = new Random(42); |
| 91 | + for (int i = 0; i < stringLength; i++) { |
| 92 | + bytes[i] = (byte) ('a' + random.nextInt(26)); |
| 93 | + } |
| 94 | + inputString = new String(bytes, StandardCharsets.UTF_8); |
| 95 | + inputBinary = Binary.fromConstantByteArray(bytes); |
| 96 | + LOG.info("Setup: stringLength={}", stringLength); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Benchmark converting a {@link Binary} as a string value and building a {@link Variant}. |
| 101 | + * This exercises the path taken by {@link VariantConverters} when decoding a shredded |
| 102 | + * {@code typed_value} string column. |
| 103 | + */ |
| 104 | + @Benchmark |
| 105 | + public void appendStringAsString(Blackhole blackhole) { |
| 106 | + for (int i = 0; i < INNER_ITERATIONS; i++) { |
| 107 | + VariantBuilder builder = new VariantBuilder(); |
| 108 | + builder.appendString(inputString); |
| 109 | + blackhole.consume(builder.build()); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Benchmark converting a {@link Binary} as a string value and building a {@link Variant}. |
| 115 | + * This exercises the path taken by {@link VariantConverters} when decoding a shredded |
| 116 | + * {@code typed_value} string column. |
| 117 | + */ |
| 118 | + @Benchmark |
| 119 | + public void appendStringAsBinary(Blackhole blackhole) { |
| 120 | + for (int i = 0; i < INNER_ITERATIONS; i++) { |
| 121 | + VariantBuilder builder = new VariantBuilder(); |
| 122 | + builder.appendAsString(inputBinary); |
| 123 | + blackhole.consume(builder.build()); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Benchmark appending a {@link Binary} as a binary value and building a {@link Variant}. |
| 129 | + * This exercises the path taken by {@link VariantConverters} when decoding a shredded |
| 130 | + * {@code typed_value} binary column. |
| 131 | + */ |
| 132 | + @Benchmark |
| 133 | + public void appendBinary(Blackhole blackhole) { |
| 134 | + for (int i = 0; i < INNER_ITERATIONS; i++) { |
| 135 | + VariantBuilder builder = new VariantBuilder(); |
| 136 | + builder.appendBinary(inputBinary.toByteBuffer()); |
| 137 | + blackhole.consume(builder.build()); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments