mxsm commented on PR #6239:
URL: https://github.com/apache/rocketmq/pull/6239#issuecomment-1453799771

   ```java
   public class ThreadLocalIndexOptimize {
       private final ThreadLocal<AtomicInteger> threadLocalIndex = new 
ThreadLocal<>();
       private final Random random = new Random();
       private final static int POSITIVE_MASK = 0x7FFFFFFF;
   
       public int incrementAndGet() {
           AtomicInteger index = this.threadLocalIndex.get();
           if (null == index) {
               index = new AtomicInteger(random.nextInt());
               threadLocalIndex.set(index);
           }
           return Math.abs(index.incrementAndGet() & POSITIVE_MASK);
       }
   
       @Override
       public String toString() {
           return "ThreadLocalIndex{" +
               "threadLocalIndex=" + threadLocalIndex.get() +
               '}';
       }
   }
   ```
   benchmark
   ```java
   @BenchmarkMode(Mode.Throughput)
   @Warmup(iterations = 3, time = 1)
   @Measurement(iterations = 5, time = 5)
   @Fork(1)
   @State(value = Scope.Benchmark)
   @OutputTimeUnit(TimeUnit.SECONDS)
   public class ThreadLocalIndexBenchmark {
   
       private ThreadLocalIndex index = new ThreadLocalIndex();
   
       private ThreadLocalIndexOptimize optimize = new 
ThreadLocalIndexOptimize();
   
       @Benchmark
       @Threads(1)
       public void threadLocalIndex_1() {
           index.incrementAndGet();
       }
   
       @Benchmark
       @Threads(1)
       public void threadLocalIndexOptimize_1() {
           optimize.incrementAndGet();
       }
   
       @Benchmark
       @Threads(8)
       public void threadLocalIndex_8() {
           index.incrementAndGet();
       }
   
       @Benchmark
       @Threads(8)
       public void threadLocalIndexOptimize_8() {
           optimize.incrementAndGet();
       }
   
   
       public static void main(String[] args) throws RunnerException {
           Options opt = new OptionsBuilder()
               .include(ThreadLocalIndexBenchmark.class.getSimpleName())
               .result("result.json")
               .resultFormat(ResultFormatType.JSON).build();
           new Runner(opt).run();
       }
   }
   ```
   result:
   ```
   REMEMBER: The numbers below are just data. To gain reusable insights, you 
need to follow up on
   why the numbers are the way they are. Use profilers (see -prof, -lprof), 
design factorial
   experiments, perform baseline and negative tests that provide experimental 
control, make sure
   the benchmarking environment is safe on JVM/OS/HW level, ask for reviews 
from the domain experts.
   Do not assume the numbers tell you what you want them to tell.
   
   Benchmark                                              Mode  Cnt           
Score          Error  Units
   ThreadLocalIndexBenchmark.threadLocalIndexOptimize_1  thrpt    5   
170159993.824 ±  1318284.420  ops/s
   ThreadLocalIndexBenchmark.threadLocalIndexOptimize_8  thrpt    5  
1055535073.108 ± 23152204.877  ops/s
   ThreadLocalIndexBenchmark.threadLocalIndex_1          thrpt    5   
234089647.698 ± 16986301.995  ops/s
   ThreadLocalIndexBenchmark.threadLocalIndex_8          thrpt    5   
776787743.442 ± 50277549.179  ops/s
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to