Skip to content

Instantly share code, notes, and snippets.

@lbloder
Last active October 22, 2024 07:48
Show Gist options
  • Save lbloder/d43555a22847e8558ea8e8d1770c6ee5 to your computer and use it in GitHub Desktop.
Save lbloder/d43555a22847e8558ea8e8d1770c6ee5 to your computer and use it in GitHub Desktop.
SentryID Generation Benchmark
package benchmark;
import io.sentry.SentryUUID;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import java.util.UUID;
@State(Scope.Thread)
public class UUIDBenchmark {
public UUID uuid;
public String uuidString;
//Generate new random UUID before each method invocation
@Setup(Level.Invocation)
public void setup() {
uuid = UUID.randomUUID();
uuidString = UUID.randomUUID().toString();
}
@Benchmark
public void uuidGenerationSecureRandom(Blackhole blackhole) throws InterruptedException {
blackhole.consume(UUID.randomUUID());
}
@Benchmark
public void uuidGenerationRandom(Blackhole blackhole) throws InterruptedException {
blackhole.consume(SentryUUID.randomUUID());
}
@Benchmark
public void uuidToString(Blackhole blackhole) throws InterruptedException {
blackhole.consume(uuid.toString());
}
@Benchmark
public void uuidPreparedStringReplace(Blackhole blackhole) throws InterruptedException {
blackhole.consume(uuidString.replace("-", ""));
}
@Benchmark
public void uuidPreparedStringSubstring(Blackhole blackhole) throws InterruptedException {
StringBuilder builder = new StringBuilder();
builder.append(uuidString, 0, 8);
builder.append(uuidString, 9, 13);
builder.append(uuidString, 14, 18);
builder.append(uuidString, 19, 23);
builder.append(uuidString, 24, 36);
blackhole.consume(builder.toString());
}
@Benchmark
public void uuidToStringAndReplace(Blackhole blackhole) throws InterruptedException {
blackhole.consume(uuid.toString().replace("-", ""));
}
@Benchmark
public void uuidToStringAndStringBuilder(Blackhole blackhole) throws InterruptedException {
String localUuidString = uuid.toString();
StringBuilder builder = new StringBuilder();
builder.append(localUuidString, 0, 8);
builder.append(localUuidString, 9, 13);
builder.append(localUuidString, 14, 18);
builder.append(localUuidString, 19, 23);
builder.append(localUuidString, 24, 36);
blackhole.consume(builder.toString());
}
@Benchmark
public void uuidToSentryStringFastUUIDString(Blackhole blackhole) throws InterruptedException {
blackhole.consume(SentryUUID.toSentryIdString(uuid));
}
@Benchmark
public void generateSentryIdSecureRandomToStringAndReplace(Blackhole blackhole) throws InterruptedException {
blackhole.consume(UUID.randomUUID().toString().replace("-", ""));
}
@Benchmark
public void generateSentryIdSecureRandomToStringAndStringBuilder(Blackhole blackhole) throws InterruptedException {
String localUuidString = UUID.randomUUID().toString();
StringBuilder builder = new StringBuilder();
builder.append(localUuidString, 0, 8);
builder.append(localUuidString, 9, 13);
builder.append(localUuidString, 14, 18);
builder.append(localUuidString, 19, 23);
builder.append(localUuidString, 24, 36);
blackhole.consume(builder.toString());
}
@Benchmark
public void generateSentryIdSecureRandomFastUUIDString(Blackhole blackhole) throws InterruptedException {
blackhole.consume(SentryUUID.toSentryIdString(UUID.randomUUID()));
}
@Benchmark
public void generateSentryIdRandomToStringAndReplace(Blackhole blackhole) throws InterruptedException {
blackhole.consume(SentryUUID.randomUUID().toString().replace("-", ""));
}
@Benchmark
public void generateSentryIdRandomToStringAndStringBuilder(Blackhole blackhole) throws InterruptedException {
String localUuidString = SentryUUID.randomUUID().toString();
StringBuilder builder = new StringBuilder();
builder.append(localUuidString, 0, 8);
builder.append(localUuidString, 9, 13);
builder.append(localUuidString, 14, 18);
builder.append(localUuidString, 19, 23);
builder.append(localUuidString, 24, 36);
blackhole.consume(builder.toString());
}
@Benchmark
public void generateSentryIdRandomFastUUIDString(Blackhole blackhole) throws InterruptedException {
blackhole.consume(SentryUUID.toSentryIdString(SentryUUID.randomUUID()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment