test/micro/org/openjdk/micro/jdk/java/util/zip/ChecksumBenchmarks.java
branchJEP-230-microbenchmarks-branch
changeset 56913 013359fdfeb2
parent 56905 d4ab0656f48e
equal deleted inserted replaced
56909:7cf3051d8572 56913:013359fdfeb2
       
     1 /*
       
     2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 package org.openjdk.micro.jdk.java.util.zip;
       
    26 
       
    27 import java.nio.ByteBuffer;
       
    28 import java.nio.charset.StandardCharsets;
       
    29 import java.util.Random;
       
    30 import java.util.concurrent.TimeUnit;
       
    31 import java.util.zip.Checksum;
       
    32 import org.openjdk.jmh.annotations.Benchmark;
       
    33 import org.openjdk.jmh.annotations.OperationsPerInvocation;
       
    34 import org.openjdk.jmh.annotations.OutputTimeUnit;
       
    35 import org.openjdk.jmh.annotations.Scope;
       
    36 import org.openjdk.jmh.annotations.Setup;
       
    37 import org.openjdk.jmh.annotations.State;
       
    38 
       
    39 /**
       
    40  *
       
    41  * Base class for benchmarking JDK supported Checksums.
       
    42  *
       
    43  * To use the base class extend it and use a setup method configure the checksum
       
    44  * field.
       
    45  * 
       
    46  */
       
    47 @State(Scope.Thread)
       
    48 @OutputTimeUnit(TimeUnit.MILLISECONDS)
       
    49 public abstract class ChecksumBenchmarks {
       
    50 
       
    51     private final byte[] bytes_1to9 = "123456789".getBytes(StandardCharsets.US_ASCII);
       
    52     private final byte[] byteArray_1k = new byte[1024];
       
    53     private final byte[] byteArray_64k = new byte[65536];
       
    54     private final ByteBuffer wrappedByteBuffer_1k = ByteBuffer.wrap(byteArray_1k);
       
    55     private final ByteBuffer readonlyByteBuffer_1k = ByteBuffer.wrap(byteArray_1k).asReadOnlyBuffer();
       
    56     private final ByteBuffer directByteBuffer_1k = ByteBuffer.allocateDirect(byteArray_1k.length);
       
    57     private final ByteBuffer wrappedByteBuffer_64k = ByteBuffer.wrap(byteArray_64k);
       
    58     private final ByteBuffer readonlyByteBuffer_64k = ByteBuffer.wrap(byteArray_64k).asReadOnlyBuffer();
       
    59     private final ByteBuffer directByteBuffer_64k = ByteBuffer.allocateDirect(byteArray_64k.length);
       
    60 
       
    61     @Setup
       
    62     final public void setup() {
       
    63         Random r = new Random(123456789L);
       
    64         r.nextBytes(byteArray_1k);
       
    65         r.nextBytes(byteArray_64k);
       
    66         directByteBuffer_1k.put(byteArray_1k);
       
    67         directByteBuffer_64k.put(byteArray_64k);
       
    68     }
       
    69 
       
    70     protected Checksum checksum;
       
    71 
       
    72     @Benchmark
       
    73     @OperationsPerInvocation(9)
       
    74     public void byteArray_9() {
       
    75         checksum.update(bytes_1to9);
       
    76     }
       
    77 
       
    78     @Benchmark
       
    79     @OperationsPerInvocation(1024)
       
    80     public void byteArray_1K() {
       
    81         checksum.update(byteArray_1k);
       
    82     }
       
    83 
       
    84     @Benchmark
       
    85     @OperationsPerInvocation(1024)
       
    86     public void wrappedByteBuffer_1K() {
       
    87         wrappedByteBuffer_1k.position(0);
       
    88         checksum.update(wrappedByteBuffer_1k);
       
    89     }
       
    90 
       
    91     @Benchmark
       
    92     @OperationsPerInvocation(1024)
       
    93     public void readonlyByteBuffer_1K() {
       
    94         readonlyByteBuffer_1k.position(0);
       
    95         checksum.update(readonlyByteBuffer_1k);
       
    96     }
       
    97 
       
    98     @Benchmark
       
    99     @OperationsPerInvocation(1024)
       
   100     public void directByteBuffer_1K() {
       
   101         directByteBuffer_1k.position(0);
       
   102         checksum.update(directByteBuffer_1k);
       
   103     }
       
   104 
       
   105     @Benchmark
       
   106     @OperationsPerInvocation(65536)
       
   107     public void byteArray_64K() {
       
   108         checksum.update(byteArray_64k);
       
   109     }
       
   110 
       
   111     @Benchmark
       
   112     @OperationsPerInvocation(65536)
       
   113     public void wrappedByteBuffer_64K() {
       
   114         wrappedByteBuffer_64k.position(0);
       
   115         checksum.update(wrappedByteBuffer_64k);
       
   116     }
       
   117 
       
   118     @Benchmark
       
   119     @OperationsPerInvocation(65536)
       
   120     public void readonlyByteBuffer_64K() {
       
   121         readonlyByteBuffer_64k.position(0);
       
   122         checksum.update(readonlyByteBuffer_64k);
       
   123     }
       
   124 
       
   125     @Benchmark
       
   126     @OperationsPerInvocation(65536)
       
   127     public void directByteBuffer_64K() {
       
   128         directByteBuffer_64k.position(0);
       
   129         checksum.update(directByteBuffer_64k);
       
   130     }
       
   131 }