|
| 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, |
| 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 | +package org.apache.parquet.benchmarks; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.nio.ByteBuffer; |
| 23 | +import java.util.concurrent.TimeUnit; |
| 24 | +import org.apache.parquet.bytes.ByteBufferInputStream; |
| 25 | +import org.apache.parquet.bytes.HeapByteBufferAllocator; |
| 26 | +import org.apache.parquet.column.values.ValuesReader; |
| 27 | +import org.apache.parquet.column.values.ValuesWriter; |
| 28 | +import org.apache.parquet.column.values.alp.AlpValuesReaderForDouble; |
| 29 | +import org.apache.parquet.column.values.alp.AlpValuesReaderForFloat; |
| 30 | +import org.apache.parquet.column.values.alp.AlpValuesWriter; |
| 31 | +import org.apache.parquet.column.values.plain.PlainValuesReader; |
| 32 | +import org.apache.parquet.column.values.plain.PlainValuesWriter; |
| 33 | +import org.openjdk.jmh.annotations.Benchmark; |
| 34 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 35 | +import org.openjdk.jmh.annotations.Fork; |
| 36 | +import org.openjdk.jmh.annotations.Level; |
| 37 | +import org.openjdk.jmh.annotations.Measurement; |
| 38 | +import org.openjdk.jmh.annotations.Mode; |
| 39 | +import org.openjdk.jmh.annotations.OperationsPerInvocation; |
| 40 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 41 | +import org.openjdk.jmh.annotations.Param; |
| 42 | +import org.openjdk.jmh.annotations.Scope; |
| 43 | +import org.openjdk.jmh.annotations.Setup; |
| 44 | +import org.openjdk.jmh.annotations.State; |
| 45 | +import org.openjdk.jmh.annotations.Warmup; |
| 46 | +import org.openjdk.jmh.infra.Blackhole; |
| 47 | + |
| 48 | +/** |
| 49 | + * Decoding-level micro-benchmarks for ALP, with PLAIN on the same data as a baseline. The data sets |
| 50 | + * are those of {@link AlpEncodingBenchmark}. |
| 51 | + * |
| 52 | + * <p>Each invocation decodes {@value #VALUE_COUNT} values; throughput is reported per-value via |
| 53 | + * {@link OperationsPerInvocation}. |
| 54 | + */ |
| 55 | +@BenchmarkMode(Mode.Throughput) |
| 56 | +@OutputTimeUnit(TimeUnit.SECONDS) |
| 57 | +@Fork(1) |
| 58 | +@Warmup(iterations = 3, time = 1) |
| 59 | +@Measurement(iterations = 5, time = 1) |
| 60 | +@State(Scope.Thread) |
| 61 | +public class AlpDecodingBenchmark { |
| 62 | + |
| 63 | + static final int VALUE_COUNT = 100_000; |
| 64 | + private static final int INIT_SLAB_SIZE = 64 * 1024; |
| 65 | + private static final int PAGE_SIZE = 4 * 1024 * 1024; |
| 66 | + |
| 67 | + @Param({"decimal", "random"}) |
| 68 | + public String data; |
| 69 | + |
| 70 | + private byte[] alpDoublePage; |
| 71 | + private byte[] plainDoublePage; |
| 72 | + private byte[] alpFloatPage; |
| 73 | + private byte[] plainFloatPage; |
| 74 | + |
| 75 | + @Setup(Level.Trial) |
| 76 | + public void setup() throws IOException { |
| 77 | + double[] doubles = AlpEncodingBenchmark.doubles(data, VALUE_COUNT); |
| 78 | + float[] floats = AlpEncodingBenchmark.floats(data, VALUE_COUNT); |
| 79 | + alpDoublePage = page( |
| 80 | + new AlpValuesWriter.DoubleAlpValuesWriter(INIT_SLAB_SIZE, PAGE_SIZE, new HeapByteBufferAllocator()), |
| 81 | + doubles); |
| 82 | + plainDoublePage = |
| 83 | + page(new PlainValuesWriter(INIT_SLAB_SIZE, PAGE_SIZE, new HeapByteBufferAllocator()), doubles); |
| 84 | + alpFloatPage = page( |
| 85 | + new AlpValuesWriter.FloatAlpValuesWriter(INIT_SLAB_SIZE, PAGE_SIZE, new HeapByteBufferAllocator()), |
| 86 | + floats); |
| 87 | + plainFloatPage = page(new PlainValuesWriter(INIT_SLAB_SIZE, PAGE_SIZE, new HeapByteBufferAllocator()), floats); |
| 88 | + } |
| 89 | + |
| 90 | + private static byte[] page(ValuesWriter w, double[] values) throws IOException { |
| 91 | + for (double v : values) { |
| 92 | + w.writeDouble(v); |
| 93 | + } |
| 94 | + byte[] bytes = w.getBytes().toByteArray(); |
| 95 | + w.close(); |
| 96 | + return bytes; |
| 97 | + } |
| 98 | + |
| 99 | + private static byte[] page(ValuesWriter w, float[] values) throws IOException { |
| 100 | + for (float v : values) { |
| 101 | + w.writeFloat(v); |
| 102 | + } |
| 103 | + byte[] bytes = w.getBytes().toByteArray(); |
| 104 | + w.close(); |
| 105 | + return bytes; |
| 106 | + } |
| 107 | + |
| 108 | + private static void init(ValuesReader r, byte[] page) throws IOException { |
| 109 | + r.initFromPage(VALUE_COUNT, ByteBufferInputStream.wrap(ByteBuffer.wrap(page))); |
| 110 | + } |
| 111 | + |
| 112 | + @Benchmark |
| 113 | + @OperationsPerInvocation(VALUE_COUNT) |
| 114 | + public void decodeDouble(Blackhole bh) throws IOException { |
| 115 | + AlpValuesReaderForDouble r = new AlpValuesReaderForDouble(); |
| 116 | + init(r, alpDoublePage); |
| 117 | + for (int i = 0; i < VALUE_COUNT; i++) { |
| 118 | + bh.consume(r.readDouble()); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @Benchmark |
| 123 | + @OperationsPerInvocation(VALUE_COUNT) |
| 124 | + public void decodeDoublePlain(Blackhole bh) throws IOException { |
| 125 | + PlainValuesReader.DoublePlainValuesReader r = new PlainValuesReader.DoublePlainValuesReader(); |
| 126 | + init(r, plainDoublePage); |
| 127 | + for (int i = 0; i < VALUE_COUNT; i++) { |
| 128 | + bh.consume(r.readDouble()); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + @Benchmark |
| 133 | + @OperationsPerInvocation(VALUE_COUNT) |
| 134 | + public void decodeFloat(Blackhole bh) throws IOException { |
| 135 | + AlpValuesReaderForFloat r = new AlpValuesReaderForFloat(); |
| 136 | + init(r, alpFloatPage); |
| 137 | + for (int i = 0; i < VALUE_COUNT; i++) { |
| 138 | + bh.consume(r.readFloat()); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @Benchmark |
| 143 | + @OperationsPerInvocation(VALUE_COUNT) |
| 144 | + public void decodeFloatPlain(Blackhole bh) throws IOException { |
| 145 | + PlainValuesReader.FloatPlainValuesReader r = new PlainValuesReader.FloatPlainValuesReader(); |
| 146 | + init(r, plainFloatPage); |
| 147 | + for (int i = 0; i < VALUE_COUNT; i++) { |
| 148 | + bh.consume(r.readFloat()); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments