Skip to content

Commit b76d374

Browse files
author
Anuraag Agrawal
authored
Use singleton SecureRandom (#162)
1 parent 7e8c33c commit b76d374

File tree

2 files changed

+88
-9
lines changed

2 files changed

+88
-9
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package com.amazonaws.xray.entities;
17+
18+
import com.amazonaws.xray.ThreadLocalStorage;
19+
import java.math.BigInteger;
20+
import java.security.SecureRandom;
21+
import java.util.concurrent.ThreadLocalRandom;
22+
import java.util.concurrent.TimeUnit;
23+
import org.openjdk.jmh.annotations.Benchmark;
24+
import org.openjdk.jmh.annotations.BenchmarkMode;
25+
import org.openjdk.jmh.annotations.Fork;
26+
import org.openjdk.jmh.annotations.Measurement;
27+
import org.openjdk.jmh.annotations.Mode;
28+
import org.openjdk.jmh.annotations.OutputTimeUnit;
29+
import org.openjdk.jmh.annotations.Threads;
30+
import org.openjdk.jmh.annotations.Warmup;
31+
import org.openjdk.jmh.runner.Runner;
32+
import org.openjdk.jmh.runner.RunnerException;
33+
import org.openjdk.jmh.runner.options.Options;
34+
import org.openjdk.jmh.runner.options.OptionsBuilder;
35+
36+
@Measurement(iterations = 5, time = 1)
37+
@Warmup(iterations = 10, time = 1)
38+
@Fork(3)
39+
@BenchmarkMode(Mode.SampleTime)
40+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
41+
@Threads(16)
42+
public class IdsBenchmark {
43+
44+
private static final SecureRandom SECURE_RANDOM = new SecureRandom();
45+
46+
@Benchmark
47+
public BigInteger traceId_secureRandom() {
48+
return new BigInteger(96, SECURE_RANDOM);
49+
}
50+
51+
@Benchmark
52+
public BigInteger traceId_threadLocalSecureRandom() {
53+
return new BigInteger(96, ThreadLocalStorage.getRandom());
54+
}
55+
56+
@Benchmark
57+
public BigInteger traceId_threadLocalRandom() {
58+
return new BigInteger(96, ThreadLocalRandom.current());
59+
}
60+
61+
@Benchmark
62+
public String segmentId_secureRandom() {
63+
return Long.toString(SECURE_RANDOM.nextLong() >>> 1, 16);
64+
}
65+
66+
@Benchmark
67+
public String segmentId_threadLocalSecureRandom() {
68+
return Long.toString(ThreadLocalStorage.getRandom().nextLong() >>> 1, 16);
69+
}
70+
71+
@Benchmark
72+
public String segmentId_threadLocalRandom() {
73+
return Long.toString(ThreadLocalRandom.current().nextLong() >>> 1, 16);
74+
}
75+
76+
// Convenience main entry-point
77+
public static void main(String[] args) throws RunnerException {
78+
Options opt = new OptionsBuilder()
79+
.addProfiler("gc")
80+
.include(".*" + IdsBenchmark.class.getSimpleName())
81+
.build();
82+
83+
new Runner(opt).run();
84+
}
85+
}

aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/ThreadLocalStorage.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,16 @@
2525
@Deprecated
2626
public class ThreadLocalStorage {
2727

28+
private static final SecureRandom SECURE_RANDOM = new SecureRandom();
29+
2830
static class LocalEntity extends ThreadLocal<Entity> {
2931
@Override
3032
protected Entity initialValue() {
3133
return null;
3234
}
3335
}
3436

35-
static class LocalSecureRandom extends ThreadLocal<SecureRandom> {
36-
@Override
37-
protected SecureRandom initialValue() {
38-
return new SecureRandom();
39-
}
40-
}
41-
4237
private static final LocalEntity CURRENT_ENTITY = new LocalEntity();
43-
private static final LocalSecureRandom CURRENT_RANDOM = new LocalSecureRandom();
4438

4539
public static Entity get() {
4640
return CURRENT_ENTITY.get();
@@ -63,6 +57,6 @@ public static void clear() {
6357
}
6458

6559
public static SecureRandom getRandom() {
66-
return CURRENT_RANDOM.get();
60+
return SECURE_RANDOM;
6761
}
6862
}

0 commit comments

Comments
 (0)