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 org.apache.cassandra.utils.CRC32Factory.CRC32Ex;
- import org.apache.cassandra.utils.PureJavaCrc32;
- import org.openjdk.jmh.annotations.*;
- import java.io.IOException;
- import java.nio.ByteBuffer;
- import java.util.concurrent.ThreadLocalRandom;
- import java.util.concurrent.TimeUnit;
- import java.util.zip.Adler32;
- import java.util.zip.CRC32;
- @BenchmarkMode(Mode.Throughput)
- @OutputTimeUnit(TimeUnit.SECONDS)
- @Warmup(iterations = 2, time = 2, timeUnit = TimeUnit.SECONDS)
- @Measurement(iterations = 2, time = 2, timeUnit = TimeUnit.SECONDS)
- @Fork(value = 3)
- @Threads(1)
- @State(Scope.Benchmark)
- public class Sample
- {
- @Param({"128", "512", "1024", "1048576"})
- //@Param({"1048576"})
- public int byteSize;
- public byte bytes[];
- public ByteBuffer bufferArray;
- public ByteBuffer bufferDirect;
- public long expected;
- public long expectedAdler;
- public void check(long val) {
- if (val != expected) {
- System.out.println("oh snap");
- System.exit(-1);
- }
- }
- public void checkAdler(long val) {
- if (val != expectedAdler) {
- System.out.println("oh snap");
- System.exit(-1);
- }
- }
- @Setup(Level.Iteration)
- public void setup() {
- bytes = new byte[byteSize];
- bufferArray = ByteBuffer.wrap(bytes);
- bufferDirect = ByteBuffer.allocateDirect(byteSize);
- bufferDirect.put(bytes).clear();
- CRC32 crc = new CRC32();
- crc.update(bytes);
- expected = crc.getValue();
- Adler32 a32 = new Adler32();
- a32.update(bytes);
- expectedAdler = a32.getValue();
- }
- @Benchmark
- public long CRC32OriginalArray()
- {
- CRC32 crc = new CRC32();
- crc.update(bytes, 0, bytes.length);
- check(crc.getValue());
- return crc.getValue();
- }
- @Benchmark
- public long PureJavaCrc32Array() {
- PureJavaCrc32 crc = new PureJavaCrc32();
- crc.update(bytes, 0, bytes.length);
- check(crc.getValue());
- return crc.getValue();
- }
- @Benchmark
- public long Adler32Array()
- {
- Adler32 crc = new Adler32();
- crc.update(bytes, 0, bytes.length);
- checkAdler(crc.getValue());
- return crc.getValue();
- }
- // @Benchmark
- // public long CRC32Array()
- // {
- // CRC32Ex crc = new CRC32Ex();
- // crc.update(bytes, 0, bytes.length);
- // check(crc.getValue());
- // return crc.getValue();
- // }
- //
- // @Benchmark
- // public long CRC32ByteBuffer()
- // {
- // bufferArray.clear();
- // CRC32Ex crc = new CRC32Ex();
- // crc.update(bufferArray);
- // return crc.getValue();
- // }
- //
- // @Benchmark
- // public long CRC32ByteBufferDirect()
- // {
- // bufferDirect.clear();
- // CRC32Ex crc = new CRC32Ex();
- // crc.update(bufferDirect);
- // return crc.getValue();
- // }
- //
- // @Benchmark
- // public long CRC32ByteBufferWrapped()
- // {
- // bufferArray.clear();
- // CRC32Ex crc = new CRC32Ex();
- // crc.update(bufferArray, 0, bytes.length);
- // check(crc.getValue());
- // return crc.getValue();
- // }
- //
- // @Benchmark
- // public long CRC32ByteBufferDirectWrapped()
- // {
- // bufferDirect.clear();
- // CRC32Ex crc = new CRC32Ex();
- // crc.update(bufferDirect, 0, bytes.length);
- // check(crc.getValue());
- // return crc.getValue();
- // }
- // @Benchmark
- // public long PureJavaCrc32ByteBuffer() {
- // bufferArray.clear();
- // PureJavaCrc32 crc = new PureJavaCrc32();
- // crc.update(bufferArray, 0, bytes.length);
- // check(crc.getValue());
- // return crc.getValue();
- // }
- //
- // @Benchmark
- // public long PureJavaCrc32ByteBufferDirect() {
- // bufferDirect.clear();
- // PureJavaCrc32 crc = new PureJavaCrc32();
- // crc.update(bufferDirect, 0, bytes.length);
- // check(crc.getValue());
- // return crc.getValue();
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment