56905
|
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.util.Random;
|
|
29 |
import java.util.concurrent.TimeUnit;
|
|
30 |
import java.util.zip.Checksum;
|
|
31 |
import org.openjdk.jmh.annotations.Benchmark;
|
|
32 |
import org.openjdk.jmh.annotations.OperationsPerInvocation;
|
|
33 |
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
|
34 |
import org.openjdk.jmh.annotations.Param;
|
|
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 with unaligned memory
|
|
42 |
* accesses.
|
|
43 |
*
|
|
44 |
* To use the base class extend it and use a setup method configure the checksum
|
|
45 |
* field.
|
|
46 |
*
|
|
47 |
*/
|
|
48 |
@State(Scope.Thread)
|
|
49 |
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
|
50 |
public abstract class ChecksumUnalignedBenchmarks {
|
|
51 |
|
|
52 |
private final byte[] unalignedByteArray_1k = new byte[1034];
|
|
53 |
private final ByteBuffer unalignedWrappedByteBuffer_1k = ByteBuffer.wrap(unalignedByteArray_1k);
|
|
54 |
private final ByteBuffer unalignedDirectByteBuffer_1k = ByteBuffer.allocateDirect(unalignedByteArray_1k.length);
|
|
55 |
|
|
56 |
@Setup
|
|
57 |
final public void setup() {
|
|
58 |
Random r = new Random(123456789L);
|
|
59 |
r.nextBytes(unalignedByteArray_1k);
|
|
60 |
unalignedDirectByteBuffer_1k.put(unalignedByteArray_1k);
|
|
61 |
}
|
|
62 |
|
|
63 |
protected Checksum checksum;
|
|
64 |
|
|
65 |
@Param({"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"})
|
|
66 |
private int offset;
|
|
67 |
|
|
68 |
@Benchmark
|
|
69 |
@OperationsPerInvocation(1024)
|
|
70 |
public void byteArray_1K() {
|
|
71 |
checksum.update(unalignedByteArray_1k, offset, 1024);
|
|
72 |
}
|
|
73 |
|
|
74 |
@Benchmark
|
|
75 |
@OperationsPerInvocation(1024)
|
|
76 |
public void wrappedByteBuffer_1K() {
|
|
77 |
unalignedWrappedByteBuffer_1k.position(offset);
|
|
78 |
unalignedWrappedByteBuffer_1k.limit(offset + 1024);
|
|
79 |
checksum.update(unalignedWrappedByteBuffer_1k);
|
|
80 |
}
|
|
81 |
|
|
82 |
@Benchmark
|
|
83 |
@OperationsPerInvocation(1024)
|
|
84 |
public void directByteBuffer_1K() {
|
|
85 |
unalignedDirectByteBuffer_1k.position(offset);
|
|
86 |
unalignedWrappedByteBuffer_1k.limit(offset + 1024);
|
|
87 |
checksum.update(unalignedDirectByteBuffer_1k);
|
|
88 |
}
|
|
89 |
}
|