Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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.cassandra.test.microbench;
- import java.nio.ByteBuffer;
- import java.util.Random;
- import java.util.concurrent.TimeUnit;
- import net.jpountz.lz4.LZ4Compressor;
- import net.jpountz.lz4.LZ4Factory;
- import net.jpountz.lz4.LZ4FastDecompressor;
- 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.Scope;
- import org.openjdk.jmh.annotations.Setup;
- import org.openjdk.jmh.annotations.State;
- import org.openjdk.jmh.annotations.Threads;
- import org.openjdk.jmh.annotations.Warmup;
- import org.openjdk.jmh.infra.Blackhole;
- import org.xerial.snappy.Snappy;
- @BenchmarkMode(Mode.Throughput)
- @OutputTimeUnit(TimeUnit.SECONDS)
- @Warmup(iterations = 0, time = 1, timeUnit = TimeUnit.SECONDS)
- @Measurement(iterations = 2, time = 10, timeUnit = TimeUnit.SECONDS)
- @Fork(value = 1,jvmArgsAppend = { "-Xmx8g", "-ea"})
- @Threads(1) // make sure this matches the number of _physical_cores_
- @State(Scope.Benchmark)
- public class CompactIntegerSequenceBench
- {
- private static final int NUM_ELEMENTS = 1024 * 1024 * 1024 / 2;
- private ByteBuffer uncompressedCorpus;
- private ByteBuffer compressedCorpusLZ4High;
- private ByteBuffer compressedCorpusLZ4Fast8k;
- private ByteBuffer compressedCorpusLZ4Fast16k;
- private ByteBuffer compressedCorpusLZ4Fast32k;
- private ByteBuffer compressedCorpusLZ4Fast64k;
- private ByteBuffer compressedCorpusSnappy;
- private ByteBuffer output;
- private LZ4Compressor highCompressor = LZ4Factory.fastestInstance().highCompressor(9);
- private LZ4Compressor fastCompressor = LZ4Factory.fastestInstance().fastCompressor();
- private LZ4FastDecompressor fastDecompressor = LZ4Factory.fastestInstance().fastDecompressor();
- @Setup(Level.Trial)
- public void setup()
- {
- uncompressedCorpus = ByteBuffer.allocateDirect(1024 * 1024 * 1024);
- Random r = new Random();
- while (uncompressedCorpus.hasRemaining())
- {
- uncompressedCorpus.put((byte)(r.nextInt() % 3));
- }
- uncompressedCorpus.flip();
- output = ByteBuffer.allocateDirect(Math.max(highCompressor.maxCompressedLength(1024 * 1024 * 32), Snappy.maxCompressedLength(1024 * 1024 * 32)));
- // compressedCorpusLZ4High = ByteBuffer.allocateDirect(Math.max(highCompressor.maxCompressedLength(uncompressedCorpus.capacity()), Snappy.maxCompressedLength(uncompressedCorpus.capacity())));
- compressedCorpusLZ4Fast8k = ByteBuffer.allocateDirect(Math.max(fastCompressor.maxCompressedLength(uncompressedCorpus.capacity()), Snappy.maxCompressedLength(uncompressedCorpus.capacity())));
- compressedCorpusLZ4Fast16k = ByteBuffer.allocateDirect(Math.max(fastCompressor.maxCompressedLength(uncompressedCorpus.capacity()), Snappy.maxCompressedLength(uncompressedCorpus.capacity())));
- compressedCorpusLZ4Fast32k = ByteBuffer.allocateDirect(Math.max(fastCompressor.maxCompressedLength(uncompressedCorpus.capacity()), Snappy.maxCompressedLength(uncompressedCorpus.capacity())));
- compressedCorpusLZ4Fast64k = ByteBuffer.allocateDirect(Math.max(fastCompressor.maxCompressedLength(uncompressedCorpus.capacity()), Snappy.maxCompressedLength(uncompressedCorpus.capacity())));
- // compressedCorpusSnappy = ByteBuffer.allocateDirect(Math.max(fastCompressor.maxCompressedLength(uncompressedCorpus.capacity()), Snappy.maxCompressedLength(uncompressedCorpus.capacity())));
- //
- while (uncompressedCorpus.hasRemaining())
- {
- compressedCorpusLZ4Fast8k.position(compressedCorpusLZ4Fast8k.position() + fastCompressor.compress(uncompressedCorpus, uncompressedCorpus.position(), 1024 * 8, compressedCorpusLZ4Fast8k, compressedCorpusLZ4Fast8k.position(), compressedCorpusLZ4Fast8k.remaining()));
- uncompressedCorpus.position(uncompressedCorpus.position() + 1024 * 8);
- }
- uncompressedCorpus.flip();
- while (uncompressedCorpus.hasRemaining())
- {
- compressedCorpusLZ4Fast16k.position(compressedCorpusLZ4Fast16k.position() + fastCompressor.compress(uncompressedCorpus, uncompressedCorpus.position(), 1024 * 16, compressedCorpusLZ4Fast16k, compressedCorpusLZ4Fast16k.position(), compressedCorpusLZ4Fast16k.remaining()));
- uncompressedCorpus.position(uncompressedCorpus.position() + 1024 * 16);
- }
- uncompressedCorpus.flip();
- while (uncompressedCorpus.hasRemaining())
- {
- compressedCorpusLZ4Fast32k.position(compressedCorpusLZ4Fast32k.position() + fastCompressor.compress(uncompressedCorpus, uncompressedCorpus.position(), 1024 * 32, compressedCorpusLZ4Fast32k, compressedCorpusLZ4Fast32k.position(), compressedCorpusLZ4Fast32k.remaining()));
- uncompressedCorpus.position(uncompressedCorpus.position() + 1024 * 32);
- }
- uncompressedCorpus.flip();
- while (uncompressedCorpus.hasRemaining())
- {
- compressedCorpusLZ4Fast64k.position(compressedCorpusLZ4Fast64k.position() + fastCompressor.compress(uncompressedCorpus, uncompressedCorpus.position(), 1024 * 64, compressedCorpusLZ4Fast64k, compressedCorpusLZ4Fast64k.position(), compressedCorpusLZ4Fast64k.remaining()));
- uncompressedCorpus.position(uncompressedCorpus.position() + 1024 * 64);
- }
- uncompressedCorpus.flip();
- compressedCorpusLZ4Fast8k.flip();
- compressedCorpusLZ4Fast16k.flip();
- compressedCorpusLZ4Fast32k.flip();
- compressedCorpusLZ4Fast64k.flip();
- }
- public static void main(String args[]) throws Exception
- {
- ByteBuffer corpus = ByteBuffer.allocateDirect(1024 * 1024 * 1024);
- Random r = new Random();
- LZ4Factory factory = LZ4Factory.fastestInstance();
- LZ4Compressor compressor = factory.fastCompressor();
- ByteBuffer output = ByteBuffer.allocateDirect(compressor.maxCompressedLength(1024 * 1024 * 64));
- while (corpus.hasRemaining())
- {
- corpus.put((byte)(r.nextInt() % 3));
- }
- int sizes[] = new int[] { 1024 * 8, 1024 * 16, 1024 * 32, 1024 * 64 };
- for (int size : sizes)
- {
- long compressedSize = 0;
- corpus.clear();
- while (corpus.hasRemaining())
- {
- compressedSize += compressor.compress(corpus, 0, size, output, 0, output.capacity());
- corpus.position(corpus.position() + size);
- }
- System.out.printf("Chunk size %d, ratio %f%n", size, compressedSize / (double)(1024 * 1024 * 1024));
- }
- }
- @Benchmark
- @OperationsPerInvocation(1024 * 8)
- public void benchCompressLZ4Fast8k(Blackhole bh)
- {
- if (!uncompressedCorpus.hasRemaining())
- uncompressedCorpus.position(0);
- bh.consume(fastCompressor.compress(uncompressedCorpus, uncompressedCorpus.position(), 1024 * 8, output, 0, output.remaining()));
- uncompressedCorpus.position(uncompressedCorpus.position() + 1024 * 8);
- }
- @Benchmark
- @OperationsPerInvocation(1024 * 16)
- public void benchCompressLZ4Fast16k(Blackhole bh)
- {
- if (!uncompressedCorpus.hasRemaining())
- uncompressedCorpus.position(0);
- bh.consume(fastCompressor.compress(uncompressedCorpus, uncompressedCorpus.position(), 1024 * 16, output, 0, output.remaining()));
- uncompressedCorpus.position(uncompressedCorpus.position() + 1024 * 16);
- }
- @Benchmark
- @OperationsPerInvocation(1024 * 32)
- public void benchCompressLZ4Fast32k(Blackhole bh)
- {
- if (!uncompressedCorpus.hasRemaining())
- uncompressedCorpus.position(0);
- bh.consume(fastCompressor.compress(uncompressedCorpus, uncompressedCorpus.position(), 1024 * 32, output, 0, output.remaining()));
- uncompressedCorpus.position(uncompressedCorpus.position() + 1024 * 32);
- }
- @Benchmark
- @OperationsPerInvocation(1024 * 64)
- public void benchCompressLZ4Fast64k(Blackhole bh)
- {
- if (!uncompressedCorpus.hasRemaining())
- uncompressedCorpus.position(0);
- bh.consume(fastCompressor.compress(uncompressedCorpus, uncompressedCorpus.position(), 1024 * 64, output, 0, output.remaining()));
- uncompressedCorpus.position(uncompressedCorpus.position() + 1024 * 64);
- }
- @Benchmark
- @OperationsPerInvocation(1024 * 8)
- public void benchDecompressLZ4Fast8k()
- {
- if (!compressedCorpusLZ4Fast8k.hasRemaining())
- compressedCorpusLZ4Fast8k.position(0);
- compressedCorpusLZ4Fast8k.position(compressedCorpusLZ4Fast8k.position() + fastDecompressor.decompress(compressedCorpusLZ4Fast8k, compressedCorpusLZ4Fast8k.position(), output, 0, 1024 * 8));
- }
- @Benchmark
- @OperationsPerInvocation(1024 * 16)
- public void benchDecompressLZ4Fast16k()
- {
- if (!compressedCorpusLZ4Fast16k.hasRemaining())
- compressedCorpusLZ4Fast16k.position(0);
- compressedCorpusLZ4Fast16k.position(compressedCorpusLZ4Fast16k.position() + fastDecompressor.decompress(compressedCorpusLZ4Fast16k, compressedCorpusLZ4Fast16k.position(), output, 0, 1024 * 16));
- }
- @Benchmark
- @OperationsPerInvocation(1024 * 32)
- public void benchDecompressLZ4Fast32k()
- {
- if (!compressedCorpusLZ4Fast32k.hasRemaining())
- compressedCorpusLZ4Fast32k.position(0);
- compressedCorpusLZ4Fast32k.position(compressedCorpusLZ4Fast32k.position() + fastDecompressor.decompress(compressedCorpusLZ4Fast32k, compressedCorpusLZ4Fast32k.position(), output, 0, 1024 * 32));
- }
- @Benchmark
- @OperationsPerInvocation(1024 * 64)
- public void benchDecompressLZ4Fast64k()
- {
- if (!compressedCorpusLZ4Fast64k.hasRemaining())
- compressedCorpusLZ4Fast64k.position(0);
- compressedCorpusLZ4Fast64k.position(compressedCorpusLZ4Fast64k.position() + fastDecompressor.decompress(compressedCorpusLZ4Fast64k, compressedCorpusLZ4Fast64k.position(), output, 0, 1024 * 64));
- }
- // @Benchmark
- // @OperationsPerInvocation(1024 * 8)
- // public void benchCompresssLZ4High8k(Blackhole bh)
- // {
- // if (!uncompressedCorpus.hasRemaining())
- // uncompressedCorpus.position(0);
- // bh.consume(highCompressor.compress(uncompressedCorpus, uncompressedCorpus.position(), 1024 * 8, output, 0, output.remaining()));
- // uncompressedCorpus.position(uncompressedCorpus.position() + 1024 * 8);
- // }
- //
- // @Benchmark
- // @OperationsPerInvocation(1024 * 16)
- // public void benchCompressLZ4High16k(Blackhole bh)
- // {
- // if (!uncompressedCorpus.hasRemaining())
- // uncompressedCorpus.position(0);
- // bh.consume(highCompressor.compress(uncompressedCorpus, uncompressedCorpus.position(), 1024 * 16, output, 0, output.remaining()));
- // uncompressedCorpus.position(uncompressedCorpus.position() + 1024 * 16);
- // }
- //
- // @Benchmark
- // @OperationsPerInvocation(1024 * 32)
- // public void benchCompressLZ4High32k(Blackhole bh)
- // {
- // if (!uncompressedCorpus.hasRemaining())
- // uncompressedCorpus.position(0);
- // bh.consume(highCompressor.compress(uncompressedCorpus, uncompressedCorpus.position(), 1024 * 32, output, 0, output.remaining()));
- // uncompressedCorpus.position(uncompressedCorpus.position() + 1024 * 32);
- // }
- //
- // @Benchmark
- // @OperationsPerInvocation(1024 * 64)
- // public void benchCompressLZ4High64k(Blackhole bh)
- // {
- // if (!uncompressedCorpus.hasRemaining())
- // uncompressedCorpus.position(0);
- // bh.consume(highCompressor.compress(uncompressedCorpus, uncompressedCorpus.position(), 1024 * 64, output, 0, output.remaining()));
- // uncompressedCorpus.position(uncompressedCorpus.position() + 1024 * 64);
- // }
- //
- // @Benchmark
- // @OperationsPerInvocation(1024 * 8)
- // public void benchCompressSnappy8k(Blackhole bh) throws Exception
- // {
- // if (uncompressedCorpus.position() == uncompressedCorpus.capacity())
- // uncompressedCorpus.position(0);
- // output.clear();
- // uncompressedCorpus.limit(uncompressedCorpus.position() + 1024 * 8);
- // bh.consume(Snappy.compress(uncompressedCorpus, output));
- // uncompressedCorpus.position(uncompressedCorpus.position() + 1024 * 8);
- // }
- //
- // @Benchmark
- // @OperationsPerInvocation(1024 * 16)
- // public void benchCompressSnappy16k(Blackhole bh) throws Exception
- // {
- // if (uncompressedCorpus.position() == uncompressedCorpus.capacity())
- // uncompressedCorpus.position(0);
- // output.clear();
- // uncompressedCorpus.limit(uncompressedCorpus.position() + 1024 * 16);
- // bh.consume(Snappy.compress(uncompressedCorpus, output));
- // uncompressedCorpus.position(uncompressedCorpus.position() + 1024 * 16);
- // }
- //
- // @Benchmark
- // @OperationsPerInvocation(1024 * 32)
- // public void benchCompressSnappy32k(Blackhole bh) throws Exception
- // {
- // if (uncompressedCorpus.position() == uncompressedCorpus.capacity())
- // uncompressedCorpus.position(0);
- // output.clear();
- // uncompressedCorpus.limit(uncompressedCorpus.position() + 1024 * 32);
- // bh.consume(Snappy.compress(uncompressedCorpus, output));
- // uncompressedCorpus.position(uncompressedCorpus.position() + 1024 * 32);
- // }
- //
- // @Benchmark
- // @OperationsPerInvocation(1024 * 64)
- // public void benchCompressSnappy64k(Blackhole bh) throws Exception
- // {
- // if (uncompressedCorpus.position() == uncompressedCorpus.capacity())
- // uncompressedCorpus.position(0);
- // output.clear();
- // uncompressedCorpus.limit(uncompressedCorpus.position() + 1024 * 64);
- // bh.consume(Snappy.compress(uncompressedCorpus, output));
- // uncompressedCorpus.position(uncompressedCorpus.position() + 1024 * 64);
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement