jsancio commented on a change in pull request #10324: URL: https://github.com/apache/kafka/pull/10324#discussion_r594806269
########## File path: jmh-benchmarks/src/main/java/org/apache/kafka/jmh/timeline/TimelineHashMapBenchmark.java ########## @@ -87,4 +189,129 @@ } return map; } + + @Benchmark + public Map<Integer, String> testUpdateEntriesInHashMap(HashMapInput input) { + for (Integer key : input.keys) { + input.map.put(key, String.valueOf(key)); + } + return input.map; + } + + @Benchmark + public scala.collection.Map testUpdateEntriesInImmutableMap(ImmutableMapInput input) { + scala.collection.immutable.HashMap<Integer, String> map = input.map; + for (Integer key : input.keys) { + map = map.updated(key, String.valueOf(key)); + } + return map; + } + + @Benchmark + public Map<Integer, String> testUpdateEntriesInTimelineMap(TimelineMapInput input) { + for (Integer key : input.keys) { + input.map.put(key, String.valueOf(key)); + } + return input.map; + } + + @Benchmark + public Map<Integer, String> testUpdateEntriesWithSnapshots(TimelineMapInput input) { + long epoch = 0; + int j = 0; + for (Integer key : input.keys) { + if (j > 1_000) { + input.snapshotRegistry.deleteSnapshotsUpTo(epoch - 10_000); + input.snapshotRegistry.createSnapshot(epoch); + j = 0; + } else { + j++; + } + input.map.put(key, String.valueOf(key)); + epoch++; + } + return input.map; + } + + @Benchmark + public Map<Integer, String> testRemoveEntriesInHashMap(HashMapInput input) { + for (Integer key : input.keys) { + input.map.remove(key); + } + return input.map; + } + + @Benchmark + public scala.collection.Map testRemoveEntriesInImmutableMap(ImmutableMapInput input) { + scala.collection.immutable.HashMap<Integer, String> map = input.map; + for (Integer key : input.keys) { + map = map.removed(key); + } + return map; + } + + @Benchmark + public Map<Integer, String> testRemoveEntriesInTimelineMap(TimelineMapInput input) { + for (Integer key : input.keys) { + input.map.remove(key); + } + return input.map; + } + + @Benchmark + public Map<Integer, String> testRemoveEntriesWithSnapshots(TimelineMapInput input) { + long epoch = 0; + int j = 0; + for (Integer key : input.keys) { + if (j > 1_000) { + input.snapshotRegistry.deleteSnapshotsUpTo(epoch - 10_000); + input.snapshotRegistry.createSnapshot(epoch); + j = 0; + } else { + j++; + } + input.map.remove(key, String.valueOf(key)); + epoch++; + } + return input.map; + } + + @Benchmark + public int testIterateEntriesInHashMap(HashMapInput input) { + int count = 0; + for (HashMap.Entry<Integer, String> entry : input.map.entrySet()) { + count++; + } + return count; + } + + @Benchmark + public int testIterateEntriesInImmutableMap(ImmutableMapInput input) { + int count = 0; + scala.collection.Iterator<scala.Tuple2<Integer, String>> iterator = input.map.iterator(); + while (iterator.hasNext()) { + iterator.next(); + count++; + } + return count; + } + + @Benchmark + public int testIterateEntriesWithSnapshots(TimelineMapSnapshotInput input) { + int count = 0; + for (TimelineHashMap.Entry<Integer, String> entry : input.map.entrySet(input.epoch)) { Review comment: @cmccabe It looks like this benchmark fails with the following exception. Any idea on what's the issue? ``` java.lang.ArrayIndexOutOfBoundsException: Index 1024 out of bounds for length 1024 at org.apache.kafka.timeline.BaseHashTable.unpackSlot(BaseHashTable.java:210) at org.apache.kafka.timeline.SnapshottableHashTable$HistoricalIterator.hasNext(SnapshottableHashTable.java:255) at org.apache.kafka.timeline.TimelineHashMap$EntryIterator.hasNext(TimelineHashMap.java:359) at org.apache.kafka.jmh.timeline.TimelineHashMapBenchmark.testIterateEntriesWithSnapshots(TimelineHashMapBenchmark.java:303) at org.apache.kafka.jmh.timeline.jmh_generated.TimelineHashMapBenchmark_testIterateEntriesWithSnapshots_jmhTest.testIterateEntriesWithSnapshots_avgt_jmhStub(TimelineHashMapBenchmark_testIterateEntriesWithSnapshots_jmhTest.java:204) at org.apache.kafka.jmh.timeline.jmh_generated.TimelineHashMapBenchmark_testIterateEntriesWithSnapshots_jmhTest.testIterateEntriesWithSnapshots_AverageTime(TimelineHashMapBenchmark_testIterateEntriesWithSnapshots_jmhTest.java:162) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.openjdk.jmh.runner.BenchmarkHandler$BenchmarkTask.call(BenchmarkHandler.java:453) at org.openjdk.jmh.runner.BenchmarkHandler$BenchmarkTask.call(BenchmarkHandler.java:437) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) at java.base/java.lang.Thread.run(Thread.java:834) ``` ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org