-
Notifications
You must be signed in to change notification settings - Fork 1.5k
GH-3511: Add JMH encoding benchmarks and fix parquet-benchmarks shaded jar #3512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iemejia
wants to merge
3
commits into
apache:master
Choose a base branch
from
iemejia:perf-benchmarks
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
parquet-benchmarks/src/main/java/org/apache/parquet/benchmarks/BenchmarkEncodingUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.parquet.benchmarks; | ||
|
|
||
| import java.io.IOException; | ||
| import org.apache.parquet.column.page.DictionaryPage; | ||
| import org.apache.parquet.column.values.dictionary.DictionaryValuesWriter; | ||
|
|
||
| /** | ||
| * Shared helpers for encode/decode micro-benchmarks. | ||
| */ | ||
| final class BenchmarkEncodingUtils { | ||
|
|
||
| private BenchmarkEncodingUtils() {} | ||
|
|
||
| /** | ||
| * Container for the two artefacts produced by a dictionary-encoded page: | ||
| * the encoded dictionary indices ({@link #dictData}) and the dictionary | ||
| * page itself ({@link #dictPage}). The dictionary page may be {@code null} | ||
| * if the writer fell back to plain encoding (for example, when the | ||
| * dictionary exceeded its configured maximum size). | ||
| */ | ||
| static final class EncodedDictionary { | ||
| final byte[] dictData; | ||
| final DictionaryPage dictPage; | ||
|
|
||
| EncodedDictionary(byte[] dictData, DictionaryPage dictPage) { | ||
| this.dictData = dictData; | ||
| this.dictPage = dictPage; | ||
| } | ||
|
|
||
| boolean fellBackToPlain() { | ||
| return dictPage == null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Drains a {@link DictionaryValuesWriter} into an {@link EncodedDictionary}. | ||
| * | ||
| * <p>The writer's data bytes (the RLE-encoded indices) and the dictionary | ||
| * page are returned separately so both pieces can be measured or fed to a | ||
| * decoder symmetrically. The dictionary page buffer is copied so it remains | ||
| * valid after the writer's allocator is released. | ||
| * | ||
| * <p>The writer is closed via {@code toDictPageAndClose()}; callers must not | ||
| * call {@link DictionaryValuesWriter#close()} again afterwards. | ||
| */ | ||
| static EncodedDictionary drainDictionary(DictionaryValuesWriter writer) throws IOException { | ||
| byte[] dictData = writer.getBytes().toByteArray(); | ||
| DictionaryPage rawPage = writer.toDictPageAndClose(); | ||
| DictionaryPage dictPage = rawPage == null ? null : rawPage.copy(); | ||
| return new EncodedDictionary(dictData, dictPage); | ||
| } | ||
| } |
233 changes: 233 additions & 0 deletions
233
parquet-benchmarks/src/main/java/org/apache/parquet/benchmarks/BinaryEncodingBenchmark.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,233 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.parquet.benchmarks; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.ByteBuffer; | ||
| import java.util.concurrent.TimeUnit; | ||
| import org.apache.parquet.bytes.ByteBufferInputStream; | ||
| import org.apache.parquet.bytes.HeapByteBufferAllocator; | ||
| import org.apache.parquet.column.Dictionary; | ||
| import org.apache.parquet.column.Encoding; | ||
| import org.apache.parquet.column.page.DictionaryPage; | ||
| import org.apache.parquet.column.values.ValuesWriter; | ||
| import org.apache.parquet.column.values.deltalengthbytearray.DeltaLengthByteArrayValuesReader; | ||
| import org.apache.parquet.column.values.deltalengthbytearray.DeltaLengthByteArrayValuesWriter; | ||
| import org.apache.parquet.column.values.deltastrings.DeltaByteArrayReader; | ||
| import org.apache.parquet.column.values.deltastrings.DeltaByteArrayWriter; | ||
| import org.apache.parquet.column.values.dictionary.DictionaryValuesReader; | ||
| import org.apache.parquet.column.values.dictionary.DictionaryValuesWriter; | ||
| import org.apache.parquet.column.values.dictionary.PlainValuesDictionary; | ||
| import org.apache.parquet.column.values.plain.BinaryPlainValuesReader; | ||
| import org.apache.parquet.column.values.plain.PlainValuesWriter; | ||
| import org.apache.parquet.io.api.Binary; | ||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.BenchmarkMode; | ||
| import org.openjdk.jmh.annotations.Fork; | ||
| import org.openjdk.jmh.annotations.Level; | ||
| import org.openjdk.jmh.annotations.Measurement; | ||
| import org.openjdk.jmh.annotations.Mode; | ||
| import org.openjdk.jmh.annotations.OperationsPerInvocation; | ||
| import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
| import org.openjdk.jmh.annotations.Param; | ||
| import org.openjdk.jmh.annotations.Scope; | ||
| import org.openjdk.jmh.annotations.Setup; | ||
| import org.openjdk.jmh.annotations.State; | ||
| import org.openjdk.jmh.annotations.Warmup; | ||
| import org.openjdk.jmh.infra.Blackhole; | ||
|
|
||
| /** | ||
| * Encoding-level and decoding-level micro-benchmarks for BINARY values. | ||
| * Compares PLAIN, DELTA_BYTE_ARRAY, DELTA_LENGTH_BYTE_ARRAY, and DICTIONARY encodings | ||
| * across different string lengths and cardinality patterns. | ||
| * | ||
| * <p>Each benchmark invocation processes {@value #VALUE_COUNT} values. Throughput is | ||
| * reported per-value using {@link OperationsPerInvocation}. | ||
| * | ||
| * <p>The dictionary encode/decode benchmarks intentionally measure the full path: | ||
| * the encoder produces both the RLE-encoded indices and a {@link DictionaryPage}; | ||
| * the decoder consumes the indices through a {@link DictionaryValuesReader} backed | ||
| * by the same dictionary. If the dictionary exceeds {@link #MAX_DICT_BYTE_SIZE} | ||
| * (which can happen for high-cardinality, long-string parameter combinations) the | ||
| * writer falls back to plain encoding and dictionary decoding for that combination | ||
| * is skipped. | ||
| */ | ||
| @BenchmarkMode(Mode.Throughput) | ||
| @OutputTimeUnit(TimeUnit.SECONDS) | ||
| @Fork(1) | ||
| @Warmup(iterations = 3, time = 1) | ||
| @Measurement(iterations = 5, time = 1) | ||
| @State(Scope.Thread) | ||
| public class BinaryEncodingBenchmark { | ||
|
|
||
| static final int VALUE_COUNT = 100_000; | ||
| private static final int INIT_SLAB_SIZE = 64 * 1024; | ||
| private static final int PAGE_SIZE = 4 * 1024 * 1024; | ||
| private static final int MAX_DICT_BYTE_SIZE = 4 * 1024 * 1024; | ||
|
|
||
| @Param({"10", "100", "1000"}) | ||
| public int stringLength; | ||
|
|
||
| /** LOW = 100 distinct values; HIGH = all unique. */ | ||
| @Param({"LOW", "HIGH"}) | ||
| public String cardinality; | ||
|
|
||
| private Binary[] data; | ||
| private byte[] plainEncoded; | ||
| private byte[] deltaLengthEncoded; | ||
| private byte[] deltaStringsEncoded; | ||
| private byte[] dictEncoded; | ||
| private DictionaryPage dictPage; | ||
| private Dictionary binaryDictionary; | ||
| private boolean dictionaryAvailable; | ||
|
|
||
| @Setup(Level.Trial) | ||
| public void setup() throws IOException { | ||
| int distinct = "LOW".equals(cardinality) ? TestDataFactory.LOW_CARDINALITY_DISTINCT : 0; | ||
| data = TestDataFactory.generateBinaryData(VALUE_COUNT, stringLength, distinct, TestDataFactory.DEFAULT_SEED); | ||
|
|
||
| // Pre-encode data for decode benchmarks | ||
| plainEncoded = encodeBinaryWith(newPlainWriter()); | ||
| deltaLengthEncoded = encodeBinaryWith(newDeltaLengthWriter()); | ||
| deltaStringsEncoded = encodeBinaryWith(newDeltaStringsWriter()); | ||
|
|
||
| DictionaryValuesWriter.PlainBinaryDictionaryValuesWriter dictWriter = newDictWriter(); | ||
| for (Binary v : data) { | ||
| dictWriter.writeBytes(v); | ||
| } | ||
| BenchmarkEncodingUtils.EncodedDictionary encoded = BenchmarkEncodingUtils.drainDictionary(dictWriter); | ||
| dictEncoded = encoded.dictData; | ||
| dictPage = encoded.dictPage; | ||
| dictionaryAvailable = !encoded.fellBackToPlain(); | ||
| if (dictionaryAvailable) { | ||
| binaryDictionary = new PlainValuesDictionary.PlainBinaryDictionary(dictPage); | ||
| } | ||
| } | ||
|
|
||
| private byte[] encodeBinaryWith(ValuesWriter writer) throws IOException { | ||
| for (Binary v : data) { | ||
| writer.writeBytes(v); | ||
| } | ||
| byte[] bytes = writer.getBytes().toByteArray(); | ||
| writer.close(); | ||
| return bytes; | ||
| } | ||
|
|
||
| private BenchmarkEncodingUtils.EncodedDictionary encodeDictionaryWith( | ||
| DictionaryValuesWriter.PlainBinaryDictionaryValuesWriter writer) throws IOException { | ||
| for (Binary v : data) { | ||
| writer.writeBytes(v); | ||
| } | ||
| return BenchmarkEncodingUtils.drainDictionary(writer); | ||
| } | ||
|
|
||
| // ---- Writer factories ---- | ||
|
|
||
| private static PlainValuesWriter newPlainWriter() { | ||
| return new PlainValuesWriter(INIT_SLAB_SIZE, PAGE_SIZE, new HeapByteBufferAllocator()); | ||
| } | ||
|
|
||
| private static DeltaLengthByteArrayValuesWriter newDeltaLengthWriter() { | ||
| return new DeltaLengthByteArrayValuesWriter(INIT_SLAB_SIZE, PAGE_SIZE, new HeapByteBufferAllocator()); | ||
| } | ||
|
|
||
| private static DeltaByteArrayWriter newDeltaStringsWriter() { | ||
| return new DeltaByteArrayWriter(INIT_SLAB_SIZE, PAGE_SIZE, new HeapByteBufferAllocator()); | ||
| } | ||
|
|
||
| private static DictionaryValuesWriter.PlainBinaryDictionaryValuesWriter newDictWriter() { | ||
| return new DictionaryValuesWriter.PlainBinaryDictionaryValuesWriter( | ||
| MAX_DICT_BYTE_SIZE, Encoding.PLAIN_DICTIONARY, Encoding.PLAIN, new HeapByteBufferAllocator()); | ||
| } | ||
|
|
||
| // ---- Encode benchmarks ---- | ||
|
|
||
| @Benchmark | ||
| @OperationsPerInvocation(VALUE_COUNT) | ||
| public byte[] encodePlain() throws IOException { | ||
| return encodeBinaryWith(newPlainWriter()); | ||
| } | ||
|
|
||
| @Benchmark | ||
| @OperationsPerInvocation(VALUE_COUNT) | ||
| public byte[] encodeDeltaLengthByteArray() throws IOException { | ||
| return encodeBinaryWith(newDeltaLengthWriter()); | ||
| } | ||
|
|
||
| @Benchmark | ||
| @OperationsPerInvocation(VALUE_COUNT) | ||
| public byte[] encodeDeltaByteArray() throws IOException { | ||
| return encodeBinaryWith(newDeltaStringsWriter()); | ||
| } | ||
|
|
||
| @Benchmark | ||
| @OperationsPerInvocation(VALUE_COUNT) | ||
| public void encodeDictionary(Blackhole bh) throws IOException { | ||
| BenchmarkEncodingUtils.EncodedDictionary encoded = encodeDictionaryWith(newDictWriter()); | ||
| bh.consume(encoded.dictData); | ||
| bh.consume(encoded.dictPage); | ||
| } | ||
|
|
||
| // ---- Decode benchmarks ---- | ||
|
|
||
| @Benchmark | ||
| @OperationsPerInvocation(VALUE_COUNT) | ||
| public void decodePlain(Blackhole bh) throws IOException { | ||
| BinaryPlainValuesReader reader = new BinaryPlainValuesReader(); | ||
| reader.initFromPage(VALUE_COUNT, ByteBufferInputStream.wrap(ByteBuffer.wrap(plainEncoded))); | ||
| for (int i = 0; i < VALUE_COUNT; i++) { | ||
| bh.consume(reader.readBytes()); | ||
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| @OperationsPerInvocation(VALUE_COUNT) | ||
| public void decodeDeltaLengthByteArray(Blackhole bh) throws IOException { | ||
| DeltaLengthByteArrayValuesReader reader = new DeltaLengthByteArrayValuesReader(); | ||
| reader.initFromPage(VALUE_COUNT, ByteBufferInputStream.wrap(ByteBuffer.wrap(deltaLengthEncoded))); | ||
| for (int i = 0; i < VALUE_COUNT; i++) { | ||
| bh.consume(reader.readBytes()); | ||
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| @OperationsPerInvocation(VALUE_COUNT) | ||
| public void decodeDeltaByteArray(Blackhole bh) throws IOException { | ||
| DeltaByteArrayReader reader = new DeltaByteArrayReader(); | ||
| reader.initFromPage(VALUE_COUNT, ByteBufferInputStream.wrap(ByteBuffer.wrap(deltaStringsEncoded))); | ||
| for (int i = 0; i < VALUE_COUNT; i++) { | ||
| bh.consume(reader.readBytes()); | ||
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| @OperationsPerInvocation(VALUE_COUNT) | ||
| public void decodeDictionary(Blackhole bh) throws IOException { | ||
| if (!dictionaryAvailable) { | ||
| // Dictionary fell back to plain encoding (e.g. high-cardinality long strings | ||
| // exceeding MAX_DICT_BYTE_SIZE). Skip to keep the benchmark meaningful. | ||
| return; | ||
| } | ||
| DictionaryValuesReader reader = new DictionaryValuesReader(binaryDictionary); | ||
| reader.initFromPage(VALUE_COUNT, ByteBufferInputStream.wrap(ByteBuffer.wrap(dictEncoded))); | ||
| for (int i = 0; i < VALUE_COUNT; i++) { | ||
| bh.consume(reader.readBytes()); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
didn't know of this trick until i saw your PR; adopted in #3452 earlier today