|
| 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 | +} |
0 commit comments