You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@dubbo.apache.org by 徐靖峰 <ki...@foxmail.com> on 2018/09/05 04:16:51 UTC

[Enhancement] Replace Random with ThreadLocalRandom

Hi community,
i changed the random implementation  for dubbo, you can see as below.

Details

This issue links to this pull request (https://github.com/apache/incubator-dubbo/pull/2433)

A little change, replace Random with ThreadLocalRandom in LeastActiveLoadBalance and RandomLoadBalance.

Use of ThreadLocalRandom rather than shared Random objects in concurrent programs will typically encounter much less overhead and contention.

Use of ThreadLocalRandom is particularly appropriate when multiple tasks .

Benchmark

There are some attentions need to be noticed before benchmark.

third-party dependency need use carefully.

bugs fix need to trace from upstream repository.

performance benchmark

I have compared the three cases : Random,ThreadLocalRandom from JDK, ThreadLocalRandom from netty.
@BenchmarkMode({Mode.AverageTime}) @OutputTimeUnit(TimeUnit.NANOSECONDS) @Warmup(iterations = 3, time = 5) @Measurement(iterations = 3, time = 5) @Threads(50) @Fork(1) @State(Scope.Benchmark) public class RandomBenchmark {     Random random = new Random();     @Benchmark     public int random() {         return random.nextInt();     }     @Benchmark     public int threadLocalRandom() {         return ThreadLocalRandom.current().nextInt();     }     @Benchmark     public int nettyThreadLocalRandom() {         return io.netty.util.internal.ThreadLocalRandom.current().nextInt();     }     public static void main(String[] args) throws RunnerException {         Options opt = new OptionsBuilder()                 .include(RandomBenchmark.class.getSimpleName())                 .build();         new Runner(opt).run();     } } 
and below is our benchmark result under 50 concurrency
Benchmark                               Mode  Cnt     Score      Error  Units RandomBenchmark.nettyThreadLocalRandom  avgt    3   160.321 ±   83.518  ns/op RandomBenchmark.random                  avgt    3  3281.972 ± 2093.187  ns/op RandomBenchmark.threadLocalRandom       avgt    3    91.505 ±   39.702  ns/op 
From the result we can know the truth

Both the performance (according to above benchmark) and the stability(it is from the JDK)
JDK 's ThreadLocalRandom looks good.

Re: [Enhancement] Replace Random with ThreadLocalRandom

Posted by yuhang xiu <ca...@gmail.com>.
Hi,
Good job!

There is a similar pr here (using the netty version, copy code):
https://github.com/apache/incubator-dubbo/pull/2439

I saw that you submitted a test example about performance. I think it is
more recommended to use the jdk version if the gap is not large, so I
closed the duplicate one.

If there are other considerations, re-open this duplicate one.

Best regards!

徐靖峰 <ki...@foxmail.com> 于2018年9月5日周三 下午12:17写道:

> Hi community,
> i changed the random implementation  for dubbo, you can see as below.
>
> Details
>
> This issue links to this pull request (
> https://github.com/apache/incubator-dubbo/pull/2433)
>
> A little change, replace Random with ThreadLocalRandom in
> LeastActiveLoadBalance and RandomLoadBalance.
>
> Use of ThreadLocalRandom rather than shared Random objects in concurrent
> programs will typically encounter much less overhead and contention.
>
> Use of ThreadLocalRandom is particularly appropriate when multiple tasks .
>
> Benchmark
>
> There are some attentions need to be noticed before benchmark.
>
> third-party dependency need use carefully.
>
> bugs fix need to trace from upstream repository.
>
> performance benchmark
>
> I have compared the three cases : Random,ThreadLocalRandom from JDK,
> ThreadLocalRandom from netty.
> @BenchmarkMode({Mode.AverageTime}) @OutputTimeUnit(TimeUnit.NANOSECONDS)
> @Warmup(iterations = 3, time = 5) @Measurement(iterations = 3, time = 5)
> @Threads(50) @Fork(1) @State(Scope.Benchmark) public class RandomBenchmark
> {     Random random = new Random();     @Benchmark     public int random()
> {         return random.nextInt();     }     @Benchmark     public int
> threadLocalRandom() {         return
> ThreadLocalRandom.current().nextInt();     }     @Benchmark     public int
> nettyThreadLocalRandom() {         return
> io.netty.util.internal.ThreadLocalRandom.current().nextInt();     }
>  public static void main(String[] args) throws RunnerException {
>  Options opt = new OptionsBuilder()
>  .include(RandomBenchmark.class.getSimpleName())                 .build();
>        new Runner(opt).run();     } }
> and below is our benchmark result under 50 concurrency
> Benchmark                               Mode  Cnt     Score      Error
> Units RandomBenchmark.nettyThreadLocalRandom  avgt    3   160.321 ±
>  83.518  ns/op RandomBenchmark.random                  avgt    3  3281.972
> ± 2093.187  ns/op RandomBenchmark.threadLocalRandom       avgt    3
> 91.505 ±   39.702  ns/op
> From the result we can know the truth
>
> Both the performance (according to above benchmark) and the stability(it
> is from the JDK)
> JDK 's ThreadLocalRandom looks good.