|
| 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 | + |
| 10 | +package org.elasticsearch.benchmark.common.compress; |
| 11 | + |
| 12 | +import org.apache.lucene.codecs.compressing.CompressionMode; |
| 13 | +import org.apache.lucene.codecs.compressing.Compressor; |
| 14 | +import org.apache.lucene.codecs.compressing.Decompressor; |
| 15 | +import org.apache.lucene.store.ByteArrayDataInput; |
| 16 | +import org.apache.lucene.store.ByteArrayDataOutput; |
| 17 | +import org.apache.lucene.store.ByteBuffersDataInput; |
| 18 | +import org.apache.lucene.util.BytesRef; |
| 19 | +import org.elasticsearch.common.compress.fsst.FSST; |
| 20 | +import org.openjdk.jmh.annotations.*; |
| 21 | +import org.openjdk.jmh.infra.Blackhole; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.nio.ByteBuffer; |
| 25 | +import java.nio.charset.StandardCharsets; |
| 26 | +import java.nio.file.Files; |
| 27 | +import java.nio.file.Path; |
| 28 | +import java.util.List; |
| 29 | +import java.util.concurrent.TimeUnit; |
| 30 | + |
| 31 | +@Fork(1) |
| 32 | +@Warmup(iterations = 2) |
| 33 | +@Measurement(iterations = 3) |
| 34 | +@BenchmarkMode(Mode.AverageTime) |
| 35 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 36 | +@State(Scope.Benchmark) |
| 37 | +public class FSSTDecompressBenchmark { |
| 38 | + |
| 39 | + @Param({"fsst", "lz4_high", "lz4_fast"}) |
| 40 | + public String compressionType; |
| 41 | + |
| 42 | + @Param("") |
| 43 | + public String dataset; |
| 44 | + |
| 45 | + // original file |
| 46 | + private int originalSize; |
| 47 | + private byte[] input; |
| 48 | + private int[] offsets; |
| 49 | + |
| 50 | + // compressed |
| 51 | + private byte[] outBuf; |
| 52 | + private int[] outOffsets; |
| 53 | + private int compressedSize; |
| 54 | + |
| 55 | + // decompressed |
| 56 | + private byte[] decompressBuf; |
| 57 | + |
| 58 | + // fsst specific |
| 59 | + private FSST.SymbolTable symbolTable; |
| 60 | + |
| 61 | + @Setup(Level.Trial) |
| 62 | + public void setup() throws IOException { |
| 63 | + String content = Files.readString(Path.of(dataset), StandardCharsets.UTF_8); |
| 64 | + byte[] bytes = FSST.toBytes(content); |
| 65 | + originalSize = bytes.length; |
| 66 | + input = new byte[originalSize + 8]; |
| 67 | + offsets = new int[]{0, bytes.length}; |
| 68 | + System.arraycopy(bytes, 0, input, 0, bytes.length); |
| 69 | + |
| 70 | + outBuf = new byte[input.length]; |
| 71 | + outOffsets = new int[2]; |
| 72 | + |
| 73 | + decompressBuf = new byte[input.length]; |
| 74 | + |
| 75 | + if (compressionType.equals("fsst")) { |
| 76 | + List<byte[]> sample = FSST.makeSample(input, offsets); |
| 77 | + symbolTable = FSST.SymbolTable.buildSymbolTable(sample); |
| 78 | + symbolTable.compressBulk(1, input, offsets, outBuf, outOffsets); |
| 79 | + compressedSize = outOffsets[1]; |
| 80 | + } else if (compressionType.equals("lz4_fast")) { |
| 81 | + var dataInput = new ByteBuffersDataInput(List.of(ByteBuffer.wrap(input, 0, originalSize))); |
| 82 | + var dataOutput = new ByteArrayDataOutput(outBuf); |
| 83 | + Compressor compressor = CompressionMode.FAST.newCompressor(); |
| 84 | + compressor.compress(dataInput, dataOutput); |
| 85 | + compressedSize = dataOutput.getPosition(); |
| 86 | + } else if (compressionType.equals("lz4_high")) { |
| 87 | + var dataInput = new ByteBuffersDataInput(List.of(ByteBuffer.wrap(input, 0, originalSize))); |
| 88 | + var dataOutput = new ByteArrayDataOutput(outBuf); |
| 89 | + Compressor compressor = CompressionMode.HIGH_COMPRESSION.newCompressor(); |
| 90 | + compressor.compress(dataInput, dataOutput); |
| 91 | + compressedSize = dataOutput.getPosition(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Benchmark |
| 96 | + public void decompress(Blackhole bh) throws IOException { |
| 97 | + if (compressionType.equals("fsst")) { |
| 98 | + byte[] symbolTableBytes = symbolTable.exportToBytes(); |
| 99 | + FSST.Decoder decoder = FSST.Decoder.readFrom(symbolTableBytes); |
| 100 | + int decompressedLen = FSST.decompress(outBuf, 0, outOffsets[1], decoder, decompressBuf); |
| 101 | +// assert Arrays.equals(input, 0, originalSize, decompressBuf, 0, originalSize); |
| 102 | + bh.consume(decompressBuf); |
| 103 | + bh.consume(decompressedLen); |
| 104 | + } else if (compressionType.equals("lz4_fast")) { |
| 105 | + Decompressor decompressor = CompressionMode.FAST.newDecompressor(); |
| 106 | + var dataInput = new ByteArrayDataInput(outBuf, 0, compressedSize); |
| 107 | + var outBytesRef = new BytesRef(decompressBuf); |
| 108 | + decompressor.decompress(dataInput, originalSize, 0, originalSize, outBytesRef); |
| 109 | +// assert Arrays.equals(input, 0, originalSize, outBytesRef.bytes, 0, originalSize); |
| 110 | + bh.consume(outBytesRef); |
| 111 | + } else if (compressionType.equals("lz4_high")) { |
| 112 | + Decompressor decompressor = CompressionMode.HIGH_COMPRESSION.newDecompressor(); |
| 113 | + var dataInput = new ByteArrayDataInput(outBuf, 0, compressedSize); |
| 114 | + var outBytesRef = new BytesRef(decompressBuf); |
| 115 | + decompressor.decompress(dataInput, originalSize, 0, originalSize, outBytesRef); |
| 116 | +// assert Arrays.equals(input, 0, originalSize, outBytesRef.bytes, 0, originalSize); |
| 117 | + bh.consume(outBytesRef); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments