Skip to content

Commit 47fcc27

Browse files
authored
Adding Benchmark test for Checksums (#5701)
* Adding Benchmark test for Checksums * Addressed PR comments * Addressed PR comments * Checkstyle issue fixed
1 parent 611dfd1 commit 47fcc27

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.benchmark.checksum;
17+
18+
import java.nio.charset.StandardCharsets;
19+
import java.util.Locale;
20+
import java.util.concurrent.TimeUnit;
21+
import org.openjdk.jmh.annotations.Benchmark;
22+
import org.openjdk.jmh.annotations.BenchmarkMode;
23+
import org.openjdk.jmh.annotations.Fork;
24+
import org.openjdk.jmh.annotations.Level;
25+
import org.openjdk.jmh.annotations.Measurement;
26+
import org.openjdk.jmh.annotations.Mode;
27+
import org.openjdk.jmh.annotations.OutputTimeUnit;
28+
import org.openjdk.jmh.annotations.Param;
29+
import org.openjdk.jmh.annotations.Scope;
30+
import org.openjdk.jmh.annotations.Setup;
31+
import org.openjdk.jmh.annotations.State;
32+
import org.openjdk.jmh.annotations.TearDown;
33+
import org.openjdk.jmh.annotations.Warmup;
34+
import org.openjdk.jmh.infra.Blackhole;
35+
import software.amazon.awssdk.checksums.DefaultChecksumAlgorithm;
36+
import software.amazon.awssdk.checksums.SdkChecksum;
37+
38+
@State(Scope.Benchmark)
39+
@Warmup(iterations = 3, time = 15, timeUnit = TimeUnit.SECONDS)
40+
@Measurement(iterations = 5, time = 10, timeUnit = TimeUnit.SECONDS)
41+
@Fork(2)
42+
@BenchmarkMode(Mode.AverageTime)
43+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
44+
public class ChecksumBenchmark {
45+
@State(Scope.Thread)
46+
public static class ChecksumState {
47+
48+
@Param({"128B", "4KB", "128KB", "1MB"})
49+
public String size;
50+
51+
@Param({"MD5", "CRC32", "CRC32C", "SHA1", "SHA256", "CRC64NVME"})
52+
public String checksumProvider;
53+
54+
private byte[] payload;
55+
private SdkChecksum sdkChecksum;
56+
57+
@Setup(Level.Trial)
58+
public void setup() {
59+
// Initialize the correct checksum provider based on the parameter
60+
switch (checksumProvider.toUpperCase(Locale.ROOT)) {
61+
case "MD5":
62+
sdkChecksum = SdkChecksum.forAlgorithm(DefaultChecksumAlgorithm.MD5);
63+
break;
64+
case "CRC32":
65+
sdkChecksum = SdkChecksum.forAlgorithm(DefaultChecksumAlgorithm.CRC32);
66+
break;
67+
case "CRC32C":
68+
sdkChecksum = SdkChecksum.forAlgorithm(DefaultChecksumAlgorithm.CRC32C);
69+
break;
70+
case "SHA1":
71+
sdkChecksum = SdkChecksum.forAlgorithm(DefaultChecksumAlgorithm.SHA1);
72+
break;
73+
case "SHA256":
74+
sdkChecksum = SdkChecksum.forAlgorithm(DefaultChecksumAlgorithm.SHA256);
75+
break;
76+
case "CRC64NVME":
77+
sdkChecksum = SdkChecksum.forAlgorithm(DefaultChecksumAlgorithm.CRC64NVME);
78+
break;
79+
80+
default:
81+
throw new IllegalArgumentException("Invalid checksumProvider: " + checksumProvider);
82+
}
83+
// Initialize the payload based on the size parameter
84+
switch (size) {
85+
case "128B":
86+
payload = generateStringOfSize(128).getBytes(StandardCharsets.UTF_8);
87+
break;
88+
case "4KB":
89+
payload = generateStringOfSize(4 * 1024).getBytes(StandardCharsets.UTF_8);
90+
break;
91+
case "128KB":
92+
payload = generateStringOfSize(128 * 1024).getBytes(StandardCharsets.UTF_8);
93+
break;
94+
case "1MB":
95+
payload = generateStringOfSize(1000 * 1024).getBytes(StandardCharsets.UTF_8);
96+
break;
97+
default:
98+
throw new IllegalArgumentException("Invalid size: " + size);
99+
}
100+
}
101+
}
102+
103+
@Benchmark
104+
public void updateEntireByteArrayChecksum(ChecksumState state, Blackhole blackhole) {
105+
state.sdkChecksum.update(state.payload);
106+
state.sdkChecksum.getChecksumBytes();
107+
blackhole.consume(state.sdkChecksum);
108+
}
109+
110+
@TearDown
111+
public void tearDown(ChecksumState state) {
112+
state.sdkChecksum.reset();
113+
}
114+
115+
@Benchmark
116+
public void updateIndividualByteChecksumOneByteATime(ChecksumState state, Blackhole blackhole) {
117+
for (byte b : state.payload) {
118+
state.sdkChecksum.update(b);
119+
}
120+
state.sdkChecksum.getChecksumBytes();
121+
blackhole.consume(state.sdkChecksum);
122+
}
123+
124+
private static String generateStringOfSize(int byteSize) {
125+
String result = new String(new char[byteSize]).replace('\0', 'A'); // Approximate
126+
while (result.getBytes(StandardCharsets.UTF_8).length > byteSize) {
127+
result = result.substring(0, result.length() - 1); // Adjust to exact size
128+
}
129+
return result;
130+
}
131+
}

0 commit comments

Comments
 (0)