brandboat commented on code in PR #16231: URL: https://github.com/apache/kafka/pull/16231#discussion_r1632327595
########## jmh-benchmarks/src/main/java/org/apache/kafka/jmh/util/ConcurrentMapBenchmark.java: ########## @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License 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 org.apache.kafka.jmh.util; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import org.apache.kafka.common.utils.CopyOnWriteMap; +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.Param; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Threads; +import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.infra.Blackhole; + +@State(Scope.Benchmark) +@Fork(value = 1) +@Warmup(iterations = 5) +@Measurement(iterations = 15) +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +@Threads(2) +public class ConcurrentMapBenchmark { + @Param({"1000000"}) + private int times; + + @Param({"100"}) + private int mapSize; + + // execute 1 computeIfAbsent per 10000 loops + @Param({"10000"}) + private int writePerLoops; + + private Map<Integer, Integer> concurrentHashMap; + private Map<Integer, Integer> copyOnWriteMap; + + @Setup + public void setup() { + Map<Integer, Integer> mapTemplate = IntStream.range(0, mapSize).boxed() + .collect(Collectors.toMap(i -> i, i -> i)); + concurrentHashMap = new ConcurrentHashMap<>(mapTemplate); + copyOnWriteMap = new CopyOnWriteMap<>(mapTemplate); + } + + @Benchmark + public void testConcurrentHashMap(Blackhole blackhole) { Review Comment: thanks ! now the benchmark result are as belows. ``` # OS: openSUSE Tumbleweed # JMH version: 1.37 # VM version: JDK 21.0.3, OpenJDK 64-Bit Server VM, 21.0.3+9-LTS (zulu) # CPU: 13th Gen Intel(R) Core(TM) i9-13980HX Benchmark (mapSize) (writePercentage) Mode Cnt Score Error Units ConcurrentMapBenchmark.testConcurrentHashMapEntrySet 100 0.1 avgt 15 2.368 ± 0.066 ns/op ConcurrentMapBenchmark.testConcurrentHashMapGet 100 0.1 avgt 15 4.385 ± 0.013 ns/op ConcurrentMapBenchmark.testConcurrentHashMapGetRandom 100 0.1 avgt 15 8.895 ± 0.019 ns/op ConcurrentMapBenchmark.testConcurrentHashMapValues 100 0.1 avgt 15 2.289 ± 0.052 ns/op ConcurrentMapBenchmark.testCopyOnWriteMapEntrySet 100 0.1 avgt 15 2.442 ± 0.072 ns/op ConcurrentMapBenchmark.testCopyOnWriteMapGet 100 0.1 avgt 15 4.664 ± 0.014 ns/op ConcurrentMapBenchmark.testCopyOnWriteMapGetRandom 100 0.1 avgt 15 6.243 ± 0.033 ns/op ConcurrentMapBenchmark.testCopyOnWriteMapValues 100 0.1 avgt 15 2.435 ± 0.174 ns/op ``` -- 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]
