From b76d374699def8d8d117b0765631c2ed06486283 Mon Sep 17 00:00:00 2001 From: Anuraag Agrawal Date: Tue, 9 Jun 2020 09:03:27 +0900 Subject: [PATCH] Use singleton SecureRandom (#162) --- .../amazonaws/xray/entities/IdsBenchmark.java | 85 +++++++++++++++++++ .../amazonaws/xray/ThreadLocalStorage.java | 12 +-- 2 files changed, 88 insertions(+), 9 deletions(-) create mode 100644 aws-xray-recorder-sdk-benchmark/tst/main/java/com/amazonaws/xray/entities/IdsBenchmark.java diff --git a/aws-xray-recorder-sdk-benchmark/tst/main/java/com/amazonaws/xray/entities/IdsBenchmark.java b/aws-xray-recorder-sdk-benchmark/tst/main/java/com/amazonaws/xray/entities/IdsBenchmark.java new file mode 100644 index 00000000..fe966ef5 --- /dev/null +++ b/aws-xray-recorder-sdk-benchmark/tst/main/java/com/amazonaws/xray/entities/IdsBenchmark.java @@ -0,0 +1,85 @@ +/* + * 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 com.amazonaws.xray.entities; + +import com.amazonaws.xray.ThreadLocalStorage; +import java.math.BigInteger; +import java.security.SecureRandom; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.TimeUnit; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Threads; +import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.RunnerException; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +@Measurement(iterations = 5, time = 1) +@Warmup(iterations = 10, time = 1) +@Fork(3) +@BenchmarkMode(Mode.SampleTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@Threads(16) +public class IdsBenchmark { + + private static final SecureRandom SECURE_RANDOM = new SecureRandom(); + + @Benchmark + public BigInteger traceId_secureRandom() { + return new BigInteger(96, SECURE_RANDOM); + } + + @Benchmark + public BigInteger traceId_threadLocalSecureRandom() { + return new BigInteger(96, ThreadLocalStorage.getRandom()); + } + + @Benchmark + public BigInteger traceId_threadLocalRandom() { + return new BigInteger(96, ThreadLocalRandom.current()); + } + + @Benchmark + public String segmentId_secureRandom() { + return Long.toString(SECURE_RANDOM.nextLong() >>> 1, 16); + } + + @Benchmark + public String segmentId_threadLocalSecureRandom() { + return Long.toString(ThreadLocalStorage.getRandom().nextLong() >>> 1, 16); + } + + @Benchmark + public String segmentId_threadLocalRandom() { + return Long.toString(ThreadLocalRandom.current().nextLong() >>> 1, 16); + } + + // Convenience main entry-point + public static void main(String[] args) throws RunnerException { + Options opt = new OptionsBuilder() + .addProfiler("gc") + .include(".*" + IdsBenchmark.class.getSimpleName()) + .build(); + + new Runner(opt).run(); + } +} diff --git a/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/ThreadLocalStorage.java b/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/ThreadLocalStorage.java index 2f3992e0..f1c4b940 100644 --- a/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/ThreadLocalStorage.java +++ b/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/ThreadLocalStorage.java @@ -25,6 +25,8 @@ @Deprecated public class ThreadLocalStorage { + private static final SecureRandom SECURE_RANDOM = new SecureRandom(); + static class LocalEntity extends ThreadLocal { @Override protected Entity initialValue() { @@ -32,15 +34,7 @@ protected Entity initialValue() { } } - static class LocalSecureRandom extends ThreadLocal { - @Override - protected SecureRandom initialValue() { - return new SecureRandom(); - } - } - private static final LocalEntity CURRENT_ENTITY = new LocalEntity(); - private static final LocalSecureRandom CURRENT_RANDOM = new LocalSecureRandom(); public static Entity get() { return CURRENT_ENTITY.get(); @@ -63,6 +57,6 @@ public static void clear() { } public static SecureRandom getRandom() { - return CURRENT_RANDOM.get(); + return SECURE_RANDOM; } }