Skip to content

Commit

Permalink
Use singleton SecureRandom (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anuraag Agrawal authored Jun 9, 2020
1 parent 7e8c33c commit b76d374
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,16 @@
@Deprecated
public class ThreadLocalStorage {

private static final SecureRandom SECURE_RANDOM = new SecureRandom();

static class LocalEntity extends ThreadLocal<Entity> {
@Override
protected Entity initialValue() {
return null;
}
}

static class LocalSecureRandom extends ThreadLocal<SecureRandom> {
@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();
Expand All @@ -63,6 +57,6 @@ public static void clear() {
}

public static SecureRandom getRandom() {
return CURRENT_RANDOM.get();
return SECURE_RANDOM;
}
}

0 comments on commit b76d374

Please sign in to comment.