Skip to content

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 8 commits into from
Nov 29, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

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?

Copy link
Contributor Author

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

@Measurement(iterations = 5, time = 5, timeUnit = TimeUnit.SECONDS)
@Fork(1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class CheckSumBenchmark {
@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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do reset in tearDown methond @TearDown?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
}
}
Loading