test/micro/org/openjdk/micro/jdk/java/util/zip/ChecksumUnalignedBenchmarks.java
author redestad
Tue, 02 Oct 2018 11:53:46 +0200
branchJEP-230-microbenchmarks-branch
changeset 56913 013359fdfeb2
parent 56905 test/micro/src/classes/org/openjdk/micro/jdk/java/util/zip/ChecksumUnalignedBenchmarks.java@d4ab0656f48e
permissions -rw-r--r--
Reorganize micro sources
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56905
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
     1
/*
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
     4
 *
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    10
 *
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    15
 * accompanied this code).
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    16
 *
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    20
 *
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    23
 * questions.
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    24
 */
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    25
package org.openjdk.micro.jdk.java.util.zip;
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    26
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    27
import java.nio.ByteBuffer;
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    28
import java.util.Random;
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    29
import java.util.concurrent.TimeUnit;
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    30
import java.util.zip.Checksum;
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    31
import org.openjdk.jmh.annotations.Benchmark;
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    32
import org.openjdk.jmh.annotations.OperationsPerInvocation;
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    33
import org.openjdk.jmh.annotations.OutputTimeUnit;
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    34
import org.openjdk.jmh.annotations.Param;
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    35
import org.openjdk.jmh.annotations.Scope;
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    36
import org.openjdk.jmh.annotations.Setup;
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    37
import org.openjdk.jmh.annotations.State;
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    38
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    39
/**
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    40
 *
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    41
 * Base class for benchmarking JDK supported Checksums with unaligned memory
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    42
 * accesses.
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    43
 *
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    44
 * To use the base class extend it and use a setup method configure the checksum
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    45
 * field.
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    46
 *
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    47
 */
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    48
@State(Scope.Thread)
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    49
@OutputTimeUnit(TimeUnit.MILLISECONDS)
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    50
public abstract class ChecksumUnalignedBenchmarks {
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    51
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    52
    private final byte[] unalignedByteArray_1k = new byte[1034];
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    53
    private final ByteBuffer unalignedWrappedByteBuffer_1k = ByteBuffer.wrap(unalignedByteArray_1k);
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    54
    private final ByteBuffer unalignedDirectByteBuffer_1k = ByteBuffer.allocateDirect(unalignedByteArray_1k.length);
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    55
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    56
    @Setup
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    57
    final public void setup() {
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    58
        Random r = new Random(123456789L);
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    59
        r.nextBytes(unalignedByteArray_1k);
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    60
        unalignedDirectByteBuffer_1k.put(unalignedByteArray_1k);
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    61
    }
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    62
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    63
    protected Checksum checksum;
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    64
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    65
    @Param({"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"})
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    66
    private int offset;
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    67
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    68
    @Benchmark
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    69
    @OperationsPerInvocation(1024)
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    70
    public void byteArray_1K() {
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    71
        checksum.update(unalignedByteArray_1k, offset, 1024);
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    72
    }
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    73
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    74
    @Benchmark
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    75
    @OperationsPerInvocation(1024)
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    76
    public void wrappedByteBuffer_1K() {
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    77
        unalignedWrappedByteBuffer_1k.position(offset);
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    78
        unalignedWrappedByteBuffer_1k.limit(offset + 1024);
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    79
        checksum.update(unalignedWrappedByteBuffer_1k);
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    80
    }
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    81
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    82
    @Benchmark
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    83
    @OperationsPerInvocation(1024)
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    84
    public void directByteBuffer_1K() {
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    85
        unalignedDirectByteBuffer_1k.position(offset);
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    86
        unalignedWrappedByteBuffer_1k.limit(offset + 1024);
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    87
        checksum.update(unalignedDirectByteBuffer_1k);
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    88
    }
d4ab0656f48e Initial import of Claes work.
ihse
parents:
diff changeset
    89
}