-
Notifications
You must be signed in to change notification settings - Fork 894
Adding Benchmark test for Checksums #5701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ad96bda
Adding Benchmark test for Checksums
joviegas 7ebdce6
Merge branch 'master' into joviegas/benchmarks
joviegas cc983f0
Merge branch 'master' into joviegas/benchmarks
joviegas 958c487
Addressed PR comments
joviegas 17b4b98
Merge branch 'master' into joviegas/benchmarks
joviegas 34420fc
Addressed PR comments
joviegas 5ceb599
Checkstyle issue fixed
joviegas df83bc7
Merge branch 'master' into joviegas/benchmarks
joviegas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
...benchmarks/src/main/java/software/amazon/awssdk/benchmark/checksum/CheckSumBenchmark.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.benchmark.checksum; | ||
|
||
import java.util.Locale; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.Fork; | ||
import org.openjdk.jmh.annotations.Level; | ||
import org.openjdk.jmh.annotations.Measurement; | ||
import org.openjdk.jmh.annotations.Mode; | ||
import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
import org.openjdk.jmh.annotations.Param; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.annotations.Warmup; | ||
import org.openjdk.jmh.infra.Blackhole; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.concurrent.TimeUnit; | ||
import software.amazon.awssdk.checksums.DefaultChecksumAlgorithm; | ||
import software.amazon.awssdk.checksums.SdkChecksum; | ||
|
||
@State(Scope.Benchmark) | ||
@Warmup(iterations = 3, time = 3, timeUnit = TimeUnit.SECONDS) | ||
@Measurement(iterations = 5, time = 5, timeUnit = TimeUnit.SECONDS) | ||
@Fork(1) | ||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(TimeUnit.NANOSECONDS) | ||
public class CheckSumBenchmark { | ||
joviegas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@State(Scope.Thread) | ||
public static class ChecksumState { | ||
|
||
@Param( {"128B", "4KB", "128KB", "1MB"}) | ||
public String size; | ||
|
||
@Param( {"MD5", "CRC32", "CRC32C", "SHA1", "SHA256"}) | ||
public String checksumProvider; | ||
|
||
private byte[] payload; | ||
private SdkChecksum sdkChecksum; | ||
|
||
@Setup(Level.Trial) | ||
public void setup() { | ||
// Initialize the correct checksum provider based on the parameter | ||
switch (checksumProvider.toUpperCase(Locale.ROOT)) { | ||
case "MD5": | ||
sdkChecksum = SdkChecksum.forAlgorithm(DefaultChecksumAlgorithm.MD5); | ||
break; | ||
case "CRC32": | ||
sdkChecksum = SdkChecksum.forAlgorithm(DefaultChecksumAlgorithm.CRC32); | ||
break; | ||
case "CRC32C": | ||
sdkChecksum = SdkChecksum.forAlgorithm(DefaultChecksumAlgorithm.CRC32C); | ||
break; | ||
case "SHA1": | ||
sdkChecksum = SdkChecksum.forAlgorithm(DefaultChecksumAlgorithm.SHA1); | ||
break; | ||
case "SHA256": | ||
sdkChecksum = SdkChecksum.forAlgorithm(DefaultChecksumAlgorithm.SHA256); | ||
break; | ||
|
||
default: | ||
throw new IllegalArgumentException("Invalid checksumProvider: " + checksumProvider); | ||
} | ||
// Initialize the payload based on the size parameter | ||
switch (size) { | ||
case "128B": | ||
payload = generateStringOfSize(128).getBytes(StandardCharsets.UTF_8); | ||
break; | ||
case "4KB": | ||
payload = generateStringOfSize(4 * 1024).getBytes(StandardCharsets.UTF_8); | ||
break; | ||
case "128KB": | ||
payload = generateStringOfSize(128 * 1024).getBytes(StandardCharsets.UTF_8); | ||
break; | ||
case "1MB": | ||
payload = generateStringOfSize(1000 * 1024).getBytes(StandardCharsets.UTF_8); | ||
break; | ||
default: | ||
throw new IllegalArgumentException("Invalid size: " + size); | ||
} | ||
} | ||
} | ||
|
||
@Benchmark | ||
public void updateEntireByteArrayChecksum(ChecksumState state, Blackhole blackhole) { | ||
state.sdkChecksum.reset(); // Ensure we reset the checksum before each run | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we do reset in tearDown methond There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
state.sdkChecksum.update(state.payload); | ||
state.sdkChecksum.getChecksumBytes(); | ||
blackhole.consume(state.sdkChecksum); | ||
} | ||
|
||
@Benchmark | ||
public void updateIndividualByteChecksumOneByteATime(ChecksumState state, Blackhole blackhole) { | ||
state.sdkChecksum.reset();// Ensure we reset the checksum before each run | ||
for (byte b : state.payload) { | ||
state.sdkChecksum.update(b); | ||
} | ||
state.sdkChecksum.getChecksumBytes(); | ||
blackhole.consume(state.sdkChecksum); | ||
} | ||
|
||
private static String generateStringOfSize(int byteSize) { | ||
String result = new String(new char[byteSize]).replace('\0', 'A'); // Approximate | ||
while (result.getBytes(StandardCharsets.UTF_8).length > byteSize) { | ||
result = result.substring(0, result.length() - 1); // Adjust to exact size | ||
} | ||
return result; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is 3 seconds sufficient for warmup? Is the result consistent after warming up?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did test with both 3 and 15 , the relative Score is same .
However changed it to 15 to be consistent with other test